Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
tcp_client.h
1#ifndef TCP_CLIENT_H
2#define TCP_CLIENT_H
3
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <array>
7#include <stdexcept>
8#include "./network_socket.h"
9
10namespace AsyncBsdSocketLib
11{
14 class TcpClient : public NetworkSocket
15 {
16 private:
17 struct sockaddr_in mAddress;
18 bool mIsConnected;
19
20 public:
21 TcpClient() = delete;
22
26 TcpClient(std::string ipAddress, uint16_t port);
27
28 int Connection() const noexcept override;
29
32 bool IsConnected() const noexcept;
33
34 bool TrySetup() noexcept override;
35
38 bool TryConnect() noexcept;
39
44 template <std::size_t N>
45 ssize_t Send(const std::array<uint8_t, N> &buffer) const noexcept
46 {
47 ssize_t _result =
48 send(
50 buffer.data(),
51 N,
52 MSG_NOSIGNAL);
53
54 return _result;
55 }
56
62 template <std::size_t N>
63 ssize_t Receive(std::array<uint8_t, N> &buffer) const noexcept
64 {
65 ssize_t _result = recv(FileDescriptor, buffer.data(), N, 0);
66
67 return _result;
68 }
69 };
70}
71
72#endif
int FileDescriptor
File descriptor.
Definition: communicator.h:13
TCP/IP network socket.
Definition: network_socket.h:13
TCP client to connect to a TcpListener.
Definition: tcp_client.h:15
TcpClient(std::string ipAddress, uint16_t port)
Constructor.
bool TrySetup() noexcept override
Try to setup the communicator.
bool IsConnected() const noexcept
Inidicates whether the client is connected or not.
int Connection() const noexcept override
Connection descriptor for sending and receiving.
ssize_t Send(const std::array< uint8_t, N > &buffer) const noexcept
Send a byte array to the connected client.
Definition: tcp_client.h:45
ssize_t Receive(std::array< uint8_t, N > &buffer) const noexcept
Receive a byte array from the connected client.
Definition: tcp_client.h:63
bool TryConnect() noexcept
Try to connect to the server.