s08-s07-SocketsOps.h.diff revision 1bc8a9df
11bc8a9dfSShuo Chen // Copyright 2010, Shuo Chen. All rights reserved. 21bc8a9dfSShuo Chen // http://code.google.com/p/muduo/ 31bc8a9dfSShuo Chen // 41bc8a9dfSShuo Chen // Use of this source code is governed by a BSD-style license 51bc8a9dfSShuo Chen // that can be found in the License file. 61bc8a9dfSShuo Chen 71bc8a9dfSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 81bc8a9dfSShuo Chen // 91bc8a9dfSShuo Chen // This is an internal header file, you should not include this. 101bc8a9dfSShuo Chen 111bc8a9dfSShuo Chen #ifndef MUDUO_NET_SOCKETSOPS_H 121bc8a9dfSShuo Chen #define MUDUO_NET_SOCKETSOPS_H 131bc8a9dfSShuo Chen 141bc8a9dfSShuo Chen #include <arpa/inet.h> 151bc8a9dfSShuo Chen #include <endian.h> 161bc8a9dfSShuo Chen 171bc8a9dfSShuo Chen namespace muduo 181bc8a9dfSShuo Chen { 191bc8a9dfSShuo Chen namespace sockets 201bc8a9dfSShuo Chen { 211bc8a9dfSShuo Chen 221bc8a9dfSShuo Chen // the inline assembler code makes type blur, 231bc8a9dfSShuo Chen // so we disable warnings for a while. 241bc8a9dfSShuo Chen #pragma GCC diagnostic ignored "-Wconversion" 251bc8a9dfSShuo Chen inline uint64_t hostToNetwork64(uint64_t host64) 261bc8a9dfSShuo Chen { 271bc8a9dfSShuo Chen return htobe64(host64); 281bc8a9dfSShuo Chen } 291bc8a9dfSShuo Chen 301bc8a9dfSShuo Chen inline uint32_t hostToNetwork32(uint32_t host32) 311bc8a9dfSShuo Chen { 321bc8a9dfSShuo Chen return htonl(host32); 331bc8a9dfSShuo Chen } 341bc8a9dfSShuo Chen 351bc8a9dfSShuo Chen inline uint16_t hostToNetwork16(uint16_t host16) 361bc8a9dfSShuo Chen { 371bc8a9dfSShuo Chen return htons(host16); 381bc8a9dfSShuo Chen } 391bc8a9dfSShuo Chen 401bc8a9dfSShuo Chen inline uint64_t networkToHost64(uint64_t net64) 411bc8a9dfSShuo Chen { 421bc8a9dfSShuo Chen return be64toh(net64); 431bc8a9dfSShuo Chen } 441bc8a9dfSShuo Chen 451bc8a9dfSShuo Chen inline uint32_t networkToHost32(uint32_t net32) 461bc8a9dfSShuo Chen { 471bc8a9dfSShuo Chen return ntohl(net32); 481bc8a9dfSShuo Chen } 491bc8a9dfSShuo Chen 501bc8a9dfSShuo Chen inline uint16_t networkToHost16(uint16_t net16) 511bc8a9dfSShuo Chen { 521bc8a9dfSShuo Chen return ntohs(net16); 531bc8a9dfSShuo Chen } 541bc8a9dfSShuo Chen #pragma GCC diagnostic error "-Wconversion" 551bc8a9dfSShuo Chen 561bc8a9dfSShuo Chen /// 571bc8a9dfSShuo Chen /// Creates a non-blocking socket file descriptor, 581bc8a9dfSShuo Chen /// abort if any error. 591bc8a9dfSShuo Chen int createNonblockingOrDie(); 601bc8a9dfSShuo Chen 611bc8a9dfSShuo Chen void bindOrDie(int sockfd, const struct sockaddr_in& addr); 621bc8a9dfSShuo Chen void listenOrDie(int sockfd); 631bc8a9dfSShuo Chen int accept(int sockfd, struct sockaddr_in* addr); 641bc8a9dfSShuo Chen void close(int sockfd); 651bc8a9dfSShuo Chen+void shutdownWrite(int sockfd); 661bc8a9dfSShuo Chen 671bc8a9dfSShuo Chen void toHostPort(char* buf, size_t size, 681bc8a9dfSShuo Chen const struct sockaddr_in& addr); 691bc8a9dfSShuo Chen void fromHostPort(const char* ip, uint16_t port, 701bc8a9dfSShuo Chen struct sockaddr_in* addr); 711bc8a9dfSShuo Chen 721bc8a9dfSShuo Chen struct sockaddr_in getLocalAddr(int sockfd); 731bc8a9dfSShuo Chen 741bc8a9dfSShuo Chen int getSocketError(int sockfd); 751bc8a9dfSShuo Chen 761bc8a9dfSShuo Chen } 771bc8a9dfSShuo Chen } 781bc8a9dfSShuo Chen 791bc8a9dfSShuo Chen #endif // MUDUO_NET_SOCKETSOPS_H 80