15 #if PLATFORM_IS_LINUX || PLATFORM_IS_APPLE 18 #include <sys/socket.h> 19 #include <sys/ioctl.h> 20 #include <arpa/inet.h> 31 #if PLATFORM_IS_WINDOWS 35 static uint32_t s_socketInitialized;
36 static mutex s_socketFrameworkMutex;
43 #if PLATFORM_IS_WINDOWS 45 lock_guard<mutex> locker(s_socketFrameworkMutex);
46 if (++s_socketInitialized != 1)
51 WSAStartup(MAKEWORD(2, 2), &wsa_data);
60 #if PLATFORM_IS_WINDOWS 62 lock_guard<mutex> locker(s_socketFrameworkMutex);
63 if (s_socketInitialized == 0)
67 else if (--s_socketInitialized == 0)
78 struct timeval tv = { timeoutMilliseconds / 1000, (timeoutMilliseconds % 1000) * 1000 };
82 int numberOfSocketsThatCanWrite = select((
int)(socket + 1),
NULL, &ws,
NULL, &tv);
83 return (numberOfSocketsThatCanWrite > 0);
88 struct timeval tv = { timeoutMilliseconds / 1000, (timeoutMilliseconds % 1000) * 1000 };
92 int numberOfSocketsThatCanRead = select((
int)(socket + 1), &rs,
NULL,
NULL, &tv);
93 return (numberOfSocketsThatCanRead > 0);
98 int totalWriteCount = 0;
101 while (totalWriteCount < dataLength)
107 writeCount =
send(socket, (
const char*)data, dataLength, 0);
112 totalWriteCount += writeCount;
115 return totalWriteCount;
120 int count =
recv(socket, (
char*)data, dataLength, 0);
124 #if PLATFORM_IS_WINDOWS 126 DWORD err = GetLastError();
127 if (err == SO_RCVTIMEO || err == WSAETIMEDOUT || err == WSAEWOULDBLOCK)
148 #if PLATFORM_IS_WINDOWS 150 u_long blockingInt = (blocking ? 0 : 1);
151 return ioctlsocket(socket, FIONBIO, &blockingInt);
155 int opts = fcntl(socket, F_GETFL);
160 opts = (blocking ? opts & (~O_NONBLOCK) : opts | O_NONBLOCK);
161 return fcntl(socket, F_SETFL, opts);
170 #if PLATFORM_IS_WINDOWS 172 DWORD timeout = timeoutMilliseconds;
178 tv.tv_sec = timeoutMilliseconds / 1000;
179 tv.tv_usec = (timeoutMilliseconds % 1000) * 1000;
192 #if PLATFORM_IS_WINDOWS 197 status = closesocket(socket);
202 status =
shutdown(socket, SHUT_RDWR);
205 status =
close(socket);
239 addrinfo* result =
NULL;
240 addrinfo hints = addrinfo();
243 hints.ai_protocol = IPPROTO_TCP;
246 status = getaddrinfo(
m_host.c_str(), portString, &hints, &result);
255 m_socket =
socket(result->ai_family, result->ai_socktype, result->ai_protocol);
259 freeaddrinfo(result);
274 freeaddrinfo(result);
310 void cISTcpClient::HttpGet(
const string& subUrl,
const string& userAgent,
const string& userName,
const string& password)
312 string msg =
"GET /" + subUrl +
" HTTP/1.1\r\n";
313 msg +=
"User-Agent: " + userAgent +
"\r\n";
314 if (userName.size() != 0 && password.size() != 0)
316 string auth = userName +
":" + password;
317 msg +=
"Authorization: Basic " +
base64Encode((
const unsigned char*)auth.data(), (int)auth.size()) +
"\r\n";
319 msg +=
"Accept: */*\r\nConnection: close\r\n\r\n";
320 Write((uint8_t*)msg.data(), (int)msg.size());
NMI_API sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
int ISSocketClose(socket_t &socket)
int ISSocketCanWrite(socket_t socket, int timeoutMilliseconds)
NMI_API sint16 recv(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)
int ISSocketRead(socket_t socket, uint8_t *data, int dataLength)
size_t count(InputIterator first, InputIterator last, T const &item)
int Read(void *data, int dataLength) OVERRIDE
int ISSocketCanRead(socket_t socket, int timeoutMilliseconds)
void ISSocketFrameworkShutdown()
NMI_API sint8 setsockopt(SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value, uint16 u16OptionLen)
int SetBlocking(bool blocking)
int Open(const string &host, int port, int timeoutMilliseconds=IS_SOCKET_DEFAULT_TIMEOUT_MS)
string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len)
int ISSocketSetBlocking(socket_t socket, bool blocking)
USBInterfaceDescriptor data
void ISSocketFrameworkInitialize()
NMI_API sint8 close(SOCKET sock)
ROSCONSOLE_DECL void shutdown()
int ISSocketWrite(socket_t socket, const uint8_t *data, int dataLength)
NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags)
void HttpGet(const string &subUrl, const string &userAgent, const string &userName, const string &password)
int Write(const void *data, int dataLength) OVERRIDE
int ISSocketSetReadTimeout(socket_t socket, int timeoutMilliseconds)