Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
TcpSocket.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <exception>
4#include <optional>
5#include <string>
6
10class TcpException : public std::exception {
11 public:
12 TcpException(const std::string& message) : _message(message) {}
13 const char* what() const noexcept override { return _message.c_str(); }
14
15 private:
16 std::string _message;
17};
18
22class TcpSocket {
23 public:
27 ~TcpSocket();
28
35 void connect(const std::string& host, int port);
36
42 void send(const std::string& data);
43
49 std::optional<std::string> recvLine();
50
57 bool poll(int timeout_ms);
58
59 private:
60 int _fd = -1;
61 std::string _recvBuffer;
62};
Exception class for TCP errors.
Definition TcpSocket.hpp:10
const char * what() const noexcept override
Definition TcpSocket.hpp:13
std::string _message
Definition TcpSocket.hpp:16
TcpException(const std::string &message)
Definition TcpSocket.hpp:12
Simple TCP socket wrapper.
Definition TcpSocket.hpp:22
~TcpSocket()
Wraps close().
Definition TcpSocket.cpp:11
std::string _recvBuffer
Definition TcpSocket.hpp:61
std::optional< std::string > recvLine()
Receives a line of text from the socket.
Definition TcpSocket.cpp:95
void send(const std::string &data)
Sends data through the socket.
Definition TcpSocket.cpp:83
bool poll(int timeout_ms)
Polls the socket for incoming data.
void connect(const std::string &host, int port)
Connects to a remote host.
Definition TcpSocket.cpp:16