Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1 | /* <auto_header> |
| 2 | * <FILENAME> |
| 3 | * |
| 4 | * INTEL CONFIDENTIAL |
| 5 | * Copyright © 2011 Intel |
| 6 | * Corporation All Rights Reserved. |
| 7 | * |
| 8 | * The source code contained or described herein and all documents related to |
| 9 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 10 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 11 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 12 | * and confidential information of Intel or its suppliers and licensors. The |
| 13 | * Material is protected by worldwide copyright and trade secret laws and |
| 14 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 15 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 16 | * disclosed in any way without Intel’s prior express written permission. |
| 17 | * |
| 18 | * No license under any patent, copyright, trade secret or other intellectual |
| 19 | * property right is granted to or conferred upon you by disclosure or delivery |
| 20 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 21 | * otherwise. Any license under such intellectual property rights must be |
| 22 | * express and approved by Intel in writing. |
| 23 | * |
| 24 | * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com) |
| 25 | * CREATED: 2011-06-01 |
| 26 | * UPDATED: 2011-07-27 |
| 27 | * |
| 28 | * |
| 29 | * </auto_header> |
| 30 | */ |
| 31 | #include "Socket.h" |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <unistd.h> |
| 35 | #include <assert.h> |
| 36 | #include <netdb.h> |
| 37 | #include <strings.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <sys/time.h> |
| 41 | |
| 42 | CSocket::CSocket() : _iSockFd(socket(AF_INET, SOCK_STREAM, 0)) |
| 43 | { |
| 44 | assert(_iSockFd != -1); |
| 45 | } |
| 46 | |
| 47 | CSocket::CSocket(int iSockId) : _iSockFd(iSockId) |
| 48 | { |
| 49 | assert(_iSockFd != -1); |
| 50 | } |
| 51 | |
| 52 | CSocket::~CSocket() |
| 53 | { |
| 54 | close(_iSockFd); |
| 55 | } |
| 56 | |
| 57 | // Socket address init |
| 58 | void CSocket::initSockAddrIn(struct sockaddr_in* pSockAddrIn, uint32_t uiInAddr, uint16_t uiPort) const |
| 59 | { |
| 60 | // Fill server address |
| 61 | pSockAddrIn->sin_family = AF_INET; |
| 62 | pSockAddrIn->sin_port = htons(uiPort); |
| 63 | pSockAddrIn->sin_addr.s_addr = uiInAddr; |
| 64 | bzero(&pSockAddrIn->sin_zero, sizeof(pSockAddrIn->sin_zero)); |
| 65 | } |
| 66 | |
| 67 | // Non blocking state |
| 68 | void CSocket::setNonBlocking(bool bNonBlocking) |
| 69 | { |
| 70 | int iFlags = fcntl(_iSockFd, F_GETFL, 0); |
| 71 | |
| 72 | assert(iFlags != -1); |
| 73 | |
| 74 | if (bNonBlocking) { |
| 75 | |
| 76 | iFlags |= O_NONBLOCK; |
| 77 | } else { |
| 78 | |
| 79 | iFlags &= ~O_NONBLOCK; |
| 80 | } |
| 81 | fcntl(_iSockFd, F_SETFL, iFlags); |
| 82 | } |
| 83 | |
| 84 | // Communication timeout |
| 85 | void CSocket::setTimeout(uint32_t uiMilliseconds) |
| 86 | { |
| 87 | struct timeval tv; |
| 88 | tv.tv_sec = uiMilliseconds / 1000; |
| 89 | tv.tv_usec = (uiMilliseconds % 1000) * 1000; |
| 90 | |
| 91 | setsockopt(_iSockFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 92 | setsockopt(_iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 93 | } |
| 94 | |
| 95 | // Read |
| 96 | bool CSocket::read(void* pvData, uint32_t uiSize) |
| 97 | { |
| 98 | uint32_t uiOffset = 0; |
| 99 | uint8_t* pucData = (uint8_t*)pvData; |
| 100 | |
| 101 | while (uiSize) { |
| 102 | |
| 103 | int32_t iAccessedSize = ::recv(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL); |
| 104 | |
| 105 | if (!iAccessedSize || iAccessedSize == -1) { |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | uiSize -= iAccessedSize; |
| 110 | uiOffset += iAccessedSize; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // Write |
| 116 | bool CSocket::write(const void* pvData, uint32_t uiSize) |
| 117 | { |
| 118 | uint32_t uiOffset = 0; |
| 119 | const uint8_t* pucData = (const uint8_t*)pvData; |
| 120 | |
| 121 | while (uiSize) { |
| 122 | |
| 123 | int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL); |
| 124 | |
| 125 | if (!iAccessedSize || iAccessedSize == -1) { |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | uiSize -= iAccessedSize; |
| 130 | uiOffset += iAccessedSize; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // Fd |
| 136 | int CSocket::getFd() const |
| 137 | { |
| 138 | return _iSockFd; |
| 139 | } |