Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Connection Class Reference

One client socket plus its read/write buffers. More...

#include <Connection.hpp>

+ Collaboration diagram for Connection:

Public Member Functions

 Connection (int fd)
 
 ~Connection ()
 
void appendRead (std::string_view data)
 Append freshly-read bytes to the read buffer.
 
std::optional< std::string > nextLine ()
 Pop one complete '
'-terminated line, or nullopt if none buffered yet.
 
void queueWrite (const std::string &msg)
 Append msg to the write buffer (not sent until flushWrite()).
 
void flushWrite ()
 Try to send the buffered write data over the socket.
 
int fd () const
 
ClientType type () const
 Client kind: PENDING until the handshake promotes it to AI or GUI.
 
void setType (ClientType type)
 
int playerId () const
 Player this connection drives (-1 for GUI / not yet assigned).
 
void setPlayerId (int id)
 
bool hasPendingWrite () const
 True if there is still unsent data in the write buffer.
 
 Connection (const Connection &)=delete
 
Connectionoperator= (const Connection &)=delete
 

Private Attributes

int _fd
 
std::string _readBuf
 
std::string _writeBuf
 
ClientType _type = ClientType::PENDING
 
int _playerId = -1
 

Detailed Description

One client socket plus its read/write buffers.

Owns the fd (closed on destruction). Incoming bytes go into the read buffer, then come out one '
' line at a time; outgoing data is queued and flushed when the socket is writable. Also tracks the client kind (PENDING/AI/GUI) and, for an AI, which player it controls. Non-copyable.

Definition at line 17 of file Connection.hpp.

Constructor & Destructor Documentation

◆ Connection() [1/2]

Connection::Connection ( int  fd)
explicit

Definition at line 6 of file Connection.cpp.

◆ ~Connection()

Connection::~Connection ( )

Definition at line 8 of file Connection.cpp.

References _fd.

◆ Connection() [2/2]

Connection::Connection ( const Connection )
delete

Member Function Documentation

◆ appendRead()

void Connection::appendRead ( std::string_view  data)

Append freshly-read bytes to the read buffer.

Definition at line 13 of file Connection.cpp.

References _readBuf.

◆ fd()

int Connection::fd ( ) const

Definition at line 34 of file Connection.cpp.

References _fd.

◆ flushWrite()

void Connection::flushWrite ( )

Try to send the buffered write data over the socket.

Definition at line 27 of file Connection.cpp.

References _fd, and _writeBuf.

◆ hasPendingWrite()

bool Connection::hasPendingWrite ( ) const

True if there is still unsent data in the write buffer.

Definition at line 39 of file Connection.cpp.

References _writeBuf.

◆ nextLine()

std::optional< std::string > Connection::nextLine ( )

Pop one complete '
'-terminated line, or nullopt if none buffered yet.

Definition at line 15 of file Connection.cpp.

References _readBuf.

◆ operator=()

Connection & Connection::operator= ( const Connection )
delete

◆ playerId()

int Connection::playerId ( ) const

◆ queueWrite()

void Connection::queueWrite ( const std::string &  msg)

Append msg to the write buffer (not sent until flushWrite()).

Definition at line 25 of file Connection.cpp.

References _writeBuf.

◆ setPlayerId()

void Connection::setPlayerId ( int  id)

Definition at line 38 of file Connection.cpp.

References _playerId, and id().

Referenced by HandshakeHandler::_promoteToAi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setType()

void Connection::setType ( ClientType  type)

Definition at line 36 of file Connection.cpp.

References _type, and type().

Referenced by HandshakeHandler::_promoteToAi(), and HandshakeHandler::_promoteToGui().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ type()

ClientType Connection::type ( ) const

Client kind: PENDING until the handshake promotes it to AI or GUI.

Definition at line 35 of file Connection.cpp.

References _type.

Referenced by setType().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _fd

int Connection::_fd
private

Definition at line 46 of file Connection.hpp.

Referenced by fd(), flushWrite(), and ~Connection().

◆ _playerId

int Connection::_playerId = -1
private

Definition at line 50 of file Connection.hpp.

Referenced by playerId(), and setPlayerId().

◆ _readBuf

std::string Connection::_readBuf
private

Definition at line 47 of file Connection.hpp.

Referenced by appendRead(), and nextLine().

◆ _type

ClientType Connection::_type = ClientType::PENDING
private

Definition at line 49 of file Connection.hpp.

Referenced by setType(), and type().

◆ _writeBuf

std::string Connection::_writeBuf
private

Definition at line 48 of file Connection.hpp.

Referenced by flushWrite(), hasPendingWrite(), and queueWrite().


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