00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SOCKET_H
00020 #define SOCKET_H
00021
00022 #ifdef _WIN32
00023 #include <windows.h>
00024 #define EINPROGRESS WSAEINPROGRESS
00025 #define socket_t int
00026 #define socklen_t int
00027 #define sockaddr SOCKADDR
00028 #else
00029 #include <fcntl.h>
00030 #include <netdb.h>
00031 #include <sys/types.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <errno.h>
00035 #include <arpa/inet.h>
00036 #define socket_t int
00037 #endif
00038 #ifdef HAVE_SSL
00039 #include <openssl/ssl.h>
00040 #endif
00041 #include <Locking.h>
00042 #include <Errors.h>
00043 #include <iostream>
00044
00045 using namespace std;
00046
00047 const int bufflen = 8192;
00048
00049 enum SocketType{
00050
00051 TYPE_TCP=1,
00052 TYPE_UDP,
00053 TYPE_ICMP
00054
00055 };
00056
00057 class Socket{
00058
00059 friend class SocketHandler;
00060 friend class RealSocket;
00061 friend class ListenServer;
00062 #ifdef HAVE_SSL
00063 friend class ConnectSocket;
00064 #endif
00065
00066 public:
00067
00068 Socket();
00069 Socket(socket_t _sock);
00070 virtual ~Socket();
00071
00072 void MakeSock();
00073 socket_t GetSock();
00074
00075 int GetPort();
00076 string GetAddr();
00077 string GetLastError();
00078 #ifdef HAVE_SSL
00079 void SetSSL(bool _is_SSL);
00080 #endif
00081
00082 private:
00083
00084 void OnReadError();
00085 void OnConnected();
00086 void OnConnClosed();
00087
00088 virtual void __OnConnClosed() {}
00089 virtual void __OnConnected() {}
00090 virtual void __OnRead() = 0;
00091 virtual void __OnClose() = 0;
00092 virtual void __OnReadError() = 0;
00093
00094 socket_t sock;
00095 #ifdef HAVE_SSL
00096 SSL_CTX* ctx;
00097 SSL* ssl;
00098 #endif
00099
00100 bool is_SSL;
00101 CriticalSection cs;
00102
00103 protected:
00104
00105 void Close();
00106 SocketType type;
00107 bool connected;
00108 int port;
00109 string addr;
00110
00111 struct sockaddr_in saddr;
00112
00113 };
00114
00115 string Resolv(string _addr);
00116
00117 #endif