socket_clients.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  *
00011  */
00012 
00013 #include "services.h"
00014 #include "anope.h"
00015 #include "logger.h"
00016 #include "sockets.h"
00017 
00018 #include <errno.h>
00019 
00020 ConnectionSocket::ConnectionSocket() : Socket()
00021 {
00022 }
00023 
00024 void ConnectionSocket::Connect(const Anope::string &TargetHost, int Port)
00025 {
00026         this->io->Connect(this, TargetHost, Port);
00027 }
00028 
00029 bool ConnectionSocket::Process()
00030 {
00031         try
00032         {
00033                 if (this->flags[SF_CONNECTED])
00034                         return true;
00035                 else if (this->flags[SF_CONNECTING])
00036                         this->flags[this->io->FinishConnect(this)] = true;
00037                 else
00038                         this->flags[SF_DEAD] = true;
00039         }
00040         catch (const SocketException &ex)
00041         {
00042                 Log() << ex.GetReason();
00043         }
00044         return false;
00045 }
00046 
00047 void ConnectionSocket::ProcessError()
00048 {
00049         int optval = 0;
00050         socklen_t optlen = sizeof(optval);
00051         getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
00052         errno = optval;
00053         this->OnError(optval ? Anope::LastError() : "");
00054 }
00055 
00056 void ConnectionSocket::OnConnect()
00057 {
00058 }
00059 
00060 void ConnectionSocket::OnError(const Anope::string &error)
00061 {
00062         Log(LOG_DEBUG) << "Socket error: " << error;
00063 }
00064 
00065 ClientSocket::ClientSocket(ListenSocket *l, const sockaddrs &addr) : ls(l), clientaddr(addr)
00066 {
00067 }
00068 
00069 bool ClientSocket::Process()
00070 {
00071         try
00072         {
00073                 if (this->flags[SF_ACCEPTED])
00074                         return true;
00075                 else if (this->flags[SF_ACCEPTING])
00076                         this->flags[this->io->FinishAccept(this)] = true;
00077                 else
00078                         this->flags[SF_DEAD] = true;
00079         }
00080         catch (const SocketException &ex)
00081         {
00082                 Log() << ex.GetReason();
00083         }
00084         return false;
00085 }
00086 
00087 void ClientSocket::ProcessError()
00088 {
00089         int optval = 0;
00090         socklen_t optlen = sizeof(optval);
00091         getsockopt(this->GetFD(), SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&optval), &optlen);
00092         errno = optval;
00093         this->OnError(optval ? Anope::LastError() : "");
00094 }
00095 
00096 void ClientSocket::OnAccept()
00097 {
00098 }
00099 
00100 void ClientSocket::OnError(const Anope::string &error)
00101 {
00102         Log(LOG_DEBUG) << "Socket error: " << error;
00103 }
00104