diff options
author | pixel <pixel> | 2006-01-31 17:02:38 +0000 |
---|---|---|
committer | pixel <pixel> | 2006-01-31 17:02:38 +0000 |
commit | 6daac4eb3604736e20e8af5733f698eb144f0c2a (patch) | |
tree | 7a684f120b20e0aae0ecbedcaad480cdae581200 /lib/Socket.cc | |
parent | 6634b5d65d21b826830d7601178453a0a3084e8d (diff) |
Way too much changes - all over.
Diffstat (limited to 'lib/Socket.cc')
-rw-r--r-- | lib/Socket.cc | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/lib/Socket.cc b/lib/Socket.cc index f3e28df..5a6cede 100644 --- a/lib/Socket.cc +++ b/lib/Socket.cc @@ -1,11 +1,16 @@ +#ifdef _WIN32 +#include <winsock2.h> +#endif + #define _BSD_SOCKLEN_T_ int +#ifndef _WIN32 #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> -#include <errno.h> -#include <string.h> #include <strings.h> +#endif +#include <string.h> #include <errno.h> #ifdef HAVE_CONFIG_H #include "config.h" @@ -17,6 +22,19 @@ #include "Output.h" #include "gettext.h" +#ifdef _WIN32 +class dummy_win32_socket_class_t : public Base { + public: + dummy_win32_socket_class_t() throw (GeneralException) { + WSADATA wsaData; + + if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) + throw GeneralException("WSAStartup() failed."); + } +} dummy_win32_socket_class; +typedef int socklen_t; +#endif + Socket::Socket() throw (GeneralException) : Handle(socket(AF_INET, SOCK_STREAM, 0)), connected(false), listening(false), writeclosed(false), readclosed(false) { // cerr << "Socket(): connected = " << connected << "; readclosed = " << readclosed << "; writeclosed = " << writeclosed << endl; if (GetHandle() < 0) { @@ -161,3 +179,34 @@ int Socket::GetPort() { return r; } + +ssize_t Socket::nwrite(const void * buf, size_t count) throw (GeneralException) { + return ::send(GetHandle(), (const char *) buf, count, 0); +} + +ssize_t Socket::nread(void * buf, size_t count) throw (GeneralException) { + return ::recv(GetHandle(), (char *) buf, count, 0); +} + +int Socket::ndup() const throw (GeneralException) { +#ifdef _WIN32 + WSAPROTOCOL_INFO ProtocolInfo; + int r, s; + + if (r = WSADuplicateSocket(GetHandle(), GetCurrentProcessId(), &ProtocolInfo)) { + throw GeneralException("Error within WSADuplicateSocket()."); + } + + if ((s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ProtocolInfo, 0, 0)) == INVALID_SOCKET) { + throw GeneralException("Error within WSASocket() while dup()ing it."); + } + + return s; +#else + return dup(GetHandle()); +#endif +} + +int Socket::nclose() throw (GeneralException) { + return closesocket(GetHandle()); +} |