summaryrefslogtreecommitdiff
path: root/src/socket.cpp
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-11-25 09:32:17 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-11-25 09:32:17 +0000
commit6550242735047783301150f3551174797dddc7f5 (patch)
treeb568e1434a30005a03cc1f29a2a282c2936bc78c /src/socket.cpp
parent3f8978ceef854ab7c91e86751b61a4405c10c4c8 (diff)
Started removal of server linking code from core and implementation of socket API
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1930 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/socket.cpp')
-rw-r--r--src/socket.cpp111
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();
}
/*