| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include <Socket.h> |
|---|
| 23 | #include <NSCHelper.h> |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Print an error message |
|---|
| 27 | * @param error |
|---|
| 28 | */ |
|---|
| 29 | void simpleSocket::Socket::printError(std::string FILE, int LINE, std::string error) { |
|---|
| 30 | NSCModuleHelper::Message(NSCAPI::error, FILE, LINE, error); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Read all data on the socket |
|---|
| 36 | * @param buffer |
|---|
| 37 | * @param tmpBufferLength Length of temporary buffer to use (generally larger then expected data) |
|---|
| 38 | */ |
|---|
| 39 | bool simpleSocket::Socket::readAll(DataBuffer& buffer, unsigned int tmpBufferLength /* = 1024*/) { |
|---|
| 40 | // @todo this could be optimized a bit if we want to |
|---|
| 41 | // If only one buffer is needed we could "reuse" the buffer instead of copying it. |
|---|
| 42 | char *tmpBuffer = new char[tmpBufferLength+1]; |
|---|
| 43 | int n=recv(socket_,tmpBuffer,tmpBufferLength,0); |
|---|
| 44 | while ((n!=SOCKET_ERROR )&&(n!=0)) { |
|---|
| 45 | if (n == tmpBufferLength) { |
|---|
| 46 | // We filled the buffer (There is more to get) |
|---|
| 47 | buffer.append(tmpBuffer, n); |
|---|
| 48 | n=recv(socket_,tmpBuffer,tmpBufferLength,0); |
|---|
| 49 | |
|---|
| 50 | } else { |
|---|
| 51 | // Buffer not full, we got it "all" |
|---|
| 52 | buffer.append(tmpBuffer, n); |
|---|
| 53 | break; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | delete [] tmpBuffer; |
|---|
| 57 | return n!=SOCKET_ERROR; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Startup WSA |
|---|
| 63 | * @param wVersionRequested Version to use |
|---|
| 64 | * @return stuff |
|---|
| 65 | */ |
|---|
| 66 | WSADATA simpleSocket::WSAStartup(WORD wVersionRequested /* = 0x202 */) { |
|---|
| 67 | WSADATA wsaData; |
|---|
| 68 | int wsaret=::WSAStartup(wVersionRequested,&wsaData); |
|---|
| 69 | if(wsaret != 0) |
|---|
| 70 | throw SocketException("WSAStartup failed: " + strEx::itos(wsaret)); |
|---|
| 71 | return wsaData; |
|---|
| 72 | } |
|---|
| 73 | /** |
|---|
| 74 | * Cleanup (Shutdown) WSA |
|---|
| 75 | */ |
|---|
| 76 | void simpleSocket::WSACleanup() { |
|---|
| 77 | if (::WSACleanup() != 0) |
|---|
| 78 | throw SocketException("WSACleanup failed: ", ::WSAGetLastError()); |
|---|
| 79 | } |
|---|