summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/socket.h17
-rw-r--r--src/socket.cpp78
2 files changed, 85 insertions, 10 deletions
diff --git a/include/socket.h b/include/socket.h
index 3e681f1c4..499a4d375 100644
--- a/include/socket.h
+++ b/include/socket.h
@@ -14,10 +14,13 @@
* ---------------------------------------------------
*/
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#include <sstream>
#include <string>
-enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING };
+enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
class InspSocket
{
@@ -26,13 +29,19 @@ private:
std::string host;
int port;
InspSocketState state;
+ sockaddr_in addr;
+ in_addr addy;
+ time_t timeout_end;
+ bool timeout;
public:
InspSocket();
- InspSocket(std::string host, int port, bool listening);
- void Poll();
+ InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
virtual int OnConnected();
virtual int OnError();
virtual int OnDisconnect();
+ virtual int OnDataReady();
virtual int OnIncomingConnection();
- ~InspSocket();
+ void SetState(InspSocketState s);
+ void EngineTrigger();
+ virtual ~InspSocket();
};
diff --git a/src/socket.cpp b/src/socket.cpp
index 920713566..2b6f84cd3 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -20,8 +20,11 @@ using namespace std;
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#include <string>
#include <unistd.h>
+#include <fcntl.h>
#include <sstream>
#include <iostream>
#include <fstream>
@@ -44,18 +47,81 @@ InspSocket::InspSocket()
this->state = I_DISCONNECTED;
}
-InspSocket::InspSocket(std::string host, int port, bool listening)
+InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
{
+ if (listening) {
+ } else {
+ char* ip;
+ this->host = host;
+ hostent* hoste = gethostbyname(host.c_str());
+ if (!hoste) {
+ ip = (char*)host.c_str();
+ } else {
+ struct in_addr* ia = (in_addr*)hoste->h_addr;
+ ip = inet_ntoa(*ia);
+ }
+
+ timeout_end = time(NULL)+maxtime;
+ timeout = false;
+ if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ this->state = I_ERROR;
+ return;
+ }
+ this->port = port;
+ inet_aton(ip,&addy);
+ addr.sin_family = AF_INET;
+ addr.sin_addr = addy;
+ addr.sin_port = htons(this->port);
+
+ int flags;
+ flags = fcntl(this->fd, F_GETFL, 0);
+ fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
+
+ if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
+ {
+ if (errno != EINPROGRESS)
+ {
+ shutdown(this->fd,2);
+ close(this->fd);
+ this->fd = -1;
+ this->state = I_ERROR;
+ return;
+ }
+ }
+ this->state = I_CONNECTING;
+ return;
+ }
+}
+
+void InspSocket::EngineTrigger()
+{
+ switch (this->state)
+ {
+ case I_CONNECTING:
+ this->OnConnected();
+ break;
+ case I_LISTENING:
+ this->OnIncomingConnection();
+ break;
+ case I_CONNECTED:
+ this->OnDataReady();
+ break;
+ default:
+ break;
+ }
}
-void InspSocket::Poll()
+void InspSocket::SetState(InspSocketState s)
{
+ this->state = s;
}
-int InspSocket::OnConnected() { }
-int InspSocket::OnError() { }
-int InspSocket::OnDisconnect() { }
-int InspSocket::OnIncomingConnection() { }
+int InspSocket::OnConnected() { return 0; }
+int InspSocket::OnError() { return 0; }
+int InspSocket::OnDisconnect() { return 0; }
+int InspSocket::OnIncomingConnection() { return 0; }
+int InspSocket::OnDataReady() { return 0; }
InspSocket::~InspSocket()
{