Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5#include <string_view>
6
7#include "ClientType.hpp"
8
18 public:
19 explicit Connection(int fd);
21
23 void appendRead(std::string_view data);
25 std::optional<std::string> nextLine();
26
28 void queueWrite(const std::string& msg);
30 void flushWrite();
31
32 int fd() const;
34 ClientType type() const;
37 int playerId() const;
38 void setPlayerId(int id);
40 bool hasPendingWrite() const;
41
42 Connection(const Connection&) = delete;
43 Connection& operator=(const Connection&) = delete;
44
45 private:
46 int _fd;
47 std::string _readBuf;
48 std::string _writeBuf;
50 int _playerId = -1;
51};
Enumeration for client types in the server.
ClientType
Definition ClientType.hpp:7
One client socket plus its read/write buffers.
void flushWrite()
Try to send the buffered write data over the socket.
void setType(ClientType type)
void queueWrite(const std::string &msg)
Append msg to the write buffer (not sent until flushWrite()).
bool hasPendingWrite() const
True if there is still unsent data in the write buffer.
std::optional< std::string > nextLine()
Pop one complete ' '-terminated line, or nullopt if none buffered yet.
ClientType type() const
Client kind: PENDING until the handshake promotes it to AI or GUI.
void appendRead(std::string_view data)
Append freshly-read bytes to the read buffer.
std::string _writeBuf
ClientType _type
Connection & operator=(const Connection &)=delete
void setPlayerId(int id)
int fd() const
int playerId() const
Player this connection drives (-1 for GUI / not yet assigned).
Connection(const Connection &)=delete
std::string _readBuf