libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
TCPListenSocket.hxx
1#pragma once
2
3// cosmos
4#include <cosmos/net/ListenSocket.hxx>
5#include <cosmos/net/TCPOptions.hxx>
6#include <cosmos/net/TCPConnection.hxx>
7#include <cosmos/net/traits.hxx>
8
9namespace cosmos {
10
12
22template <SocketFamily FAMILY>
23class COSMOS_API TCPListenSocketT :
24 public ListenSocket {
25public: // types
26
27 using IPAddress = typename FamilyTraits<FAMILY>::Address;
28 using Connection = TCPConnectionT<FAMILY>;
29 static inline constexpr auto TYPE = SocketType::STREAM;
30
31public: // functions
32
33 explicit TCPListenSocketT(const SocketFlags flags = SocketFlags{SocketFlag::CLOEXEC}) :
34 ListenSocket{FAMILY, TYPE, flags} {
35 }
36
37 auto ipOptions() {
38 return typename FamilyTraits<FAMILY>::Options{m_fd};
39 }
40
41 auto ipOptions() const {
42 return typename FamilyTraits<FAMILY>::Options{m_fd};
43 }
44
45 auto tcpOptions() {
46 return TCPOptions{m_fd};
47 }
48
49 auto tcpOptions() const {
50 return TCPOptions{m_fd};
51 }
52
54 void getSockName(IPAddress &addr) {
55 Socket::getSockName(addr);
56 }
57
58 using Socket::listen;
59
60 void bind(const IPAddress &addr) {
61 return Socket::bind(addr);
62 }
63
64 TCPConnectionT<FAMILY> accept(IPAddress *addr = nullptr) {
65 auto fd = Socket::accept(addr);
66 return TCPConnectionT<FAMILY>{fd};
67 }
68};
69
70using TCP4ListenSocket = TCPListenSocketT<SocketFamily::INET>;
71using TCP6ListenSocket = TCPListenSocketT<SocketFamily::INET6>;
72
73} // end ns
void getSockName(IPAddress &addr)
Returns the current address that the socket is bound to, if any.