#include <socket.h>
Collaboration diagram for InspSocket:
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 |
Definition at line 30 of file socket.h.
|
Definition at line 46 of file socket.cpp. References I_DISCONNECTED, and state. 00047 { 00048 this->state = I_DISCONNECTED; 00049 }
|
|
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 }
|
|
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 }
|
|
Definition at line 246 of file socket.cpp. References Close(). 00247 { 00248 this->Close(); 00249 }
|
|
Definition at line 130 of file socket.cpp. 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 }
|
|
Definition at line 233 of file socket.cpp. References state. 00234 { 00235 return this->state; 00236 }
|
|
Definition at line 244 of file socket.cpp. Referenced by Close().
|
|
Definition at line 238 of file socket.cpp. Referenced by Poll().
|
|
Definition at line 242 of file socket.cpp. Referenced by Poll().
|
|
Definition at line 240 of file socket.cpp.
|
|
Definition at line 239 of file socket.cpp. Referenced by InspSocket(), and Poll().
|
|
Definition at line 241 of file socket.cpp. Referenced by Poll().
|
|
Definition at line 243 of file socket.cpp. Referenced by 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 }
|
|
Definition at line 141 of file socket.cpp. 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 }
|
|
Definition at line 227 of file socket.cpp. Referenced by Poll().
|
|
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 }
|
|
Definition at line 37 of file socket.h. Referenced by InspSocket(). |
|
Definition at line 38 of file socket.h. Referenced by InspSocket(). |
|
Definition at line 43 of file socket.h. Referenced by Poll(). |
|
Definition at line 33 of file socket.h. Referenced by Close(), InspSocket(), and Poll(). |
|
|
|
Definition at line 42 of file socket.h. Referenced by Read(). |
|
Definition at line 45 of file socket.h. Referenced by Poll(). |
|
Definition at line 41 of file socket.h. Referenced by Poll(). |
|
|
|
|
|
Definition at line 36 of file socket.h. Referenced by GetState(), InspSocket(), Poll(), and SetState(). |
|
Definition at line 40 of file socket.h. Referenced by InspSocket(), and Poll(). |
|
Definition at line 39 of file socket.h. Referenced by InspSocket(), and Poll(). |