00001 #ifndef KOLSOCKET_H_INCLUDED
00002 #define KOLSOCKET_H_INCLUDED
00003
00004 #include <exception>
00005 #include <string>
00006
00007 #ifdef WIN32
00008 #include <winsock2.h>
00009 #include <ws2tcpip.h>
00010 #else
00011 #include <unistd.h>
00012 #include <netinet/in.h>
00013 #include <sys/types.h>
00014 #include <sys/socket.h>
00015 #include <sys/ioctl.h>
00016 #endif
00017
00018 #ifndef SHUT_RD
00019 #define SHUT_RD 0
00020 #define SHUT_WR 1
00021 #define SHUT_RDWR 2
00022 #endif
00023
00024 namespace kekonline
00025 {
00026 #ifdef WIN32
00027 typedef SOCKET socket_t;
00028 typedef char sockmsg_t;
00029 typedef unsigned long in_addr_t;
00030 #else
00031 typedef int socket_t;
00032 typedef void sockmsg_t;
00033 #endif
00034
00036 class SocketException : public std::exception
00037 {
00038 public:
00039 explicit SocketException(const std::string& msg);
00040 virtual ~SocketException() throw();
00041 virtual const char* what() const throw();
00042 int reason() const throw();
00043
00044 private:
00045 int m_reason;
00046 std::string m_msg;
00047 };
00048
00050 class SocketLibrary
00051 {
00052 public:
00053 SocketLibrary();
00054 virtual ~SocketLibrary();
00055 bool isloaded() const throw();
00056
00057 private:
00058 bool m_libloaded;
00059 };
00060
00061
00063
00075 class Socket
00076 {
00077 public:
00078 #ifdef WIN32
00079 static const socket_t invalid = INVALID_SOCKET;
00080 static const int sockerr = SOCKET_ERROR;
00081 #else
00082 static const socket_t invalid = -1;
00083 static const int sockerr = -1;
00084 #endif
00085
00086 private:
00087 static int CloseSocket(socket_t s) throw();
00088 static socket_t DuplicateSocket(socket_t s) throw();
00089 static int IoctlSocket(socket_t s, int request, void* argp) throw();
00090
00091 public:
00092 Socket();
00093 Socket(const Socket& from);
00094 Socket(int domain, int type, int protocol=0);
00095 virtual ~Socket();
00096 Socket& operator=(const Socket& from);
00097 int close();
00098 int create(int domain, int type, int protocol=0);
00099 int connect(const struct sockaddr* addr, socklen_t len);
00100 int bind(const struct sockaddr* addr, socklen_t len);
00101 int listen(int backlog);
00102 Socket accept(struct sockaddr* addr=0, socklen_t* addrlen=0);
00103 int send(const void* buf, size_t len, int flags=0);
00104 int recv(void* buf, size_t len, int flags=0);
00105 int sendto(const void* buf, size_t len, int flags, const struct sockaddr* to, socklen_t tolen);
00106 int recvfrom(void* buf, size_t len, int flags, struct sockaddr* from, socklen_t* fromlen);
00107 int shutdown(int how=SHUT_RDWR);
00108 int getsockname(struct sockaddr* name, socklen_t* namelen) const;
00109 int getpeername(struct sockaddr* name, socklen_t* namelen) const;
00110 int getsockopt(int level, int optname, void* optval, socklen_t* optlen) const;
00111 int setsockopt(int level, int optname, const void* optval, socklen_t optlen);
00112 int ioctl(int request, void* argp);
00113
00114 protected:
00115 Socket(socket_t sd);
00116
00117 protected:
00118 socket_t m_sd;
00119 };
00120 }
00121
00122 #endif
00123