00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __INSP_SOCKET_H__
00018 #define __INSP_SOCKET_H__
00019
00020 #include <sys/types.h>
00021 #include <sys/socket.h>
00022 #include <netinet/in.h>
00023 #include <poll.h>
00024 #include <sstream>
00025 #include <string>
00026
00027 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
00028 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND };
00029
00030 class InspSocket
00031 {
00032 private:
00033 int fd;
00034 std::string host;
00035 int port;
00036 InspSocketState state;
00037 sockaddr_in addr;
00038 in_addr addy;
00039 time_t timeout_end;
00040 bool timeout;
00041 pollfd polls;
00042 char ibuf[16384];
00043 std::string IP;
00044 sockaddr_in client;
00045 sockaddr_in server;
00046 socklen_t length;
00047 public:
00048 InspSocket();
00049 InspSocket(int newfd, char* ip);
00050 InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
00051 virtual bool OnConnected();
00052 virtual void OnError(InspSocketError e);
00053 virtual int OnDisconnect();
00054 virtual bool OnDataReady();
00055 virtual void OnTimeout();
00056 virtual void OnClose();
00057 virtual char* Read();
00058 std::string GetIP();
00059 virtual int Write(std::string data);
00060 virtual int OnIncomingConnection(int newfd, char* ip);
00061 void SetState(InspSocketState s);
00062 InspSocketState GetState();
00063 bool Poll();
00064 int GetFd();
00065 virtual void Close();
00066 virtual ~InspSocket();
00067 };
00068
00069 #endif