trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::Channel Class Reference

This class is used to implement reactor pattern. A Channel object manages a socket fd. Users use a Channel object to receive write or read events on the socket it manages. More...

#include <trantor/net/Channel.h>

Inheritance diagram for trantor::Channel:
Collaboration diagram for trantor::Channel:

Public Types

using EventCallback = std::function<void()>

Public Member Functions

 Channel (EventLoop *loop, int fd)
 Construct a new Channel instance.
void setReadCallback (const EventCallback &cb)
 Set the read callback.
void setReadCallback (EventCallback &&cb)
void setWriteCallback (const EventCallback &cb)
 Set the write callback.
void setWriteCallback (EventCallback &&cb)
void setCloseCallback (const EventCallback &cb)
 Set the close callback.
void setCloseCallback (EventCallback &&cb)
void setErrorCallback (const EventCallback &cb)
 Set the error callback.
void setErrorCallback (EventCallback &&cb)
void setEventCallback (const EventCallback &cb)
 Set the event callback.
void setEventCallback (EventCallback &&cb)
int fd () const
 Return the fd of the socket.
int events () const
 Return the events enabled on the socket.
int revents () const
 Return the events that occurred on the socket.
bool isNoneEvent () const
 Check whether there is no event enabled on the socket.
void disableAll ()
 Disable all events on the socket.
void remove ()
 Remove the socket from the poller in the event loop.
EventLoop * ownerLoop ()
 Return the event loop.
void enableReading ()
 Enable the read event on the socket.
void disableReading ()
 Disable the read event on the socket.
void enableWriting ()
 Enable the write event on the socket.
void disableWriting ()
 Disable the write event on the socket.
bool isWriting () const
 Check whether the write event is enabled on the socket.
bool isReading () const
 Check whether the read event is enabled on the socket.
void updateEvents (int events)
 Set and update the events enabled.
void tie (const std::shared_ptr< void > &obj)
 This method is used to ensure that the callback owner is valid when a callback is called.

Static Public Attributes

static const int kNoneEvent
static const int kReadEvent
static const int kWriteEvent

Friends

class EventLoop
class EpollPoller
class KQueue
class PollPoller

Detailed Description

This class is used to implement reactor pattern. A Channel object manages a socket fd. Users use a Channel object to receive write or read events on the socket it manages.

Constructor & Destructor Documentation

◆ Channel()

trantor::Channel::Channel ( EventLoop * loop,
int fd )

Construct a new Channel instance.

Parameters
loopThe event loop in which the channel works.
fdThe socket fd.

Member Function Documentation

◆ events()

int trantor::Channel::events ( ) const
inline

Return the events enabled on the socket.

Returns
int

◆ fd()

int trantor::Channel::fd ( ) const
inline

Return the fd of the socket.

Returns
int

◆ isNoneEvent()

bool trantor::Channel::isNoneEvent ( ) const
inline

Check whether there is no event enabled on the socket.

Returns
true
false

◆ isReading()

bool trantor::Channel::isReading ( ) const
inline

Check whether the read event is enabled on the socket.

Returns
true
false

◆ isWriting()

bool trantor::Channel::isWriting ( ) const
inline

Check whether the write event is enabled on the socket.

Returns
true
false

◆ ownerLoop()

EventLoop * trantor::Channel::ownerLoop ( )
inline

Return the event loop.

Returns
EventLoop*

◆ revents()

int trantor::Channel::revents ( ) const
inline

Return the events that occurred on the socket.

Returns
int

◆ setCloseCallback()

void trantor::Channel::setCloseCallback ( const EventCallback & cb)
inline

Set the close callback.

Parameters
cbThe callback is called when the socket is closed.

◆ setErrorCallback()

void trantor::Channel::setErrorCallback ( const EventCallback & cb)
inline

Set the error callback.

Parameters
cbThe callback is called when an error occurs on the socket.

◆ setEventCallback()

void trantor::Channel::setEventCallback ( const EventCallback & cb)
inline

Set the event callback.

Parameters
cbThe callback is called when any event occurs on the socket.
Note
If the event callback is set to the channel, any other callback wouldn't be called again.

◆ setReadCallback()

void trantor::Channel::setReadCallback ( const EventCallback & cb)
inline

Set the read callback.

Parameters
cbThe callback is called when read event occurs on the socket.
Note
One should call the enableReading() method to ensure that the callback would be called when some data is received on the socket.

◆ setWriteCallback()

void trantor::Channel::setWriteCallback ( const EventCallback & cb)
inline

Set the write callback.

Parameters
cbThe callback is called when write event occurs on the socket.
Note
One should call the enableWriting() method to ensure that the callback would be called when the socket can be written.

◆ tie()

void trantor::Channel::tie ( const std::shared_ptr< void > & obj)
inline

This method is used to ensure that the callback owner is valid when a callback is called.

Parameters
objThe callback owner. Usually, the owner is also the owner of the channel.
Note
The 'obj' is kept in a weak_ptr object, so this method does not cause a circular reference problem.

◆ updateEvents()

void trantor::Channel::updateEvents ( int events)
inline

Set and update the events enabled.

Parameters
events

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