The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

BufferedSocket Class Reference

BufferedSocket is an extendable socket class which modules can use for TCP socket support. More...

#include <inspsocket.h>

Inheritance diagram for BufferedSocket:

Inheritance graph
[legend]
Collaboration diagram for BufferedSocket:

Collaboration graph
[legend]
List of all members.

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.
InspIRCdInstance
 Instance we were created by.
SocketTimeoutTimeout
 Timeout class or NULL.
unsigned long timeout_val
 Timeout length.
std::deque< std::stringoutbuffer
 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.

Detailed Description

BufferedSocket is an extendable socket class which modules can use for TCP socket support.

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.


Constructor & Destructor Documentation

BufferedSocket::BufferedSocket InspIRCd SI  ) 
 

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.

BufferedSocket::BufferedSocket InspIRCd SI,
int  newfd,
const char *  ip
 

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.

BufferedSocket::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.

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'.

Parameters:
ipaddr The IP to connect to, or bind to
port The port number to connect to, or bind to
listening true to listen on the given host:port pair, or false to connect to them
maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
connectbindip When creating an outbound connection, the IP to bind the connection to. If not defined, the port is not bound.
Returns:
On exit, GetState() returns I_ERROR if an error occured, and errno can be used to read the socket error.

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.

BufferedSocket::~BufferedSocket  )  [virtual]
 

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.


Member Function Documentation

bool BufferedSocket::BindAddr const std::string ip  ) 
 

Bind to an address.

Parameters:
ip IP to bind to
Returns:
True is the binding succeeded

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.

void BufferedSocket::Close  )  [virtual]
 

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().

bool BufferedSocket::DoConnect  )  [virtual]
 

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.

bool BufferedSocket::FlushWriteBuffer  ) 
 

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().

int BufferedSocket::GetFd  ) 
 

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().

std::string BufferedSocket::GetIP  ) 
 

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().

BufferedSocketState BufferedSocket::GetState  ) 
 

Returns the current socket state.

Definition at line 683 of file inspsocket.cpp.

References state.

Referenced by SpanningTreeUtilities::ReadConfiguration().

void BufferedSocket::HandleEvent EventType  et,
int  errornum = 0
[virtual]
 

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.

void BufferedSocket::OnClose  )  [virtual]
 

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().

bool BufferedSocket::OnConnected  )  [virtual]
 

This method is called when an outbound connection on your socket is completed.

Returns:
false to abort the connection, true to continue

Reimplemented in TreeSocket.

Definition at line 693 of file inspsocket.cpp.

Referenced by Poll().

bool BufferedSocket::OnDataReady  )  [virtual]
 

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.

Returns:
false to close the socket

Reimplemented in HttpServerSocket, ResultNotifier, Notifier, ResultNotifier, and TreeSocket.

Definition at line 697 of file inspsocket.cpp.

Referenced by Poll().

int BufferedSocket::OnDisconnect  )  [virtual]
 

When an established connection is terminated, the OnDisconnect method is triggered.

Reimplemented in TreeSocket.

Definition at line 695 of file inspsocket.cpp.

void BufferedSocket::OnError BufferedSocketError  e  )  [virtual]
 

This method is called when an error occurs.

A closed socket in itself is not an error, however errors also generate close events.

Parameters:
e The error type which occured

Reimplemented in TreeSocket.

Definition at line 694 of file inspsocket.cpp.

Referenced by BindAddr(), BufferedSocket(), DoConnect(), FlushWriteBuffer(), HandleEvent(), and SocketTimeout::Tick().

int BufferedSocket::OnIncomingConnection int  newfd,
char *  ip
[virtual]
 

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().

void BufferedSocket::OnTimeout  )  [virtual]
 

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().

bool BufferedSocket::OnWriteReady  )  [virtual]
 

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().

Returns:
false to close the socket

Reimplemented in HttpServerSocket.

Definition at line 698 of file inspsocket.cpp.

bool BufferedSocket::Poll  ) 
 

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().

const char * BufferedSocket::Read  )  [virtual]
 

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().

bool BufferedSocket::Readable  )  [virtual]
 

Returns true if this socket is readable.

Reimplemented from EventHandler.

Definition at line 23 of file inspsocket.cpp.

References I_CONNECTING.

void BufferedSocket::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.

Definition at line 148 of file inspsocket.cpp.

Referenced by DoConnect(), and Poll().

void BufferedSocket::SetState BufferedSocketState  s  ) 
 

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().

