inspsocket.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef __INSP_SOCKET_H__
00015 #define __INSP_SOCKET_H__
00016
00017 #include <sstream>
00018 #include <string>
00019 #include <deque>
00020 #include "dns.h"
00021 #include "inspircd_config.h"
00022 #include "socket.h"
00023 #include "inspsocket.h"
00024 #include "timer.h"
00025
00029 enum InspSocketState
00030 {
00032 I_DISCONNECTED,
00034 I_CONNECTING,
00036 I_CONNECTED,
00038 I_LISTENING,
00040 I_ERROR
00041 };
00042
00046 enum InspSocketError
00047 {
00049 I_ERR_TIMEOUT,
00051 I_ERR_SOCKET,
00053 I_ERR_CONNECT,
00055 I_ERR_BIND,
00057 I_ERR_RESOLVE,
00059 I_ERR_WRITE,
00061 I_ERR_NOMOREFDS
00062 };
00063
00064
00065 class InspSocket;
00066 class InspIRCd;
00067
00068 using irc::sockets::insp_sockaddr;
00069 using irc::sockets::insp_inaddr;
00070 using irc::sockets::insp_ntoa;
00071 using irc::sockets::insp_aton;
00072
00075 class CoreExport SocketTimeout : public InspTimer
00076 {
00077 private:
00080 InspSocket* sock;
00083 InspIRCd* ServerInstance;
00086 int sfd;
00087 public:
00095 SocketTimeout(int fd, InspIRCd* Instance, InspSocket* thesock, long secs_from_now, time_t now) : InspTimer(secs_from_now, now), sock(thesock), ServerInstance(Instance), sfd(fd) { };
00098 virtual void Tick(time_t now);
00099 };
00100
00112 class CoreExport InspSocket : public EventHandler
00113 {
00114 public:
00115
00119 std::string cbindip;
00120
00124 bool IsIOHooked;
00125
00129 InspIRCd* Instance;
00130
00134 SocketTimeout* Timeout;
00135
00139 unsigned long timeout_val;
00140
00144 std::deque<std::string> outbuffer;
00145
00149 char host[MAXBUF];
00150
00155 int port;
00156
00162 InspSocketState state;
00163
00168 bool timeout;
00169
00177 char ibuf[65535];
00178
00184 char IP[MAXBUF];
00185
00190 socklen_t length;
00191
00194 bool FlushWriteBuffer();
00195
00202 void SetQueues(int nfd);
00203
00208 bool ClosePending;
00209
00214 bool WaitingForWriteEvent;
00215
00221 bool BindAddr(const std::string &ip);
00222
00227 InspSocket(InspIRCd* SI);
00228
00237 InspSocket(InspIRCd* SI, int newfd, const char* ip);
00238
00252 InspSocket(InspIRCd* SI, const std::string &ipaddr, int port, bool listening, unsigned long maxtime, const std::string &connectbindip = "");
00253
00259 virtual bool OnConnected();
00260
00267 virtual void OnError(InspSocketError e);
00268
00273 virtual int OnDisconnect();
00274
00287 virtual bool OnDataReady();
00288
00298 virtual bool OnWriteReady();
00299
00311 virtual void OnTimeout();
00312
00321 virtual void OnClose();
00322
00328 virtual char* Read();
00329
00335 std::string GetIP();
00336
00342 virtual void Write(const std::string &data);
00343
00357 virtual int OnIncomingConnection(int newfd, char* ip);
00358
00364 void SetState(InspSocketState s);
00365
00371 void WantWrite();
00372
00376 InspSocketState GetState();
00377
00386 bool Poll();
00387
00393 int GetFd();
00394
00400 virtual void Close();
00401
00407 virtual ~InspSocket();
00408
00414 virtual bool DoConnect();
00415
00425 void MarkAsClosed();
00426
00429 void HandleEvent(EventType et, int errornum = 0);
00430
00433 bool Readable();
00434 };
00435
00436 #endif
00437