From 3f8978ceef854ab7c91e86751b61a4405c10c4c8 Mon Sep 17 00:00:00 2001 From: brain Date: Wed, 23 Nov 2005 11:22:06 +0000 Subject: Added some stuff to socket classes git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1929 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/socket.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 6 deletions(-) (limited to 'src/socket.cpp') 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 #include #include +#include +#include #include #include +#include #include #include #include @@ -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() { -- cgit v1.2.3