SocketsOps.h revision 9a1e991d
19a1e991dSShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 29a1e991dSShuo Chen// http://code.google.com/p/muduo/ 39a1e991dSShuo Chen// 49a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license 59a1e991dSShuo Chen// that can be found in the License file. 69a1e991dSShuo Chen 79a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 89a1e991dSShuo Chen// 99a1e991dSShuo Chen// This is an internal header file, you should not include this. 109a1e991dSShuo Chen 119a1e991dSShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H 129a1e991dSShuo Chen#define MUDUO_NET_SOCKETSOPS_H 139a1e991dSShuo Chen 149a1e991dSShuo Chen#include <arpa/inet.h> 159a1e991dSShuo Chen#include <endian.h> 169a1e991dSShuo Chen 179a1e991dSShuo Chennamespace muduo 189a1e991dSShuo Chen{ 199a1e991dSShuo Chennamespace sockets 209a1e991dSShuo Chen{ 219a1e991dSShuo Chen 229a1e991dSShuo Chen// the inline assembler code makes type blur, 239a1e991dSShuo Chen// so we disable warnings for a while. 249a1e991dSShuo Chen#pragma GCC diagnostic ignored "-Wconversion" 259a1e991dSShuo Cheninline uint64_t hostToNetwork64(uint64_t host64) 269a1e991dSShuo Chen{ 279a1e991dSShuo Chen return htobe64(host64); 289a1e991dSShuo Chen} 299a1e991dSShuo Chen 309a1e991dSShuo Cheninline uint32_t hostToNetwork32(uint32_t host32) 319a1e991dSShuo Chen{ 329a1e991dSShuo Chen return htonl(host32); 339a1e991dSShuo Chen} 349a1e991dSShuo Chen 359a1e991dSShuo Cheninline uint16_t hostToNetwork16(uint16_t host16) 369a1e991dSShuo Chen{ 379a1e991dSShuo Chen return htons(host16); 389a1e991dSShuo Chen} 399a1e991dSShuo Chen 409a1e991dSShuo Cheninline uint64_t networkToHost64(uint64_t net64) 419a1e991dSShuo Chen{ 429a1e991dSShuo Chen return be64toh(net64); 439a1e991dSShuo Chen} 449a1e991dSShuo Chen 459a1e991dSShuo Cheninline uint32_t networkToHost32(uint32_t net32) 469a1e991dSShuo Chen{ 479a1e991dSShuo Chen return ntohl(net32); 489a1e991dSShuo Chen} 499a1e991dSShuo Chen 509a1e991dSShuo Cheninline uint16_t networkToHost16(uint16_t net16) 519a1e991dSShuo Chen{ 529a1e991dSShuo Chen return ntohs(net16); 539a1e991dSShuo Chen} 549a1e991dSShuo Chen#pragma GCC diagnostic error "-Wconversion" 559a1e991dSShuo Chen 569a1e991dSShuo Chen/// 579a1e991dSShuo Chen/// Creates a non-blocking socket file descriptor, 589a1e991dSShuo Chen/// abort if any error. 599a1e991dSShuo Chenint createNonblockingOrDie(); 609a1e991dSShuo Chen 619a1e991dSShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr); 629a1e991dSShuo Chenvoid listenOrDie(int sockfd); 639a1e991dSShuo Chenint accept(int sockfd, struct sockaddr_in* addr); 649a1e991dSShuo Chenvoid close(int sockfd); 659a1e991dSShuo Chen 669a1e991dSShuo Chenvoid toHostPort(char* buf, size_t size, 679a1e991dSShuo Chen const struct sockaddr_in& addr); 689a1e991dSShuo Chenvoid fromHostPort(const char* ip, uint16_t port, 699a1e991dSShuo Chen struct sockaddr_in* addr); 709a1e991dSShuo Chen} 719a1e991dSShuo Chen} 729a1e991dSShuo Chen 739a1e991dSShuo Chen#endif // MUDUO_NET_SOCKETSOPS_H 74