diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2005-11-23 11:22:06 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2005-11-23 11:22:06 +0000 |
commit | 3f8978ceef854ab7c91e86751b61a4405c10c4c8 (patch) | |
tree | bd32f70b79616d3da8acf96befea6b58fd3e136a /src/socket.cpp | |
parent | e24156804d5bf625b200d4351fd4b65002df1fc5 (diff) |
Added some stuff to socket classes
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1929 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/socket.cpp')
-rw-r--r-- | src/socket.cpp | 78 |
1 files changed, 72 insertions, 6 deletions
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() { |