1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : clSocketBase.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "clSocketBase.h"
#include "file_logger.h"
#include <cerrno>
#include <cstdio>
#include <memory>
#include <sstream>
#ifndef _WIN32
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif
clSocketBase::clSocketBase(socket_t sockfd)
: m_socket(sockfd)
, m_closeOnExit(true)
{
}
clSocketBase::~clSocketBase() { DestroySocket(); }
void clSocketBase::Initialize()
{
#ifdef _WIN32
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
#endif
}
// Read API
int clSocketBase::Read(wxMemoryBuffer& content, long timeout)
{
content.Clear();
char buffer[4096];
timeout = (timeout * 1000); // convert to MS
while(true && timeout) {
int rc = SelectReadMS(10);
timeout -= 10;
if(rc == kSuccess) {
memset(buffer, 0x0, sizeof(buffer));
int bytesRead = recv(m_socket, buffer, sizeof(buffer), 0);
if(bytesRead < 0) {
const int err = GetLastError();
if(eWouldBlock != err) {
// Error
throw clSocketException("Read failed: " + error(err));
}
} else if(bytesRead == 0) {
// connection closed
return kError;
} else {
content.AppendData(buffer, bytesRead);
continue;
}
} else {
if(content.IsEmpty())
continue; // keep waiting until time ends
else
return kSuccess; // we already read our content
}
}
return kTimeout;
}
int clSocketBase::Read(wxString& content, const wxMBConv& conv, long timeout)
{
wxMemoryBuffer mb;
int rc = Read(mb, timeout);
if(rc == kSuccess) {
content = wxString((const char*)mb.GetData(), conv, mb.GetDataLen());
}
return rc;
}
int clSocketBase::Read(char* buffer, size_t bufferSize, size_t& bytesRead, long timeout)
{
if(SelectRead(timeout) == kTimeout) {
return kTimeout;
}
memset(buffer, 0, bufferSize);
const int res = ::recv(m_socket, buffer, bufferSize, 0);
if(res < 0) {
const int err = GetLastError();
if(eWouldBlock == err) {
return kTimeout;
}
throw clSocketException("Read failed: " + error(err));
} else if(0 == res) {
throw clSocketException("Read failed: " + error());
}
bytesRead = static_cast<size_t>(res);
return kSuccess;
}
int clSocketBase::SelectRead(long seconds)
{
if(seconds == -1) {
return kSuccess;
}
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
struct timeval tv = { seconds, 0 };
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(m_socket, &readfds);
int rc = select(m_socket + 1, &readfds, NULL, NULL, &tv);
if(rc == 0) {
// timeout
return kTimeout;
} else if(rc < 0) {
// an error occurred
throw clSocketException("SelectRead failed: " + error());
} else {
// we got something to read
return kSuccess;
}
}
// Send API
void clSocketBase::Send(const wxString& msg, const wxMBConv& conv)
{
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
wxCharBuffer cb = msg.mb_str(conv).data();
wxMemoryBuffer mb;
mb.AppendData(cb.data(), cb.length());
Send(mb);
}
void clSocketBase::Send(const std::string& msg)
{
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
wxMemoryBuffer mb;
mb.AppendData(msg.c_str(), msg.length());
Send(mb);
}
void clSocketBase::Send(const wxMemoryBuffer& msg)
{
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
char* pdata = (char*)msg.GetData();
int bytesLeft = msg.GetDataLen();
std::string str(pdata, bytesLeft);
LOG_IF_TRACE
{
clDEBUG1() << "Sending buffer:" << str << endl;
clDEBUG1() << "Message length:" << str.length() << endl;
}
while(bytesLeft) {
if(SelectWriteMS(100) == kTimeout) {
continue;
}
int bytesSent = ::send(m_socket, (const char*)pdata, bytesLeft, 0);
LOG_IF_TRACE { clDEBUG1() << "::send() completed. number of bytes sent:" << bytesSent << endl; }
if(bytesSent <= 0) {
throw clSocketException("Send error: " + error());
}
pdata += bytesSent;
bytesLeft -= bytesSent;
}
}
int clSocketBase::GetLastError()
{
#ifdef _WIN32
return ::WSAGetLastError();
#else
return errno;
#endif
}
std::string clSocketBase::error() { return error(GetLastError()); }
std::string clSocketBase::error(const int errorCode)
{
std::string err;
#ifdef _WIN32
// Get the error message, if any.
if(errorCode == 0)
return "No error message has been recorded";
LPSTR messageBuffer = nullptr;
size_t size =
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
// Free the buffer.
LocalFree(messageBuffer);
err = message;
#else
err = strerror(errorCode);
#endif
return err;
}
void clSocketBase::DestroySocket()
{
if(IsCloseOnExit()) {
if(m_socket != INVALID_SOCKET) {
#ifdef _WIN32
::shutdown(m_socket, 2);
::closesocket(m_socket);
#else
::shutdown(m_socket, 2);
::close(m_socket);
#endif
}
}
m_socket = INVALID_SOCKET;
}
int clSocketBase::ReadMessage(wxString& message, int timeout)
{
// send the length in string form to avoid binary / arch differences between remote and local machine
char msglen[11];
memset(msglen, 0, sizeof(msglen));
size_t message_len(0);
size_t bytesRead(0);
int rc = Read((char*)msglen, sizeof(msglen) - 1, bytesRead, timeout);
if(rc != kSuccess) {
// timeout
return rc;
}
// convert the string to int
message_len = ::atoi(msglen);
bytesRead = 0;
std::unique_ptr<char> pBuff(new char[message_len]);
// read the entire amount we need
int bytesLeft = message_len;
int totalRead = 0;
while(bytesLeft > 0) {
rc = Read(pBuff.get() + totalRead, bytesLeft, bytesRead, timeout);
if(rc != kSuccess) {
return rc;
} else {
bytesLeft -= bytesRead;
totalRead += bytesRead;
bytesRead = 0;
}
}
message.assign(pBuff.get(), message_len);
return kSuccess;
}
void clSocketBase::WriteMessage(const wxString& message)
{
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
// Write the message length
std::string c_str = message.mb_str(wxConvUTF8).data();
int len = c_str.length();
// send the length in string form to avoid binary / arch differences between remote and local machine
char msglen[11];
memset(msglen, 0, sizeof(msglen));
sprintf(msglen, "%010d", len);
// send it without the NULL byte
if(::send(m_socket, msglen, sizeof(msglen) - 1, 0) < 0) {
throw clSocketException("Send error: " + error(errno));
}
// now send the actual data
Send(c_str);
}
socket_t clSocketBase::Release()
{
int fd = m_socket;
m_socket = INVALID_SOCKET;
return fd;
}
void clSocketBase::MakeSocketBlocking(bool blocking)
{
#ifndef __WXMSW__
// set socket to non-blocking mode
int flags;
flags = ::fcntl(m_socket, F_GETFL);
if(blocking) {
flags &= ~O_NONBLOCK;
} else {
flags |= O_NONBLOCK;
}
::fcntl(m_socket, F_SETFL, flags);
#else
u_long iMode = blocking ? 0 : 1;
::ioctlsocket(m_socket, FIONBIO, &iMode);
#endif
}
int clSocketBase::SelectWriteMS(long milliSeconds)
{
if(milliSeconds == -1) {
return kSuccess;
}
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
struct timeval tv;
tv.tv_sec = milliSeconds / 1000;
tv.tv_usec = (milliSeconds % 1000) * 1000;
fd_set write_set;
FD_ZERO(&write_set);
FD_SET(m_socket, &write_set);
errno = 0;
int rc = select(m_socket + 1, NULL, &write_set, NULL, &tv);
if(rc == 0) {
// timeout
return kTimeout;
} else if(rc < 0) {
// an error occurred
throw clSocketException("SelectWriteMS failed: " + error());
} else {
// we got something to read
return kSuccess;
}
}
int clSocketBase::SelectWrite(long seconds)
{
if(seconds == -1) {
return kSuccess;
}
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
struct timeval tv = { seconds, 0 };
fd_set write_set;
FD_ZERO(&write_set);
FD_SET(m_socket, &write_set);
errno = 0;
int rc = select(m_socket + 1, NULL, &write_set, NULL, &tv);
if(rc == 0) {
// timeout
return kTimeout;
} else if(rc < 0) {
// an error occurred
throw clSocketException("SelectRead failed: " + error());
} else {
// we got something to read
return kSuccess;
}
}
int clSocketBase::SelectReadMS(long milliSeconds)
{
if(milliSeconds == -1) {
return kSuccess;
}
if(m_socket == INVALID_SOCKET) {
throw clSocketException("Invalid socket!");
}
int seconds = milliSeconds / 1000; // convert the number into seconds
int ms = milliSeconds % 1000; // the remainder is less than a second
struct timeval tv = { seconds, ms * 1000 };
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(m_socket, &readfds);
int rc = select(m_socket + 1, &readfds, NULL, NULL, &tv);
if(rc == 0) {
// timeout
return kTimeout;
} else if(rc < 0) {
// an error occurred
throw clSocketException("SelectRead failed: " + error());
} else {
// we got something to read
return kSuccess;
}
}
void clSocketBase::SetSocket(socket_t socket)
{
SetCloseOnExit(false);
m_socket = socket;
}
|