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