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[1024];
00043 sockaddr_in client;
00044 sockaddr_in server;
00045 socklen_t length;
00046 public:
00047 InspSocket();
00048 InspSocket(int newfd);
00049 InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
00050 virtual bool OnConnected();
00051 virtual void OnError(InspSocketError e);
00052 virtual int OnDisconnect();
00053 virtual bool OnDataReady();
00054 virtual void OnTimeout();
00055 virtual void OnClose();
00056 virtual char* Read();
00057 virtual int Write(std::string data);
00058 virtual int OnIncomingConnection(int newfd, char* ip);
00059 void SetState(InspSocketState s);
00060 InspSocketState GetState();
00061 bool Poll();
00062 virtual void Close();
00063 virtual ~InspSocket();
00064 };
00065
00066 #endif