Mercurial > code
annotate C++/modules/Socket/SocketTcp.cpp @ 380:06b0f405c58f
Socket: initial support for object-oriented addresses
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 19 Jun 2015 13:56:12 +0200 |
parents | 92457ea8f7e2 |
children |
rev | line source |
---|---|
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * SocketTcp.cpp -- portable C++ socket wrappers |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #include "SocketAddress.h" |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 #include "SocketTcp.h" |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 |
378
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
22 void SocketTcp::listen(int max) |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
23 { |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
24 if (::listen(m_handle, max) == Error) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
25 throw SocketError(SocketError::System, "listen"); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
26 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
28 |
378
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
29 std::unique_ptr<SocketTcp> SocketTcp::accept() |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
30 { |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
31 std::unique_ptr<SocketAddress> dummy; |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
32 |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
33 return accept(dummy); |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
34 } |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
35 |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
36 std::unique_ptr<SocketTcp> SocketTcp::accept(std::unique_ptr<SocketAddress> &info) |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
37 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
38 Socket::Handle handle; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
39 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
40 // Store the information |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
41 sockaddr_storage address; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
42 socklen_t addrlen; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
43 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
44 addrlen = sizeof (sockaddr_storage); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 handle = ::accept(m_handle, reinterpret_cast<sockaddr *>(&address), &addrlen); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
46 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
47 if (handle == Invalid) { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 #if defined(_WIN32) |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
49 int error = WSAGetLastError(); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
51 if (error == WSAEWOULDBLOCK) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
52 throw SocketError(SocketError::WouldBlockRead, "accept", error); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
53 } |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
54 |
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
55 throw SocketError(SocketError::System, "accept", error); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
56 #else |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
57 if (errno == EAGAIN || errno == EWOULDBLOCK) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
58 throw SocketError(SocketError::WouldBlockRead, "accept"); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
59 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
61 throw SocketError(SocketError::System, "accept"); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 #endif |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
63 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 |
380
06b0f405c58f
Socket: initial support for object-oriented addresses
David Demelier <markand@malikania.fr>
parents:
378
diff
changeset
|
65 info = SocketAddress::decode(address, addrlen); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 |
378
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
67 return std::make_unique<SocketTcp>(handle); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
68 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
69 |
378
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
70 void SocketTcp::connect(const std::unique_ptr<SocketAddress> &address) |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
71 { |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
72 if (m_state == SocketState::Connected) { |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
73 return; |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
74 } |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
75 |
378
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
76 auto &sa = address->address(); |
92457ea8f7e2
Socket: switch to more OO
David Demelier <markand@malikania.fr>
parents:
350
diff
changeset
|
77 auto addrlen = address->length(); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
79 if (::connect(m_handle, reinterpret_cast<const sockaddr *>(&sa), addrlen) == Error) { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
80 /* |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
81 * Determine if the error comes from a non-blocking connect that cannot be |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
82 * accomplished yet. |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
83 */ |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
84 #if defined(_WIN32) |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
85 int error = WSAGetLastError(); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
86 |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
87 if (error == WSAEWOULDBLOCK) { |
319 | 88 throw SocketError(SocketError::WouldBlockWrite, "connect", error); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
89 } |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
90 |
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
91 throw SocketError(SocketError::System, "connect", error); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
92 #else |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
93 if (errno == EINPROGRESS) { |
319 | 94 throw SocketError(SocketError::WouldBlockWrite, "connect"); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
95 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
96 |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
97 throw SocketError(SocketError::System, "connect"); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
98 #endif |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
99 } |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
100 |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
101 m_state = SocketState::Connected; |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
102 } |
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
103 |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
104 unsigned SocketTcp::recv(void *data, unsigned dataLen) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
105 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
106 int nbread; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
107 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
108 nbread = ::recv(m_handle, (Socket::Arg)data, dataLen, 0); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
109 if (nbread == Error) { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
110 #if defined(_WIN32) |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
111 int error = WSAGetLastError(); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
112 |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
113 if (error == WSAEWOULDBLOCK) { |
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
114 throw SocketError(SocketError::WouldBlockRead, "recv", error); |
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
115 } |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
116 |
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
117 throw SocketError(SocketError::System, "recv", error); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
118 #else |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
119 if (errno == EAGAIN || errno == EWOULDBLOCK) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
120 throw SocketError(SocketError::WouldBlockRead, "recv"); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
121 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
122 |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
123 throw SocketError(SocketError::System, "recv"); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
124 #endif |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
125 } else if (nbread == 0) { |
316
4c0af1143fc4
Add wait operation (no tests yet)
David Demelier <markand@malikania.fr>
parents:
315
diff
changeset
|
126 m_state = SocketState::Closed; |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
127 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
128 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
129 return (unsigned)nbread; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
130 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
131 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
132 unsigned SocketTcp::send(const void *data, unsigned length) |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
133 { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
134 int nbsent; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
135 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
136 nbsent = ::send(m_handle, (Socket::ConstArg)data, length, 0); |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
137 if (nbsent == Error) { |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
138 #if defined(_WIN32) |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
139 int error = WSAGetLastError(); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
140 |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
141 if (error == WSAEWOULDBLOCK) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
142 throw SocketError(SocketError::WouldBlockWrite, "send", error); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
143 } |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
144 |
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
145 throw SocketError(SocketError::System, "send", error); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
146 #else |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
147 if (errno == EAGAIN || errno == EWOULDBLOCK) { |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
148 throw SocketError(SocketError::WouldBlockWrite, "send"); |
335
486767e1d165
Socket: fix under Windows and add new style
David Demelier <markand@malikania.fr>
parents:
334
diff
changeset
|
149 } |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
150 |
317
890729b8cb60
Reimplement SocketError and add a new Timeout code
David Demelier <markand@malikania.fr>
parents:
316
diff
changeset
|
151 throw SocketError(SocketError::System, "send"); |
315
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
152 #endif |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
153 } |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
154 |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
155 return (unsigned)nbsent; |
c9356cb38c86
Split sockets into SocketTcp and SocketUdp
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
156 } |