4 #include <sys/socket.h>
7 #include <msp/io/handle_private.h>
8 #include <msp/strings/format.h>
9 #include <msp/time/rawtime_private.h>
10 #include <msp/time/units.h>
22 int err = WSAStartup(0x0002, &wsa_data);
24 std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
41 Socket::Socket(SocketHandle h, const SockAddr &paddr):
45 peer_addr(paddr.copy())
48 socklen_t size = sizeof(sockaddr_storage);
49 getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
50 local_addr = SockAddr::create(sa);
53 event = CreateEvent(0, false, false, 0);
59 Socket::Socket(Family af, int type, int proto):
64 handle = socket(af, type, proto);
67 event = CreateEvent(0, false, false, 0);
78 void Socket::set_block(bool b)
80 mode = (mode&~IO::M_NONBLOCK);
82 mode = (mode|IO::M_NONBLOCK);
86 ioctlsocket(handle, FIONBIO, &flag);
88 int flags = fcntl(handle, F_GETFL);
89 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
93 const IO::Handle &Socket::get_event_handle()
99 void Socket::bind(const SockAddr &addr)
104 unsigned size = addr.fill_sockaddr(sa);
106 int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
108 throw SystemError("Unable to bind", errno);
111 local_addr = addr.copy();
116 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
119 set_events(IO::P_NONE);
121 signal_flush_required.emit();
128 handle = MSP_NET_INVALID_SOCKET_HANDLE;
130 signal_closed.emit();
138 void Socket::set_timeout(const Time::TimeDelta &timeout)
141 timeval tv = Time::rawtime_to_timeval(timeout.raw());
142 set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
143 set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
145 DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
146 set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
147 set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
151 const SockAddr &Socket::get_local_address() const
154 throw InvalidState("Local address not set");
158 const SockAddr &Socket::get_peer_address() const
161 throw InvalidState("Peer address not set");
165 void Socket::check_state(bool conn) const
167 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
168 throw Exception("Socket is closed");
169 if(conn && !connected)
170 throw Exception("Socket is not connected");
173 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
176 return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
178 return setsockopt(handle, level, optname, optval, optlen);
182 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
185 return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
187 return getsockopt(handle, level, optname, optval, optlen);
191 unsigned Socket::do_write(const char *buf, unsigned size)
198 int ret = ::send(handle, buf, size, 0);
204 throw SystemError("Writing to socket failed", errno);
210 unsigned Socket::do_read(char *buf, unsigned size)
217 int ret = ::recv(handle, buf, size, 0);
223 throw SystemError("Reading from socket failed", errno);
225 else if(ret==0 && !eof_flag)
228 signal_end_of_file.emit();
229 set_events(IO::P_NONE);