Mercurial > code
comparison C++/Socket.cpp @ 319:cba77da58496
* Finalize SocketSsl
* Add documentation
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 08 Mar 2015 11:04:01 +0100 |
parents | 890729b8cb60 |
children |
comparison
equal
deleted
inserted
replaced
318:68ae6d7dea1f | 319:cba77da58496 |
---|---|
95 | 95 |
96 /* -------------------------------------------------------- | 96 /* -------------------------------------------------------- |
97 * Socket class | 97 * Socket class |
98 * -------------------------------------------------------- */ | 98 * -------------------------------------------------------- */ |
99 | 99 |
100 #if defined(_WIN32) | |
101 std::mutex Socket::s_mutex; | |
102 std::atomic<bool> Socket::s_initialized{false}; | |
103 #endif | |
104 | |
100 Socket::Socket(int domain, int type, int protocol) | 105 Socket::Socket(int domain, int type, int protocol) |
101 { | 106 { |
102 #if defined(_WIN32) && !defined(SOCKET_NO_WSA_INIT) | 107 #if defined(_WIN32) && !defined(SOCKET_NO_WSA_INIT) |
103 if (!s_initialized) | 108 if (!s_initialized) |
104 initialize(); | 109 initialize(); |
106 | 111 |
107 m_handle = ::socket(domain, type, protocol); | 112 m_handle = ::socket(domain, type, protocol); |
108 | 113 |
109 if (m_handle == Invalid) | 114 if (m_handle == Invalid) |
110 throw SocketError(SocketError::System, "socket"); | 115 throw SocketError(SocketError::System, "socket"); |
116 | |
117 m_state = SocketState::Opened; | |
111 } | 118 } |
112 | 119 |
113 void Socket::bind(const SocketAddress &address) | 120 void Socket::bind(const SocketAddress &address) |
114 { | 121 { |
115 const auto &sa = address.address(); | 122 const auto &sa = address.address(); |
162 | 169 |
163 bool operator<(const Socket &s1, const Socket &s2) | 170 bool operator<(const Socket &s1, const Socket &s2) |
164 { | 171 { |
165 return s1.handle() < s2.handle(); | 172 return s1.handle() < s2.handle(); |
166 } | 173 } |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 #if 0 | |
181 void SocketStandard::tryConnect(Socket &s, const SocketAddress &address, int timeout) | |
182 { | |
183 | |
184 } | |
185 | |
186 unsigned SocketStandard::tryRecvfrom(Socket &s, void *data, unsigned len, SocketAddress &info, int timeout) | |
187 { | |
188 SocketListener listener{{s, Read}}; | |
189 | |
190 listener.select(timeout); | |
191 | |
192 return recvfrom(s, data, len, info); | |
193 } | |
194 | |
195 unsigned SocketStandard::trySendto(Socket &s, const void *data, unsigned len, const SocketAddress &info, int timeout) | |
196 { | |
197 SocketListener listener{{s, Write}}; | |
198 | |
199 listener.select(timeout); | |
200 | |
201 return sendto(s, data, len, info); | |
202 } | |
203 | |
204 Socket Socket::tryAccept(int timeout) | |
205 { | |
206 SocketAddress dummy; | |
207 | |
208 return tryAccept(dummy, timeout); | |
209 } | |
210 | |
211 #endif |