|
|||
|
|||
|
#include <inspsocket.h>
Inheritance diagram for BufferedSocket:


Public Member Functions | |
| bool | FlushWriteBuffer () |
| Flushes the write buffer. | |
| void | SetQueues (int nfd) |
| Set the queue sizes This private method sets the operating system queue sizes for this socket to 65535 so that it can queue more information without application-level queueing which was required in older software. | |
| bool | BindAddr (const std::string &ip) |
| Bind to an address. | |
| BufferedSocket (InspIRCd *SI) | |
| The default constructor does nothing and should not be used. | |
| BufferedSocket (InspIRCd *SI, int newfd, const char *ip) | |
| This constructor is used to associate an existing connecting with an BufferedSocket class. | |
| BufferedSocket (InspIRCd *SI, const std::string &ipaddr, int port, bool listening, unsigned long maxtime, const std::string &connectbindip="") | |
| This constructor is used to create a new socket, either listening for connections, or an outbound connection to another host. | |
| virtual bool | OnConnected () |
| This method is called when an outbound connection on your socket is completed. | |
| virtual void | OnError (BufferedSocketError e) |
| This method is called when an error occurs. | |
| virtual int | OnDisconnect () |
| When an established connection is terminated, the OnDisconnect method is triggered. | |
| virtual bool | OnDataReady () |
| When there is data waiting to be read on a socket, the OnDataReady() method is called. | |
| virtual bool | OnWriteReady () |
| When it is ok to write to the socket, and a write event was requested, this method is triggered. | |
| virtual void | OnTimeout () |
| When an outbound connection fails, and the attempt times out, you will receive this event. | |
| virtual void | OnClose () |
| Whenever close() is called, OnClose() will be called first. | |
| virtual const char * | Read () |
| Reads all pending bytes from the socket into a char* array which can be up to 16 kilobytes in length. | |
| std::string | GetIP () |
| Returns the IP address associated with this connection, or an empty string if no IP address exists. | |
| virtual void | Write (const std::string &data) |
| Writes a std::string to the socket. | |
| virtual int | OnIncomingConnection (int newfd, char *ip) |
| If your socket is a listening socket, when a new connection comes in on the socket this method will be called. | |
| void | SetState (BufferedSocketState s) |
| Changes the socket's state. | |
| void | WantWrite () |
| Call this to receive the next write event that comes along for this fd to the OnWriteReady method. | |
| BufferedSocketState | GetState () |
| Returns the current socket state. | |
| bool | Poll () |
| Only the core should call this function. | |
| int | GetFd () |
| This method returns the socket's file descriptor as assigned by the operating system, or -1 if no descriptor has been assigned. | |
| virtual void | Close () |
| This method causes the socket to close, and may also be triggered by other methods such as OnTimeout and OnError. | |
| virtual | ~BufferedSocket () |
| The destructor may implicitly call OnClose(), and will close() and shutdown() the file descriptor used for this socket. | |
| virtual bool | DoConnect () |
| This method attempts to connect to a hostname. | |
| void | HandleEvent (EventType et, int errornum=0) |
| Handle event from EventHandler parent class. | |
| bool | Readable () |
| Returns true if this socket is readable. | |
Public Attributes | |
| std::string | cbindip |
| Bind IP. | |
| bool | IsIOHooked |
| Is hooked by a module for low level IO. | |
| InspIRCd * | Instance |
| Instance we were created by. | |
| SocketTimeout * | Timeout |
| Timeout class or NULL. | |
| unsigned long | timeout_val |
| Timeout length. | |
| std::deque< std::string > | outbuffer |
| Socket output buffer (binary safe). | |
| char | host [MAXBUF] |
| The hostname connected to. | |
| int | port |
| The port connected to, or the port this socket is listening on. | |
| BufferedSocketState | state |
| The state for this socket, either listening, connecting, connected or error. | |
| bool | timeout |
| This value is true if the socket has timed out. | |
| char | IP [MAXBUF] |
| The IP address being connected to stored in string form for easy retrieval by accessors. | |
| socklen_t | length |
| Used by accept() to indicate the sizes of the sockaddr_in structures. | |
| bool | ClosePending |
| When the socket has been marked as closing, this flag will be set to true, then the next time the socket is examined, the socket is deleted and closed. | |
| bool | WaitingForWriteEvent |
| Set to true when we're waiting for a write event. | |
It is fully integrated into InspIRCds socket loop and attaches its sockets to the core's instance of the SocketEngine class, meaning that any sockets you create have the same power and abilities as a socket created by the core itself. To use BufferedSocket, you must inherit a class from it, and use the BufferedSocket constructors to establish connections and bindings.
Definition at line 105 of file inspsocket.h.
|
|
The default constructor does nothing and should not be used.
Definition at line 28 of file inspsocket.cpp. References EventHandler::fd, I_DISCONNECTED, Instance, IsIOHooked, state, Timeout, and WaitingForWriteEvent. |
|
||||||||||||||||
|
This constructor is used to associate an existing connecting with an BufferedSocket class. The given file descriptor must be valid, and when initialized, the BufferedSocket will be set with the given IP address and placed in CONNECTED state. Definition at line 38 of file inspsocket.cpp. References SocketEngine::AddFd(), EventHandler::fd, I_CONNECTED, Instance, IsIOHooked, MAXBUF, InspIRCd::SE, state, strlcpy(), Timeout, and WaitingForWriteEvent. |
|
||||||||||||||||||||||||||||
|
This constructor is used to create a new socket, either listening for connections, or an outbound connection to another host. Note that if you specify a hostname in the 'ipaddr' parameter, this class will not connect. You must resolve your hostnames before passing them to BufferedSocket. To do so, you should use the nonblocking class 'Resolver'.
Definition at line 51 of file inspsocket.cpp. References InspIRCd::BindSocket(), cbindip, Close(), ClosePending, DEBUG, ERROR, EventHandler::fd, host, I_ERR_BIND, I_ERR_CONNECT, I_ERR_NOMOREFDS, I_ERR_SOCKET, I_ERROR, I_LISTENING, Instance, IsIOHooked, LogManager::Log(), InspIRCd::Logs, MAXBUF, OnError(), irc::sockets::OpenTCPSocket(), port, state, strlcpy(), Timeout, timeout_val, and WaitingForWriteEvent. |
|
|
The destructor may implicitly call OnClose(), and will close() and shutdown() the file descriptor used for this socket.
Definition at line 702 of file inspsocket.cpp. References Close(), TimerManager::DelTimer(), Instance, Timeout, and InspIRCd::Timers. |
|
|
Bind to an address.
Definition at line 168 of file inspsocket.cpp. References SocketEngine::Bind(), Conf, DEBUG, ConfigReader::Enumerate(), EventHandler::fd, I_ERR_BIND, I_ERROR, Instance, LogManager::Log(), InspIRCd::Logs, OnError(), ConfigReader::ReadValue(), InspIRCd::SE, and state. |
|
|
This method causes the socket to close, and may also be triggered by other methods such as OnTimeout and OnError.
Definition at line 353 of file inspsocket.cpp. References SocketEngine::Close(), InspIRCd::Config, DEFAULT, ServerConfig::GetIOHook(), CoreException::GetReason(), CoreException::GetSource(), I_LISTENING, Instance, LogManager::Log(), InspIRCd::Logs, OnClose(), Module::OnRawSocketClose(), InspIRCd::SE, SocketEngine::Shutdown(), and InspIRCd::SocketCull. Referenced by BufferedSocket(), DoConnect(), ModuleSpanningTree::DoPingChecks(), FlushWriteBuffer(), cmd_rsquit::Handle(), ModuleSpanningTree::HandleSquit(), SpanningTreeUtilities::ReadConfiguration(), ~BufferedSocket(), ModuleMsSQL::~ModuleMsSQL(), ModuleSQLite3::~ModuleSQLite3(), and SpanningTreeUtilities::~SpanningTreeUtilities(). |
|
|
This method attempts to connect to a hostname. This only occurs on a non-listening socket. This method is asyncronous. Definition at line 243 of file inspsocket.cpp. References TimerManager::AddTimer(), SocketEngine::Blocking(), Close(), SocketEngine::Connect(), DEBUG, EventHandler::fd, I_CONNECTING, I_ERR_CONNECT, I_ERR_NOMOREFDS, I_ERR_SOCKET, I_ERROR, Instance, LogManager::Log(), InspIRCd::Logs, SocketEngine::NonBlocking(), OnError(), InspIRCd::SE, SetQueues(), state, InspIRCd::Time(), Timeout, timeout_val, and InspIRCd::Timers. |
|
|
Flushes the write buffer.
Definition at line 466 of file inspsocket.cpp. References Close(), InspIRCd::Config, DEBUG, SocketEngine::DelFd(), EventHandler::fd, ServerConfig::GetIOHook(), CoreException::GetReason(), CoreException::GetSource(), I_CONNECTED, I_ERR_WRITE, I_ERROR, Instance, length, LogManager::Log(), InspIRCd::Logs, OnError(), Module::OnRawSocketWrite(), outbuffer, InspIRCd::SE, SocketEngine::Send(), state, and SocketEngine::WantWrite(). Referenced by HttpServerSocket::Page(), TreeSocket::SendError(), HttpServerSocket::SendHTTPError(), and HttpServerSocket::ServeData(). |
|
|
This method returns the socket's file descriptor as assigned by the operating system, or -1 if no descriptor has been assigned.
Reimplemented from EventHandler. Definition at line 688 of file inspsocket.cpp. References EventHandler::fd. Referenced by ModuleSpanningTree::ConnectServer(), HandshakeTimer::HandshakeTimer(), ServernameResolver::OnLookupComplete(), ModuleSSLOpenSSL::OnRequest(), and ModuleSSLGnuTLS::OnRequest(). |
|
|
Returns the IP address associated with this connection, or an empty string if no IP address exists.
Definition at line 384 of file inspsocket.cpp. References IP. Referenced by TreeSocket::OnConnected(), and TreeSocket::SendError(). |
|
|
Returns the current socket state.
Definition at line 683 of file inspsocket.cpp. References state. Referenced by SpanningTreeUtilities::ReadConfiguration(). |
|
||||||||||||
|
Handle event from EventHandler parent class.
Implements EventHandler. Definition at line 712 of file inspsocket.cpp. References EVENT_ERROR, EVENT_READ, EVENT_WRITE, I_CONNECTING, I_ERR_BIND, I_ERR_CONNECT, I_ERR_TIMEOUT, I_ERR_WRITE, Instance, OnError(), InspIRCd::SocketCull, and WaitingForWriteEvent. |
|
|
Whenever close() is called, OnClose() will be called first. Please note that this means OnClose will be called alongside OnError(), OnTimeout(), and Close(), and also when cancelling a listening socket by calling the destructor indirectly. Reimplemented in HttpServerSocket, and TreeSocket. Definition at line 700 of file inspsocket.cpp. Referenced by Close(). |
|
|
This method is called when an outbound connection on your socket is completed.
Reimplemented in TreeSocket. Definition at line 693 of file inspsocket.cpp. Referenced by Poll(). |
|
|
When there is data waiting to be read on a socket, the OnDataReady() method is called. Within this method, you *MUST* call the Read() method to read any pending data. At its lowest level, this event is signalled by the core via the socket engine. If you return false from this function, the core removes your socket from its list and erases it from the socket engine, then calls BufferedSocket::Close() and deletes it.
Reimplemented in HttpServerSocket, ResultNotifier, Notifier, ResultNotifier, and TreeSocket. Definition at line 697 of file inspsocket.cpp. Referenced by Poll(). |
|
|
When an established connection is terminated, the OnDisconnect method is triggered.
Reimplemented in TreeSocket. Definition at line 695 of file inspsocket.cpp. |
|
|
This method is called when an error occurs. A closed socket in itself is not an error, however errors also generate close events.
Reimplemented in TreeSocket. Definition at line 694 of file inspsocket.cpp. Referenced by BindAddr(), BufferedSocket(), DoConnect(), FlushWriteBuffer(), HandleEvent(), and SocketTimeout::Tick(). |
|
||||||||||||
|
If your socket is a listening socket, when a new connection comes in on the socket this method will be called. Given the new file descriptor in the parameters, and the IP, it is recommended you copy them to a new instance of your socket class, e.g.: MySocket* newsocket = new MySocket(newfd,ip); Once you have done this, you can then associate the new socket with the core using Server::AddSocket(). Reimplemented in HttpServerSocket, ResultNotifier, Notifier, ResultNotifier, and TreeSocket. Definition at line 696 of file inspsocket.cpp. Referenced by Poll(). |
|
|
When an outbound connection fails, and the attempt times out, you will receive this event. The method will trigger once maxtime seconds are reached (as given in the constructor) just before the socket's descriptor is closed. A failed DNS lookup may cause this event if the DNS server is not responding, as well as a failed connect() call, because DNS lookups are nonblocking as implemented by this class. Reimplemented in TreeSocket. Definition at line 699 of file inspsocket.cpp. Referenced by SocketTimeout::Tick(). |
|
|
When it is ok to write to the socket, and a write event was requested, this method is triggered. Within this method you should call write() or send() etc, to send data to the other end of the socket. Further write events will not be triggered unless you call WantWrite().
Reimplemented in HttpServerSocket. Definition at line 698 of file inspsocket.cpp. |
|
|
Only the core should call this function. When called, it is assumed the socket is ready to read data, and the method call routes the event to the various methods of BufferedSocket for you to handle. This can also cause the socket's state to change. Definition at line 579 of file inspsocket.cpp. References SocketEngine::Accept(), SocketEngine::BoundsCheckFd(), InspIRCd::Config, DEBUG, SocketEngine::DelFd(), ServerConfig::GetIOHook(), CoreException::GetReason(), SocketEngine::GetRef(), CoreException::GetSource(), I_CONNECTED, I_CONNECTING, I_LISTENING, Instance, length, LogManager::Log(), InspIRCd::Logs, SocketEngine::NonBlocking(), OnConnected(), OnDataReady(), OnIncomingConnection(), Module::OnRawSocketAccept(), Module::OnRawSocketConnect(), port, InspIRCd::SE, SetQueues(), and SetState(). |
|
|
Reads all pending bytes from the socket into a char* array which can be up to 16 kilobytes in length.
Definition at line 389 of file inspsocket.cpp. References SocketEngine::BoundsCheckFd(), InspIRCd::Config, DEFAULT, ServerConfig::GetIOHook(), InspIRCd::GetReadBuffer(), CoreException::GetReason(), CoreException::GetSource(), Instance, LogManager::Log(), InspIRCd::Logs, ServerConfig::NetBufferSize, Module::OnRawSocketRead(), and InspIRCd::SE. Referenced by TreeSocket::OnDataReady(), and HttpServerSocket::OnDataReady(). |
|
|
Returns true if this socket is readable.
Reimplemented from EventHandler. Definition at line 23 of file inspsocket.cpp. References I_CONNECTING. |
|
|
Set the queue sizes This private method sets the operating system queue sizes for this socket to 65535 so that it can queue more information without application-level queueing which was required in older software.
Definition at line 148 of file inspsocket.cpp. Referenced by DoConnect(), and Poll(). |
|
|
Changes the socket's state. The core uses this to change socket states, and you should not call it directly. Definition at line 678 of file inspsocket.cpp. References state. Referenced by Poll(). |
|
|
Call this to receive the next write event that comes along for this fd to the OnWriteReady method.
Definition at line 142 of file inspsocket.cpp. References Instance, InspIRCd::SE, WaitingForWriteEvent, and SocketEngine::WantWrite(). |
|
|
Writes a std::string to the socket. No carriage returns or linefeeds are appended to the string.
Definition at line 457 of file inspsocket.cpp. References Instance, outbuffer, InspIRCd::SE, and SocketEngine::WantWrite(). Referenced by HttpServerSocket::Page(), HttpServerSocket::SendHeaders(), HttpServerSocket::SendHTTPError(), HttpServerSocket::ServeData(), and TreeSocket::WriteLine(). |
|
|
Bind IP.
Definition at line 112 of file inspsocket.h. Referenced by BufferedSocket(). |
|
|
When the socket has been marked as closing, this flag will be set to true, then the next time the socket is examined, the socket is deleted and closed.
Definition at line 192 of file inspsocket.h. Referenced by BufferedSocket(). |
|
|
The hostname connected to.
Definition at line 142 of file inspsocket.h. Referenced by BufferedSocket(), and TreeSocket::TreeSocket(). |
|
|
|
The IP address being connected to stored in string form for easy retrieval by accessors.
Definition at line 168 of file inspsocket.h. Referenced by GetIP(). |
|
|
Is hooked by a module for low level IO.
Definition at line 117 of file inspsocket.h. Referenced by ServerConfig::AddIOHook(), and BufferedSocket(). |
|
|
Used by accept() to indicate the sizes of the sockaddr_in structures.
Definition at line 174 of file inspsocket.h. Referenced by FlushWriteBuffer(), and Poll(). |
|
|
Socket output buffer (binary safe).
Definition at line 137 of file inspsocket.h. Referenced by FlushWriteBuffer(), and Write(). |
|
|
The port connected to, or the port this socket is listening on.
Definition at line 148 of file inspsocket.h. Referenced by BufferedSocket(), and Poll(). |
|
|
The state for this socket, either listening, connecting, connected or error.
Definition at line 155 of file inspsocket.h. Referenced by BindAddr(), BufferedSocket(), DoConnect(), FlushWriteBuffer(), GetState(), SetState(), and SocketTimeout::Tick(). |
|
|
This value is true if the socket has timed out.
Definition at line 161 of file inspsocket.h. Referenced by SocketTimeout::Tick(). |
|
|
Timeout class or NULL.
Definition at line 127 of file inspsocket.h. Referenced by BufferedSocket(), DoConnect(), SocketTimeout::Tick(), and ~BufferedSocket(). |
|
|
Timeout length.
Definition at line 132 of file inspsocket.h. Referenced by BufferedSocket(), and DoConnect(). |
|
|
Set to true when we're waiting for a write event. If this is true and a write event comes in, we call the write instead of the read method. Definition at line 198 of file inspsocket.h. Referenced by BufferedSocket(), HandleEvent(), HttpServerSocket::SetWrite(), and WantWrite(). |