diff options
Diffstat (limited to 'src/socket.cpp')
-rw-r--r-- | src/socket.cpp | 111 |
1 files changed, 92 insertions, 19 deletions
diff --git a/src/socket.cpp b/src/socket.cpp index 2b6f84cd3..cb019a3c2 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -25,6 +25,7 @@ using namespace std; #include <string> #include <unistd.h> #include <fcntl.h> +#include <poll.h> #include <sstream> #include <iostream> #include <fstream> @@ -66,6 +67,7 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { this->state = I_ERROR; + this->OnError(I_ERR_SOCKET); return; } this->port = port; @@ -82,9 +84,8 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long { if (errno != EINPROGRESS) { - shutdown(this->fd,2); - close(this->fd); - this->fd = -1; + this->Close(); + this->OnError(I_ERR_CONNECT); this->state = I_ERROR; return; } @@ -94,37 +95,109 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long } } -void InspSocket::EngineTrigger() +void InspSocket::Close() { - switch (this->state) + if (this->fd != -1) { - case I_CONNECTING: - this->OnConnected(); - break; - case I_LISTENING: - this->OnIncomingConnection(); - break; - case I_CONNECTED: - this->OnDataReady(); - break; - default: - break; + this->OnClose(); + shutdown(this->fd,2); + close(this->fd); + this->fd = -1; } } +char* InspSocket::Read() +{ + int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0); + if (n > 0) + { + return ibuf; + } + else + { + return NULL; + } +} + +// There are two possible outcomes to this function. +// It will either write all of the data, or an undefined amount. +// If an undefined amount is written the connection has failed +// and should be aborted. +int InspSocket::Write(std::string data) +{ + char* d = (char*)data.c_str(); + unsigned int written = 0; + int n = 0; + int s = data.length(); + while ((written < data.length()) && (n >= 0)) + { + n = send(this->fd,d,s,0); + if (n > 0) + { + // If we didnt write everything, advance + // the pointers so that when we retry + // the next time around the loop, we try + // to write what we failed to write before. + written += n; + s -= n; + d += n; + } + } + return written; +} + +bool InspSocket::Poll() +{ + if (time(NULL) > timeout_end) + { + this->OnTimeout(); + this->Close(); + this->OnError(I_ERR_TIMEOUT); + timeout = true; + this->state = I_ERROR; + return false; + } + polls.fd = this->fd; + state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN; + int ret = poll(&polls,1,1); + + if (ret > 0) + { + switch (this->state) + { + case I_CONNECTING: + return this->OnConnected(); + break; + case I_LISTENING: + this->OnIncomingConnection(); + break; + case I_CONNECTED: + return this->OnDataReady(); + break; + default: + break; + } + } + + return true; +} + void InspSocket::SetState(InspSocketState s) { this->state = s; } -int InspSocket::OnConnected() { return 0; } -int InspSocket::OnError() { return 0; } +bool InspSocket::OnConnected() { return true; } +void InspSocket::OnError(InspSocketError e) { return; } int InspSocket::OnDisconnect() { return 0; } int InspSocket::OnIncomingConnection() { return 0; } -int InspSocket::OnDataReady() { return 0; } +bool InspSocket::OnDataReady() { return true; } +void InspSocket::OnTimeout() { return; } +void InspSocket::OnClose() { return; } InspSocket::~InspSocket() { + this->Close(); } /* |