Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
tcp_listener.h
1#ifndef TCP_LISTENER_H
2#define TCP_LISTENER_H
3
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <array>
7#include "./network_socket.h"
8
9namespace AsyncBsdSocketLib
10{
14 {
15 private:
16 static const int cBacklog{3};
17
18 struct sockaddr_in mAddress;
19 int mConnection;
20
21 public:
22 TcpListener() = delete;
23
26 TcpListener(uint16_t port);
27
31 TcpListener(std::string ipAddress, uint16_t port);
32
33 int Connection() const noexcept override;
34
35 bool TrySetup() noexcept override;
36
39 bool TryAccept() noexcept;
40
45 template <std::size_t N>
46 ssize_t Send(const std::array<uint8_t, N> &buffer) const noexcept
47 {
48 ssize_t _result =
49 send(
50 mConnection,
51 buffer.data(),
52 N,
53 MSG_NOSIGNAL);
54
55 return _result;
56 }
57
63 template <std::size_t N>
64 ssize_t Receive(std::array<uint8_t, N> &buffer) const noexcept
65 {
66 ssize_t _result = recv(mConnection, buffer.data(), N, 0);
67
68 return _result;
69 }
70
74 };
75}
76
77#endif
TCP/IP network socket.
Definition: network_socket.h:13
TCP listener (server) to accept a TcpClient.
Definition: tcp_listener.h:14
ssize_t Send(const std::array< uint8_t, N > &buffer) const noexcept
Send a byte array to the connected client.
Definition: tcp_listener.h:46
TcpListener(std::string ipAddress, uint16_t port)
Constructor.
bool TryAccept() noexcept
Try to accept a client to form a connection.
bool TryMakeConnectionNonblock() noexcept
Try to make the current connection (if exists) non-block.
bool TrySetup() noexcept override
Try to setup the communicator.
TcpListener(uint16_t port)
Constructor to listen on any IP address.
ssize_t Receive(std::array< uint8_t, N > &buffer) const noexcept
Receive a byte array from the connected client.
Definition: tcp_listener.h:64
int Connection() const noexcept override
Connection descriptor for sending and receiving.