Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

InspSocket Class Reference

#include <socket.h>

Collaboration diagram for InspSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 InspSocket ()
 InspSocket (int newfd, char *ip)
 InspSocket (std::string host, int port, bool listening, unsigned long maxtime)
virtual bool OnConnected ()
virtual void OnError (InspSocketError e)
virtual int OnDisconnect ()
virtual bool OnDataReady ()
virtual void OnTimeout ()
virtual void OnClose ()
virtual char * Read ()
std::string GetIP ()
virtual int Write (std::string data)
virtual int OnIncomingConnection (int newfd, char *ip)
void SetState (InspSocketState s)
InspSocketState GetState ()
bool Poll ()
int GetFd ()
virtual void Close ()
virtual ~InspSocket ()

Private Attributes

int fd
std::string host
int port
InspSocketState state
sockaddr_in addr
in_addr addy
time_t timeout_end
bool timeout
pollfd polls
char ibuf [16384]
std::string IP
sockaddr_in client
sockaddr_in server
socklen_t length

Detailed Description

Definition at line 30 of file socket.h.


Constructor & Destructor Documentation

InspSocket::InspSocket  ) 
 

Definition at line 49 of file socket.cpp.

References I_DISCONNECTED, and state.

00050 {
00051         this->state = I_DISCONNECTED;
00052 }

InspSocket::InspSocket int  newfd,
char *  ip
 

Definition at line 54 of file socket.cpp.

References SocketEngine::AddFd(), fd, I_CONNECTED, IP, state, and X_ESTAB_MODULE.

00055 {
00056         this->fd = newfd;
00057         this->state = I_CONNECTED;
00058         this->IP = ip;
00059         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
00060 }

InspSocket::InspSocket std::string  host,
int  port,
bool  listening,
unsigned long  maxtime
 

Definition at line 62 of file socket.cpp.

References SocketEngine::AddFd(), addr, addy, Close(), DEBUG, fd, I_CONNECTING, I_ERR_BIND, I_ERR_CONNECT, I_ERR_SOCKET, I_ERROR, I_LISTENING, IP, OnError(), state, timeout, timeout_end, and X_ESTAB_MODULE.

00063 {
00064         if (listening) {
00065                 if ((this->fd = OpenTCPSocket()) == ERROR)
00066                 {
00067                         this->fd = -1;
00068                         this->state = I_ERROR;
00069                         this->OnError(I_ERR_SOCKET);
00070                         log(DEBUG,"OpenTCPSocket() error");
00071                         return;
00072                 }
00073                 else
00074                 {
00075                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
00076                         {
00077                                 this->Close();
00078                                 this->fd = -1;
00079                                 this->state = I_ERROR;
00080                                 this->OnError(I_ERR_BIND);
00081                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
00082                                 return;
00083                         }
00084                         else
00085                         {
00086                                 this->state = I_LISTENING;
00087                                 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
00088                                 log(DEBUG,"New socket now in I_LISTENING state");
00089                                 return;
00090                         }
00091                 }                       
00092         } else {
00093                 char* ip;
00094                 this->host = host;
00095                 hostent* hoste = gethostbyname(host.c_str());
00096                 if (!hoste) {
00097                         ip = (char*)host.c_str();
00098                 } else {
00099                         struct in_addr* ia = (in_addr*)hoste->h_addr;
00100                         ip = inet_ntoa(*ia);
00101                 }
00102 
00103                 this->IP = ip;
00104 
00105                 timeout_end = time(NULL)+maxtime;
00106                 timeout = false;
00107                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
00108                 {
00109                         this->state = I_ERROR;
00110                         this->OnError(I_ERR_SOCKET);
00111                         return;
00112                 }
00113                 this->port = port;
00114                 inet_aton(ip,&addy);
00115                 addr.sin_family = AF_INET;
00116                 addr.sin_addr = addy;
00117                 addr.sin_port = htons(this->port);
00118 
00119                 int flags;
00120                 flags = fcntl(this->fd, F_GETFL, 0);
00121                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
00122 
00123                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
00124                 {
00125                         if (errno != EINPROGRESS)
00126                         {
00127                                 this->Close();
00128                                 this->OnError(I_ERR_CONNECT);
00129                                 this->state = I_ERROR;
00130                                 return;
00131                         }
00132                 }
00133                 this->state = I_CONNECTING;
00134                 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
00135                 return;
00136         }
00137 }

InspSocket::~InspSocket  )  [virtual]
 

Definition at line 265 of file socket.cpp.

References Close().

00266 {
00267         this->Close();
00268 }


Member Function Documentation

void InspSocket::Close  )  [virtual]
 

Definition at line 139 of file socket.cpp.

References fd, and OnClose().

Referenced by InspSocket(), and ~InspSocket().

00140 {
00141         if (this->fd != -1)
00142         {
00143                 this->OnClose();
00144                 shutdown(this->fd,2);
00145                 close(this->fd);
00146                 this->fd = -1;
00147         }
00148 }

int InspSocket::GetFd  ) 
 

Definition at line 252 of file socket.cpp.

References fd.

00253 {
00254         return this->fd;
00255 }

std::string InspSocket::GetIP  ) 
 

Definition at line 150 of file socket.cpp.

References IP.

00151 {
00152         return this->IP;
00153 }

InspSocketState InspSocket::GetState  ) 
 

Definition at line 247 of file socket.cpp.

References state.

00248 {
00249         return this->state;
00250 }

void InspSocket::OnClose  )  [virtual]
 

Definition at line 263 of file socket.cpp.

Referenced by Close().

00263 { return; }

bool InspSocket::OnConnected  )  [virtual]
 

Definition at line 257 of file socket.cpp.

