Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 2 | * Copyright (c) 2011-2014, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | * list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation and/or |
| 13 | * other materials provided with the distribution. |
| 14 | * |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software without |
| 17 | * specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "Socket.h" |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <unistd.h> |
| 34 | #include <assert.h> |
| 35 | #include <netdb.h> |
| 36 | #include <strings.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <netinet/in.h> |
David Wagner | 3e783c2 | 2013-12-05 19:29:33 +0100 | [diff] [blame] | 39 | #include <netinet/tcp.h> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 40 | #include <sys/time.h> |
| 41 | |
| 42 | CSocket::CSocket() : _iSockFd(socket(AF_INET, SOCK_STREAM, 0)) |
| 43 | { |
| 44 | assert(_iSockFd != -1); |
David Wagner | 3e783c2 | 2013-12-05 19:29:33 +0100 | [diff] [blame] | 45 | |
| 46 | int iNoDelay = 1; |
| 47 | // (see man 7 tcp) |
| 48 | // Setting TCP_NODELAY allows us sending commands and responses as soon as |
| 49 | // they are ready to be sent, instead of waiting for more data on the |
| 50 | // socket. |
| 51 | setsockopt(_iSockFd, IPPROTO_TCP, TCP_NODELAY, (char *)&iNoDelay, sizeof(iNoDelay)); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | CSocket::CSocket(int iSockId) : _iSockFd(iSockId) |
| 55 | { |
| 56 | assert(_iSockFd != -1); |
| 57 | } |
| 58 | |
| 59 | CSocket::~CSocket() |
| 60 | { |
| 61 | close(_iSockFd); |
| 62 | } |
| 63 | |
| 64 | // Socket address init |
| 65 | void CSocket::initSockAddrIn(struct sockaddr_in* pSockAddrIn, uint32_t uiInAddr, uint16_t uiPort) const |
| 66 | { |
| 67 | // Fill server address |
| 68 | pSockAddrIn->sin_family = AF_INET; |
| 69 | pSockAddrIn->sin_port = htons(uiPort); |
| 70 | pSockAddrIn->sin_addr.s_addr = uiInAddr; |
| 71 | bzero(&pSockAddrIn->sin_zero, sizeof(pSockAddrIn->sin_zero)); |
| 72 | } |
| 73 | |
| 74 | // Non blocking state |
| 75 | void CSocket::setNonBlocking(bool bNonBlocking) |
| 76 | { |
| 77 | int iFlags = fcntl(_iSockFd, F_GETFL, 0); |
| 78 | |
| 79 | assert(iFlags != -1); |
| 80 | |
| 81 | if (bNonBlocking) { |
| 82 | |
| 83 | iFlags |= O_NONBLOCK; |
| 84 | } else { |
| 85 | |
| 86 | iFlags &= ~O_NONBLOCK; |
| 87 | } |
| 88 | fcntl(_iSockFd, F_SETFL, iFlags); |
| 89 | } |
| 90 | |
| 91 | // Communication timeout |
| 92 | void CSocket::setTimeout(uint32_t uiMilliseconds) |
| 93 | { |
| 94 | struct timeval tv; |
| 95 | tv.tv_sec = uiMilliseconds / 1000; |
| 96 | tv.tv_usec = (uiMilliseconds % 1000) * 1000; |
| 97 | |
| 98 | setsockopt(_iSockFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 99 | setsockopt(_iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 100 | } |
| 101 | |
| 102 | // Read |
| 103 | bool CSocket::read(void* pvData, uint32_t uiSize) |
| 104 | { |
| 105 | uint32_t uiOffset = 0; |
| 106 | uint8_t* pucData = (uint8_t*)pvData; |
| 107 | |
| 108 | while (uiSize) { |
| 109 | |
| 110 | int32_t iAccessedSize = ::recv(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL); |
| 111 | |
| 112 | if (!iAccessedSize || iAccessedSize == -1) { |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | uiSize -= iAccessedSize; |
| 117 | uiOffset += iAccessedSize; |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // Write |
| 123 | bool CSocket::write(const void* pvData, uint32_t uiSize) |
| 124 | { |
| 125 | uint32_t uiOffset = 0; |
| 126 | const uint8_t* pucData = (const uint8_t*)pvData; |
| 127 | |
| 128 | while (uiSize) { |
| 129 | |
| 130 | int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL); |
| 131 | |
| 132 | if (!iAccessedSize || iAccessedSize == -1) { |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | uiSize -= iAccessedSize; |
| 137 | uiOffset += iAccessedSize; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | // Fd |
| 143 | int CSocket::getFd() const |
| 144 | { |
| 145 | return _iSockFd; |
| 146 | } |