4 #include <sys/socket.h>
7 #include <msp/strings/formatter.h>
8 #include <msp/time/units.h>
20 int err=WSAStartup(0x0002, &wsa_data);
22 std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
39 Socket::Socket(SocketHandle h, const SockAddr &paddr):
43 peer_addr(paddr.copy())
46 socklen_t size=sizeof(sockaddr_storage);
47 getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
48 local_addr=SockAddr::create(sa);
51 event=CreateEvent(0, false, false, 0);
55 Socket::Socket(Family af, int type, int proto):
60 handle=socket(af, type, proto);
63 event=CreateEvent(0, false, false, 0);
72 void Socket::set_block(bool b)
74 mode=(mode&~IO::M_NONBLOCK);
76 mode=(mode|IO::M_NONBLOCK);
80 ioctlsocket(handle, FIONBIO, &flag);
82 int flags=fcntl(handle, F_GETFL);
83 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
87 IO::Handle Socket::get_event_handle()
97 void Socket::bind(const SockAddr &addr)
102 unsigned size=addr.fill_sockaddr(sa);
104 int err=::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
106 throw SystemError("Unable to bind", errno);
109 local_addr=addr.copy();
113 Closes the socket. Most operations on the socket will throw an exception after
118 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
121 set_events(IO::P_NONE);
123 signal_flush_required.emit();
130 handle=MSP_NET_INVALID_SOCKET_HANDLE;
132 signal_closed.emit();
140 void Socket::set_timeout(const Time::TimeDelta &timeout)
144 timeout.fill_timeval(tv);
145 set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
146 set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
148 DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
149 set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
150 set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
154 const SockAddr &Socket::get_local_address() const
157 throw InvalidState("Local address not set");
161 const SockAddr &Socket::get_peer_address() const
164 throw InvalidState("Peer address not set");
168 void Socket::check_state(bool conn) const
170 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
171 throw Exception("Socket is closed");
172 if(conn && !connected)
173 throw Exception("Socket is not connected");
176 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
179 return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
181 return setsockopt(handle, level, optname, optval, optlen);
185 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
188 return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
190 return getsockopt(handle, level, optname, optval, optlen);
194 unsigned Socket::do_write(const char *buf, unsigned size)
201 int ret=::send(handle, buf, size, 0);
207 throw SystemError("Writing to socket failed", errno);
213 unsigned Socket::do_read(char *buf, unsigned size)
220 int ret=::recv(handle, buf, size, 0);
226 throw SystemError("Reading from socket failed", errno);
228 else if(ret==0 && !eof_flag)
231 signal_end_of_file.emit();
232 set_events(IO::P_NONE);