Referenced by Poll().

00257 { return true; }

bool InspSocket::OnDataReady  )  [virtual]
 

Definition at line 261 of file socket.cpp.

Referenced by Poll().

00261 { return true; }

int InspSocket::OnDisconnect  )  [virtual]
 

Definition at line 259 of file socket.cpp.

00259 { return 0; }

void InspSocket::OnError InspSocketError  e  )  [virtual]
 

Definition at line 258 of file socket.cpp.

Referenced by InspSocket(), and Poll().

00258 { return; }

int InspSocket::OnIncomingConnection int  newfd,
char *  ip
[virtual]
 

Definition at line 260 of file socket.cpp.

Referenced by Poll().

00260 { return 0; }

void InspSocket::OnTimeout  )  [virtual]
 

Definition at line 262 of file socket.cpp.

Referenced by Poll().

00262 { return; }

bool InspSocket::Poll  ) 
 

Definition at line 197 of file socket.cpp.

References SocketEngine::AddFd(), client, SocketEngine::DelFd(), I_CONNECTED, I_CONNECTING, I_ERR_TIMEOUT, I_ERROR, I_LISTENING, length, OnConnected(), OnDataReady(), OnError(), OnIncomingConnection(), OnTimeout(), SetState(), state, timeout, timeout_end, and X_ESTAB_MODULE.

00198 {
00199         if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
00200         {
00201                 // for non-listening sockets, the timeout can occur
00202                 // which causes termination of the connection after
00203                 // the given number of seconds without a successful
00204                 // connection.
00205                 this->OnTimeout();
00206                 this->OnError(I_ERR_TIMEOUT);
00207                 timeout = true;
00208                 this->state = I_ERROR;
00209                 return false;
00210         }
00211 
00212         int incoming = -1;
00213         
00214         switch (this->state)
00215         {
00216                 case I_CONNECTING:
00217                         this->SetState(I_CONNECTED);
00218                         /* Our socket was in write-state, so delete it and re-add it
00219                          * in read-state.
00220                          */
00221                         SE->DelFd(this->fd);
00222                         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
00223                         return this->OnConnected();
00224                 break;
00225                 case I_LISTENING:
00226                         length = sizeof (client);
00227                         incoming = accept (this->fd, (sockaddr*)&client,&length);
00228                         this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
00229                         return true;
00230                 break;
00231                 case I_CONNECTED:
00232                         return this->OnDataReady();
00233                 break;
00234                 default:
00235                 break;
00236         }
00237 
00238         return true;
00239 }

char * InspSocket::Read  )  [virtual]
 

Definition at line 155 of file socket.cpp.

References DEBUG, and ibuf.

00156 {
00157         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
00158         if (n > 0)
00159         {
00160                 ibuf[n] = 0;
00161                 return ibuf;
00162         }
00163         else
00164         {
00165                 log(DEBUG,"EOF or error on socket");
00166                 return NULL;
00167         }
00168 }

void InspSocket::SetState InspSocketState  s  ) 
 

Definition at line 241 of file socket.cpp.

References DEBUG, and state.

Referenced by Poll().

00242 {
00243         log(DEBUG,"Socket state change");
00244         this->state = s;
00245 }

int InspSocket::Write std::string  data  )  [virtual]
 

Definition at line 174 of file socket.cpp.

00175 {
00176         char* d = (char*)data.c_str();
00177         unsigned int written = 0;
00178         int n = 0;
00179         int s = data.length();
00180         while ((written < data.length()) && (n >= 0))
00181         {
00182                 n = send(this->fd,d,s,0);
00183                 if (n > 0)
00184                 {
00185                         // If we didnt write everything, advance
00186                         // the pointers so that when we retry
00187                         // the next time around the loop, we try
00188                         // to write what we failed to write before.
00189                         written += n;
00190                         s -= n;
00191                         d += n;
00192                 }
00193         }
00194         return written;
00195 }


Member Data Documentation

sockaddr_in InspSocket::addr [private]
 

Definition at line 37 of file socket.h.

Referenced by InspSocket().

in_addr InspSocket::addy [private]
 

Definition at line 38 of file socket.h.

Referenced by InspSocket().

sockaddr_in InspSocket::client [private]
 

Definition at line 44 of file socket.h.

Referenced by Poll().

int InspSocket::fd [private]
 

Definition at line 33 of file socket.h.

Referenced by Close(), GetFd(), and InspSocket().

std::string InspSocket::host [private]
 

Definition at line 34 of file socket.h.

char InspSocket::ibuf[16384] [private]
 

Definition at line 42 of file socket.h.

Referenced by Read().

std::string InspSocket::IP [private]
 

Definition at line 43 of file socket.h.

Referenced by GetIP(), and InspSocket().

socklen_t InspSocket::length [private]
 

Definition at line 46 of file socket.h.

Referenced by Poll().

pollfd InspSocket::polls [private]
 

Definition at line 41 of file socket.h.

int InspSocket::port [private]
 

Definition at line 35 of file socket.h.

sockaddr_in InspSocket::server [private]
 

Definition at line 45 of file socket.h.

InspSocketState InspSocket::state [private]
 

Definition at line 36 of file socket.h.

Referenced by GetState(), InspSocket(), Poll(), and SetState().

bool InspSocket::timeout [private]
 

Definition at line 40 of file socket.h.

Referenced by InspSocket(), and Poll().

time_t InspSocket::timeout_end [private]
 

Definition at line 39 of file socket.h.

Referenced by InspSocket(), and Poll().


The documentation for this class was generated from the following files:
Generated on Mon Dec 12 13:31:12 2005 for InspIRCd by  doxygen 1.4.4-20050815