Menu

[2c7fdc]: / include / Socket.cpp  Maximize  Restore  History

Download this file

99 lines (91 with data), 3.7 kB

 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
/**************************************************************************
* Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
* *
* This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
* *
* 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <Socket.h>
#include <NSCHelper.h>
/**
* Print an error message
* @param error
*/
void simpleSocket::Socket::printError(std::wstring FILE, int LINE, std::wstring error) {
NSCModuleHelper::Message(NSCAPI::error, FILE, LINE, error);
}
/**
* Read all data on the socket
* @param buffer
* @param tmpBufferLength Length of temporary buffer to use (generally larger then expected data)
*/
bool simpleSocket::Socket::readAll(DataBuffer& buffer, unsigned int tmpBufferLength /* = 1024*/, int maxLength /* = -1 */) {
// @todo this could be optimized a bit if we want to
// If only one buffer is needed we could "reuse" the buffer instead of copying it.
char *tmpBuffer = new char[tmpBufferLength+1];
int n=recv(socket_,tmpBuffer,tmpBufferLength,0);
//std::wcout << _T("read: ") << n << std::endl;
while ((n!=SOCKET_ERROR )&&(n!=0)) {
if (n == tmpBufferLength) {
// We filled the buffer (There is more to get)
buffer.append(tmpBuffer, n);
if ((maxLength!=-1)&&(n >= maxLength))
break;
n=recv(socket_,tmpBuffer,tmpBufferLength,0);
} else {
// Buffer not full, we got it "all"
buffer.append(tmpBuffer, n);
break;
}
}
delete [] tmpBuffer;
if (n == SOCKET_ERROR) {
int ret = ::WSAGetLastError();
if (ret == WSAEWOULDBLOCK)
return true;
throw SocketException(_T("recv returned SOCKET_ERROR: "), ret);
}
return n>0;
}
bool simpleSocket::Socket::sendAll(const char * buffer, unsigned int len) {
int n = send(buffer, len, 0);
if (n == SOCKET_ERROR) {
int ret = ::WSAGetLastError();
if (ret == WSAEWOULDBLOCK)
return false;
throw SocketException(_T("recv returned SOCKET_ERROR: "), ret);
}
return false;
}
/**
* Startup WSA
* @param wVersionRequested Version to use
* @return stuff
*/
WSADATA simpleSocket::WSAStartup(WORD wVersionRequested /* = 0x202 */) {
WSADATA wsaData;
int wsaret=::WSAStartup(wVersionRequested,&wsaData);
if(wsaret != 0)
throw SocketException(_T("WSAStartup failed: ") + strEx::itos(wsaret));
return wsaData;
}
/**
* Cleanup (Shutdown) WSA
*/
void simpleSocket::WSACleanup() {
if (::WSACleanup() != 0)
throw SocketException(_T("WSACleanup failed: "), ::WSAGetLastError());
}