Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Socket.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
6
7 /*
8 ********************************************************************************
9 ********************* Foundation::IO::Network::Socket::Ptr *********************
10 ********************************************************************************
11 */
12 inline Socket::Ptr::Ptr (nullptr_t)
13 {
14 }
15 inline Socket::Ptr::Ptr (const shared_ptr<_IRep>& rep)
16 : fRep_{rep}
17 {
18 }
19 inline Socket::Ptr::Ptr (shared_ptr<_IRep>&& rep)
20 : fRep_{move (rep)}
21 {
22 }
23 inline Socket::Ptr& Socket::Ptr::operator= (Ptr&& s) noexcept
24 {
25 fRep_ = move (s.fRep_);
26 return *this;
27 }
28 inline Socket::Ptr& Socket::Ptr::operator= (const Ptr& s)
29 {
30 fRep_ = s.fRep_;
31 return *this;
32 }
33 inline void Socket::Ptr::reset () noexcept
34 {
35 fRep_.reset ();
36 }
37 inline shared_ptr<Socket::_IRep> Socket::Ptr::_GetSharedRep () const
38 {
39 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
40 return fRep_;
41 }
42 inline Socket::_IRep& Socket::Ptr::_ref () const
43 {
44 RequireNotNull (fRep_);
45 return *fRep_;
46 }
47 inline const Socket::_IRep& Socket::Ptr::_cref () const
48 {
49 RequireNotNull (fRep_);
50 return *fRep_;
51 }
52 inline Socket::PlatformNativeHandle Socket::Ptr::GetNativeSocket () const
53 {
54 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
55 return _cref ().GetNativeSocket ();
56 }
57 inline optional<IO::Network::SocketAddress> Socket::Ptr::GetLocalAddress () const
58 {
59 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
60 return _cref ().GetLocalAddress ();
61 }
62 inline SocketAddress::FamilyType Socket::Ptr::GetAddressFamily () const
63 {
64 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
65 return _cref ().GetAddressFamily ();
66 }
67 inline void Socket::Ptr::Shutdown (ShutdownTarget shutdownTarget)
68 {
69 // not important to null-out, but may as well...
70 if (fRep_ != nullptr) {
71 _ref ().Shutdown (shutdownTarget);
72 }
73 }
74 inline void Socket::Ptr::Close () const
75 {
76 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
77 // not important to null-out, but may as well...
78 if (fRep_ != nullptr) {
79 fRep_->Close ();
80 }
81 }
82 template <typename RESULT_TYPE>
83 inline RESULT_TYPE Socket::Ptr::getsockopt (int level, int optname) const
84 {
85 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
86 RESULT_TYPE r{};
87 socklen_t roptlen = sizeof (r);
88 _cref ().getsockopt (level, optname, &r, &roptlen);
89 return r;
90 }
91 template <typename ARG_TYPE>
92 inline void Socket::Ptr::setsockopt (int level, int optname, ARG_TYPE arg) const
93 {
94 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized};
95 socklen_t optvallen = sizeof (arg);
96 _ref ().setsockopt (level, optname, &arg, optvallen);
97 }
98 inline bool Socket::Ptr::operator== (const Ptr& rhs) const
99 {
100 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized}; // nb: not deadlock risk cuz these aren't really mutexes, just checks
101 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext2{rhs._fThisAssertExternallySynchronized};
102 return _GetSharedRep () == rhs._GetSharedRep ();
103 }
104 inline strong_ordering Socket::Ptr::operator<=> (const Ptr& rhs) const
105 {
106 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{_fThisAssertExternallySynchronized}; // nb: not deadlock risk cuz these aren't really mutexes, just checks
107 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext2{rhs._fThisAssertExternallySynchronized};
108 return Common::StdCompat::compare_three_way{}(_GetSharedRep (), rhs._GetSharedRep ());
109 }
110
111#if qStroika_Foundation_Common_Platform_Windows
112 /*
113 ********************************************************************************
114 ******************* ThrowWSASystemErrorIfNegative ******************************
115 ********************************************************************************
116 */
117 template <typename INT_TYPE>
118 inline INT_TYPE ThrowWSASystemErrorIfSOCKET_ERROR (INT_TYPE returnCode)
119 requires (is_signed_v<INT_TYPE>)
120 {
121 if (returnCode == SOCKET_ERROR) {
122 Execution::ThrowSystemErrNo (::WSAGetLastError ());
123 }
124 return returnCode;
125 }
126 // this overload is needed because the winsock type for SOCKET is UNSIGNED so < 0 test doesn't work
127 inline IO::Network::Socket::PlatformNativeHandle ThrowWSASystemErrorIfSOCKET_ERROR (IO::Network::Socket::PlatformNativeHandle returnCode)
128 {
129 // see docs for https://docs.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-accept
130 if (returnCode == INVALID_SOCKET) {
131 Execution::ThrowSystemErrNo (::WSAGetLastError ());
132 }
133 return returnCode;
134 }
135#endif
136
137}
#define RequireNotNull(p)
Definition Assertions.h:347
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
FamilyType
Socket address family - also sometimes referred to as domain (argument to ::socket calls it domain)