void BufferedSocket::WantWrite  ) 
 

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().

void BufferedSocket::Write const std::string data  )  [virtual]
 

Writes a std::string to the socket.

No carriage returns or linefeeds are appended to the string.

Parameters:
data The data to send

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().


Member Data Documentation

std::string BufferedSocket::cbindip
 

Bind IP.

Definition at line 112 of file inspsocket.h.

Referenced by BufferedSocket().

bool BufferedSocket::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.

Definition at line 192 of file inspsocket.h.

Referenced by BufferedSocket().

char BufferedSocket::host[MAXBUF]
 

The hostname connected to.

Definition at line 142 of file inspsocket.h.

Referenced by BufferedSocket(), and TreeSocket::TreeSocket().

InspIRCd* BufferedSocket::Instance
 

Instance we were created by.

Definition at line 122 of file inspsocket.h.

Referenced by TreeSocket::AddLine(), TreeSocket::Admin(), BindAddr(), BufferedSocket(), TreeSocket::Capab(), TreeSocket::ChangeHost(), TreeSocket::ChangeName(), Close(), TreeSocket::ComparePass(), TreeSocket::DelLine(), TreeSocket::DoBurst(), TreeSocket::DoCollision(), DoConnect(), TreeSocket::Encap(), TreeSocket::Error(), FlushWriteBuffer(), TreeSocket::ForceJoin(), TreeSocket::ForceMode(), TreeSocket::ForceNick(), TreeSocket::ForceTopic(), HandleEvent(), TreeSocket::Inbound_Server(), TreeSocket::LocalPong(), TreeSocket::MakePass(), TreeSocket::MetaData(), TreeSocket::Modules(), TreeSocket::Motd(), TreeSocket::MyCapabilities(), TreeSocket::OnClose(), TreeSocket::OnConnected(), Notifier::OnDataReady(), ResultNotifier::OnDataReady(), HttpServerSocket::OnDataReady(), TreeSocket::OnIncomingConnection(), ResultNotifier::OnIncomingConnection(), HttpServerSocket::OnWriteReady(), TreeSocket::OperQuit(), TreeSocket::OperType(), TreeSocket::Outbound_Reply_Server(), TreeSocket::ParseUID(), Poll(), TreeSocket::ProcessLine(), TreeSocket::Push(), TreeSocket::RandString(), Read(), TreeSocket::RemoteKill(), TreeSocket::RemoteServer(), TreeSocket::RemoveStatus(), TreeSocket::SendCapabilities(), TreeSocket::SendChannelModes(), TreeSocket::SendFJoins(), HttpServerSocket::SendHeaders(), TreeSocket::SendUsers(), TreeSocket::SendXLines(), TreeSocket::ServerMessage(), TreeSocket::ServiceJoin(), TreeSocket::ServicePart(), HttpServerSocket::SetWrite(), TreeSocket::Squit(), TreeSocket::Stats(), TreeSocket::Time(), TreeSocket::TreeSocket(), WantWrite(), TreeSocket::Whois(), Write(), TreeSocket::WriteLine(), and ~BufferedSocket().

char BufferedSocket::IP[MAXBUF]
 

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().

bool BufferedSocket::IsIOHooked
 

Is hooked by a module for low level IO.

Definition at line 117 of file inspsocket.h.

Referenced by ServerConfig::AddIOHook(), and BufferedSocket().

socklen_t BufferedSocket::length
 

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().

std::deque<std::string> BufferedSocket::outbuffer
 

Socket output buffer (binary safe).

Definition at line 137 of file inspsocket.h.

Referenced by FlushWriteBuffer(), and Write().

int BufferedSocket::port
 

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().

BufferedSocketState BufferedSocket::state
 

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().

bool BufferedSocket::timeout
 

This value is true if the socket has timed out.

Definition at line 161 of file inspsocket.h.

Referenced by SocketTimeout::Tick().

SocketTimeout* BufferedSocket::Timeout
 

Timeout class or NULL.

Definition at line 127 of file inspsocket.h.

Referenced by BufferedSocket(), DoConnect(), SocketTimeout::Tick(), and ~BufferedSocket().

unsigned long BufferedSocket::timeout_val
 

Timeout length.

Definition at line 132 of file inspsocket.h.

Referenced by BufferedSocket(), and DoConnect().

bool BufferedSocket::WaitingForWriteEvent
 

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().


The documentation for this class was generated from the following files: