Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ConnectionOrientedMasterSocket.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_Network_ConnectionOrientedMasterSocket_h_
5#define _Stroika_Foundation_IO_Network_ConnectionOrientedMasterSocket_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "ConnectionOrientedStreamSocket.h"
10
12
13 /**
14 * This class is to be used with ConnectionOrientedStreamSocket. You create a ConnectionOrientedMasterSocket, and
15 * Bind () it, and Listen () on it, and the resulting sockets (from Accept()) are of type ConnectionOrientedStreamSocket.
16 *
17 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter">C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter</a>
18 */
19 namespace ConnectionOrientedMasterSocket {
20
21 using namespace Socket;
22
23 class _IRep;
24
25 /**
26 * \par Example Usage
27 * \code
28 * ConnectionOrientedMasterSocket::Ptr ms = ConnectionOrientedMasterSocket::New (SocketAddress::INET, Socket::STREAM);
29 * ms.Bind (addr);
30 * ms.Listen (backlog);
31 * Sequence<ConnectionOrientedMasterSocket::Ptr> l; // cannot do Sequence<ConnectionOrientedMasterSocket> cuz not copyable
32 * l.push_back (ms);
33 * \endcode
34 *
35 * \note Since ConnectionOrientedMasterSocket::Ptr is a smart pointer, the constness of the methods depends on whether they modify the smart pointer itself, not
36 * the underlying thread object.
37 *
38 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter">C++-Standard-Thread-Safety-For-Envelope-But-Ambiguous-Thread-Safety-For-Letter</a>
39 */
40 class Ptr : public Socket::Ptr {
41 private:
42 using inherited = Socket::Ptr;
43
44 public:
45 /**
46 */
47 Ptr () = delete;
48 Ptr (nullptr_t);
49 Ptr (const Ptr& src) = default;
50 Ptr (Ptr&& src) = default;
51 Ptr (shared_ptr<_IRep>&& rep);
52 Ptr (const shared_ptr<_IRep>& rep);
53
54 public:
55 /**
56 */
57 nonvirtual Ptr& operator= (const Ptr& rhs) = default;
58 nonvirtual Ptr& operator= (Ptr&& rhs) = default;
59
60 public:
61 /**
62 * @todo Need timeout on this API? Or global (for instance) timeout?
63 *
64 * throws on error, and otherwise means should call accept
65 */
66 nonvirtual void Listen (unsigned int backlog) const;
67
68 public:
69 /**
70 * After Listen() on a connected socket returns (not throws) - you can call Accept() on tha same
71 * socket to allocate a NEW socket with the new connection stream.
72 *
73 * @todo Need timeout on this API? Or global (for instance) timeout?
74 *
75 * \note ***Cancelation Point***
76 */
78
79 protected:
80 /**
81 */
82 nonvirtual shared_ptr<_IRep> _GetSharedRep () const;
83
84 protected:
85 /**
86 * \pre fRep_ != nullptr
87 */
88 nonvirtual _IRep& _ref () const;
89
90 protected:
91 /**
92 * \pre fRep_ != nullptr
93 */
94 nonvirtual const _IRep& _cref () const;
95 };
96
97 /**
98 */
99 class _IRep : public Socket::_IRep {
100 public:
101 virtual ~_IRep () = default;
102 virtual void Listen (unsigned int backlog) = 0;
103 virtual ConnectionOrientedStreamSocket::Ptr Accept () = 0;
104 };
105
106 /**
107 * \par Example Usage
108 * \code
109 * ConnectionOrientedMasterSocket::Ptr ms = ConnectionOrientedMasterSocket::New (SocketAddress::INET, Socket::STREAM);
110 * ms.Bind (addr);
111 * ms.Listen (backlog);
112 * \endcode
113 *
114 * \note unless you call @Detach() - socket is CLOSED in DTOR of rep, so when final reference goes away
115 *
116 * \note ConnectionOrientedMasterSocket is not copyable, but it can be copied into a ConnectionOrientedMasterSocket::Ptr or
117 * Socket::Ptr. This is critical to save them in a container, for example.
118 */
119 Ptr New (SocketAddress::FamilyType family, Type socketKind, const optional<IPPROTO>& protocol = {});
120
121 /**
122 * This function associates a Platform native socket handle with a Stroika wrapper object.
123 *
124 * Once a PlatformNativeHandle is attached to Socket object, it will be automatically closed
125 * when the last reference to the socket disappears (or when someone calls close).
126 *
127 * To prevent that behavior, you can Detach the PlatformNativeHandle before destroying
128 * the associated Socket object.
129 */
131
132 };
133
134}
135
137
138 // Specialize to override GetSDKPollable
139 template <typename T>
140 struct WaitForIOReady_Traits;
141 template <>
142 struct WaitForIOReady_Traits<IO::Network::ConnectionOrientedMasterSocket::Ptr> {
144 static inline auto GetSDKPollable (const HighLevelType& t)
145 {
146 return t.GetNativeSocket ();
147 }
148 };
149
150}
151
152/*
153 ********************************************************************************
154 ***************************** Implementation Details ***************************
155 ********************************************************************************
156 */
157#include "ConnectionOrientedMasterSocket.inl"
158
159#endif /*_Stroika_Foundation_IO_Network_ConnectionOrientedMasterSocket_h_*/
a smart pointer wrapper (like shared_ptr <_IRep>).
Definition Socket.h:178
nonvirtual PlatformNativeHandle GetNativeSocket() const
Definition Socket.inl:52
FamilyType
Socket address family - also sometimes referred to as domain (argument to ::socket calls it domain)
Ptr New(SocketAddress::FamilyType family, Type socketKind, const optional< IPPROTO > &protocol={})