s06-s05-SocketsOps.h.diff revision 048f6023
1048f6023SShuo Chen // Copyright 2010, Shuo Chen. All rights reserved. 2048f6023SShuo Chen // http://code.google.com/p/muduo/ 3048f6023SShuo Chen // 4048f6023SShuo Chen // Use of this source code is governed by a BSD-style license 5048f6023SShuo Chen // that can be found in the License file. 6048f6023SShuo Chen 7048f6023SShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 8048f6023SShuo Chen // 9048f6023SShuo Chen // This is an internal header file, you should not include this. 10048f6023SShuo Chen 11048f6023SShuo Chen #ifndef MUDUO_NET_SOCKETSOPS_H 12048f6023SShuo Chen #define MUDUO_NET_SOCKETSOPS_H 13048f6023SShuo Chen 14048f6023SShuo Chen #include <arpa/inet.h> 15048f6023SShuo Chen #include <endian.h> 16048f6023SShuo Chen 17048f6023SShuo Chen namespace muduo 18048f6023SShuo Chen { 19048f6023SShuo Chen namespace sockets 20048f6023SShuo Chen { 21048f6023SShuo Chen 22048f6023SShuo Chen // the inline assembler code makes type blur, 23048f6023SShuo Chen // so we disable warnings for a while. 24048f6023SShuo Chen #pragma GCC diagnostic ignored "-Wconversion" 25048f6023SShuo Chen inline uint64_t hostToNetwork64(uint64_t host64) 26048f6023SShuo Chen { 27048f6023SShuo Chen return htobe64(host64); 28048f6023SShuo Chen } 29048f6023SShuo Chen 30048f6023SShuo Chen inline uint32_t hostToNetwork32(uint32_t host32) 31048f6023SShuo Chen { 32048f6023SShuo Chen return htonl(host32); 33048f6023SShuo Chen } 34048f6023SShuo Chen 35048f6023SShuo Chen inline uint16_t hostToNetwork16(uint16_t host16) 36048f6023SShuo Chen { 37048f6023SShuo Chen return htons(host16); 38048f6023SShuo Chen } 39048f6023SShuo Chen 40048f6023SShuo Chen inline uint64_t networkToHost64(uint64_t net64) 41048f6023SShuo Chen { 42048f6023SShuo Chen return be64toh(net64); 43048f6023SShuo Chen } 44048f6023SShuo Chen 45048f6023SShuo Chen inline uint32_t networkToHost32(uint32_t net32) 46048f6023SShuo Chen { 47048f6023SShuo Chen return ntohl(net32); 48048f6023SShuo Chen } 49048f6023SShuo Chen 50048f6023SShuo Chen inline uint16_t networkToHost16(uint16_t net16) 51048f6023SShuo Chen { 52048f6023SShuo Chen return ntohs(net16); 53048f6023SShuo Chen } 54048f6023SShuo Chen #pragma GCC diagnostic error "-Wconversion" 55048f6023SShuo Chen 56048f6023SShuo Chen /// 57048f6023SShuo Chen /// Creates a non-blocking socket file descriptor, 58048f6023SShuo Chen /// abort if any error. 59048f6023SShuo Chen int createNonblockingOrDie(); 60048f6023SShuo Chen 61048f6023SShuo Chen void bindOrDie(int sockfd, const struct sockaddr_in& addr); 62048f6023SShuo Chen void listenOrDie(int sockfd); 63048f6023SShuo Chen int accept(int sockfd, struct sockaddr_in* addr); 64048f6023SShuo Chen void close(int sockfd); 65048f6023SShuo Chen 66048f6023SShuo Chen void toHostPort(char* buf, size_t size, 67048f6023SShuo Chen const struct sockaddr_in& addr); 68048f6023SShuo Chen void fromHostPort(const char* ip, uint16_t port, 69048f6023SShuo Chen struct sockaddr_in* addr); 70048f6023SShuo Chen 71048f6023SShuo Chen struct sockaddr_in getLocalAddr(int sockfd); 72048f6023SShuo Chen 73048f6023SShuo Chen+int getSocketError(int sockfd); 74048f6023SShuo Chen+ 75048f6023SShuo Chen } 76048f6023SShuo Chen } 77048f6023SShuo Chen 78048f6023SShuo Chen #endif // MUDUO_NET_SOCKETSOPS_H 79