00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __Socket_h
00030 #define __Socket_h
00031
00032 #if HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036 #include "SocketInterface.hpp"
00037 #include "SocketEventWatcher.hpp"
00038 #include "DynamicBuffer.hpp"
00039 #include <cstdlib>
00040 #include <boost/shared_ptr.hpp>
00041 #include <sys/types.h>
00042 #ifndef WIN32
00043 #include <sys/socket.h>
00044 #include <netinet/in.h>
00045 #else
00046 #include <winsock.h>
00047 #endif
00048 #ifdef HAVE_OPENSSL
00049 #include <openssl/ssl.h>
00050 #endif
00051
00053
00055
00056 namespace H {
00057
00059
00061
00066 typedef enum {
00067 #ifndef WIN32
00068 SOCKET_INTERNET = PF_INET,
00069 SOCKET_UNIX = PF_UNIX,
00070 SOCKET_INTERNET2 = PF_INET6
00071 #else
00072 SOCKET_INTERNET = AF_INET,
00073 SOCKET_UNIX = AF_UNIX,
00074 #ifdef AF_INET 6
00075 SOCKET_INTERNET2 = AF_INET6
00076 #endif
00077 #endif
00078 } SocketDomain;
00079
00084 typedef enum {
00085 SOCKET_STREAM = SOCK_STREAM,
00086 SOCKET_DGRAM = SOCK_DGRAM,
00087 SOCKET_RAW = SOCK_RAW,
00088 SOCKET_SEQPACKET = SOCK_SEQPACKET,
00089 SOCKET_RDM = SOCK_RDM
00090 } SocketType;
00091
00096 typedef enum {
00097 SOCKET_PROTO_TCP = IPPROTO_TCP,
00098 SOCKET_PROTO_UDP = IPPROTO_UDP
00099 } SocketProtocol;
00100
00101
00106 #ifndef SOCKET_ERROR
00107 #define SOCKET_ERROR -1
00108 #endif
00109
00111
00113
00120 class Socket : public SocketInterface {
00121 public:
00122
00123 boost::shared_ptr<Socket> accept();
00124 void bind(int Port);
00125 void closeSocket();
00126 void connect(std::string Host, int Port);
00127 void createSocket(SocketDomain Domain = SOCKET_INTERNET, SocketType Type = SOCKET_STREAM);
00128 std::string getAddress() const;
00129 int getOldSocket() const;
00130 int getSocket() const;
00131 bool isSocketValid() const;
00132 void listen();
00133 void processEvents();
00134 int readIntoBuffer(DynamicBuffer<char> & Buffer);
00135 void setEventWatcher(SocketEventWatcher * pWatcher);
00136 void setMessageMode(bool Enabled);
00137 void setTo(Socket const & SocketToBecome);
00138 void shutdown();
00139 int write(const char * Buffer, int BufLen);
00140 void writeMessage(std::string const & Message, bool FormatMessage = true);
00141
00142
00143 Socket();
00144 Socket(Socket const & InitFrom);
00145 virtual ~Socket();
00146
00147 protected:
00148
00149 void addToMessageBuffer(char * Data, int BufLen);
00150 void handleSocketDisconnect();
00151 void handleSocketRead(DynamicBuffer<char> & ReadBuffer);
00152 void init();
00153 int read(char * Buffer, int BufLen);
00154 void setAddress();
00155 void threadProcRead();
00156
00157
00158 std::string mAddress;
00159 int mBacklog;
00160 SocketDomain mDomain;
00161 SocketEventWatcher * mpEventWatcher;
00162 int mOldSocket;
00163 int mPort;
00164 bool mProcessing;
00165 SocketProtocol mProtocol;
00166 DynamicBuffer<char> mMessageBuffer;
00167 bool mMessageMode;
00168 struct sockaddr_in mSockAddr;
00169 socklen_t mSockAddrLen;
00170 int mSocket;
00171 SocketType mType;
00172
00173 private:
00177 struct SocketReadThreadProc {
00178 SocketReadThreadProc(Socket * pSocketRead) : mpSocketRead(pSocketRead) {
00179 mpSocketRead->mThreading = false;
00180 };
00181
00183 void operator()() {
00184 mpSocketRead->mThreading = true;
00185 mpSocketRead->threadProcRead();
00186 mpSocketRead->mThreading = false;
00187 }
00188
00189 Socket * mpSocketRead;
00190 };
00191 bool mThreading;
00192 SocketReadThreadProc mThreadProcRead;
00193 };
00194
00196
00197 }
00198
00199 #endif // __Socket_h