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)
 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 ()
virtual int Write (std::string data)
virtual int OnIncomingConnection (int newfd, char *ip)
void SetState (InspSocketState s)
InspSocketState GetState ()
bool Poll ()
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 [1024]
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 46 of file socket.cpp.

References I_DISCONNECTED, and state.

00047 {
00048         this->state = I_DISCONNECTED;
00049 }

InspSocket::InspSocket int  newfd  ) 
 

Definition at line 51 of file socket.cpp.

References fd, I_CONNECTED, and state.

00052 {
00053         this->fd = newfd;
00054         this->state = I_CONNECTED;
00055 }

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

Definition at line 57 of file socket.cpp.

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

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

InspSocket::~InspSocket  )  [virtual]
 

Definition at line 246 of file socket.cpp.

References Close().

00247 {
00248         this->Close();
00249 }


Member Function Documentation

void InspSocket::Close  )  [virtual]
 

Definition at line 130 of file socket.cpp.

References fd, and OnClose().

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

00131 {
00132         if (this->fd != -1)
00133         {
00134                 this->OnClose();
00135                 shutdown(this->fd,2);
00136                 close(this->fd);
00137                 this->fd = -1;
00138         }
00139 }

InspSocketState InspSocket::GetState  ) 
 

Definition at line 233 of file socket.cpp.

References state.

00234 {
00235         return this->state;
00236 }

void InspSocket::OnClose  )  [virtual]
 

Definition at line 244 of file socket.cpp.

Referenced by Close().

00244 { return; }

bool InspSocket::OnConnected  )  [virtual]
 

Definition at line 238 of file socket.cpp.

Referenced by Poll().

00238 { return true; }

bool InspSocket::OnDataReady  )  [virtual]
 

Definition at line 242 of file socket.cpp.

Referenced by Poll().

00242 { return true; }

int InspSocket::OnDisconnect  )  [virtual]
 

Definition at line 240 of file socket.cpp.

00240 { return 0; }

void InspSocket::OnError InspSocketError  e  )  [virtual]
 

Definition at line 239 of file socket.cpp.

Referenced by InspSocket(), and Poll().

00239 { return; }

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

Definition at line 241 of file socket.cpp.

Referenced by Poll().

00241 { return 0; }

void InspSocket::OnTimeout  )  [virtual]
 

Definition at line 243 of file socket.cpp.

Referenced by Poll().

00243 { return; }

bool InspSocket::Poll  ) 
 

Definition at line 183 of file socket.cpp.

References client, fd, I_CONNECTED, I_CONNECTING, I_ERR_TIMEOUT, I_ERROR, I_LISTENING, length, OnConnected(), OnDataReady(), OnError(), OnIncomingConnection(), OnTimeout(), polls, SetState(), state, timeout, and timeout_end.

00184 {
00185         if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
00186         {
00187                 // for non-listening sockets, the timeout can occur
00188                 // which causes termination of the connection after
00189                 // the given number of seconds without a successful
00190                 // connection.
00191                 this->OnTimeout();
00192                 this->OnError(I_ERR_TIMEOUT);
00193                 timeout = true;
00194                 this->state = I_ERROR;
00195                 return false;
00196         }
00197         polls.fd = this->fd;
00198         state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
00199         int ret = poll(&polls,1,1);
00200 
00201         if (ret > 0)
00202         {
00203                 int incoming = -1;
00204                 
00205                 switch (this->state)
00206                 {
00207                         case I_CONNECTING:
00208                                 this->SetState(I_CONNECTED);
00209                                 return this->OnConnected();
00210                         break;
00211                         case I_LISTENING:
00212                                 length = sizeof (client);
00213                                 incoming = accept (this->fd, (sockaddr*)&client,&length);
00214                                 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
00215                                 return true;
00216                         break;
00217                         case I_CONNECTED:
00218                                 return this->OnDataReady();
00219                         break;
00220                         default:
00221                         break;
00222                 }
00223         }
00224         return true;
00225 }

char * InspSocket::Read  )  [virtual]
 

Definition at line 141 of file socket.cpp.

References DEBUG, and ibuf.

00142 {
00143         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
00144         if (n > 0)
00145         {
00146                 ibuf[n] = 0;
00147                 return ibuf;
00148         }
00149         else
00150         {
00151                 log(DEBUG,"EOF or error on socket");
00152                 return NULL;
00153         }
00154 }

void InspSocket::SetState InspSocketState  s  ) 
 

Definition at line 227 of file socket.cpp.

References DEBUG, and state.

Referenced by Poll().

00228 {
00229         log(DEBUG,"Socket state change");
00230         this->state = s;
00231 }

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

Definition at line 160 of file socket.cpp.

00161 {
00162         char* d = (char*)data.c_str();
00163         unsigned int written = 0;
00164         int n = 0;
00165         int s = data.length();
00166         while ((written < data.length()) && (n >= 0))
00167         {
00168                 n = send(this->fd,d,s,0);
00169                 if (n > 0)
00170                 {
00171                         // If we didnt write everything, advance
00172                         // the pointers so that when we retry
00173                         // the next time around the loop, we try
00174                         // to write what we failed to write before.
00175                         written += n;
00176                         s -= n;
00177                         d += n;
00178                 }
00179         }
00180         return written;
00181 }


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 43 of file socket.h.

Referenced by Poll().

int InspSocket::fd [private]
 

Definition at line 33 of file socket.h.

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

std::string InspSocket::host [private]
 

Definition at line 34 of file socket.h.

char InspSocket::ibuf[1024] [private]
 

Definition at line 42 of file socket.h.

Referenced by Read().

socklen_t InspSocket::length [private]
 

Definition at line 45 of file socket.h.

Referenced by Poll().

pollfd InspSocket::polls [private]
 

Definition at line 41 of file socket.h.

Referenced by Poll().

int InspSocket::port [private]
 

Definition at line 35 of file socket.h.

sockaddr_in InspSocket::server [private]
 

Definition at line 44 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 Sun Nov 27 01:43:25 2005 for InspIRCd by  doxygen 1.4.4-20050815