4 #include <sys/socket.h>
7 #include <msp/strings/format.h>
8 #include <msp/time/rawtime_private.h>
9 #include <msp/time/units.h>
21 int err = WSAStartup(0x0002, &wsa_data);
23 std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
40 Socket::Socket(SocketHandle h, const SockAddr &paddr):
44 peer_addr(paddr.copy())
47 socklen_t size = sizeof(sockaddr_storage);
48 getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
49 local_addr = SockAddr::create(sa);
52 event = CreateEvent(0, false, false, 0);
56 Socket::Socket(Family af, int type, int proto):
61 handle = socket(af, type, proto);
64 event = CreateEvent(0, false, false, 0);
73 void Socket::set_block(bool b)
75 mode = (mode&~IO::M_NONBLOCK);
77 mode = (mode|IO::M_NONBLOCK);
81 ioctlsocket(handle, FIONBIO, &flag);
83 int flags = fcntl(handle, F_GETFL);
84 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
88 IO::Handle Socket::get_event_handle()
98 void Socket::bind(const SockAddr &addr)
103 unsigned size = addr.fill_sockaddr(sa);
105 int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
107 throw SystemError("Unable to bind", errno);
110 local_addr = addr.copy();
115 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
118 set_events(IO::P_NONE);
120 signal_flush_required.emit();
127 handle = MSP_NET_INVALID_SOCKET_HANDLE;
129 signal_closed.emit();
137 void Socket::set_timeout(const Time::TimeDelta &timeout)
140 timeval tv = Time::rawtime_to_timeval(timeout.raw());
141 set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
142 set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
144 DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
145 set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
146 set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
150 const SockAddr &Socket::get_local_address() const
153 throw InvalidState("Local address not set");
157 const SockAddr &Socket::get_peer_address() const
160 throw InvalidState("Peer address not set");
164 void Socket::check_state(bool conn) const
166 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
167 throw Exception("Socket is closed");
168 if(conn && !connected)
169 throw Exception("Socket is not connected");
172 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
175 return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
177 return setsockopt(handle, level, optname, optval, optlen);
181 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
184 return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
186 return getsockopt(handle, level, optname, optval, optlen);
190 unsigned Socket::do_write(const char *buf, unsigned size)
197 int ret = ::send(handle, buf, size, 0);
203 throw SystemError("Writing to socket failed", errno);
209 unsigned Socket::do_read(char *buf, unsigned size)
216 int ret = ::recv(handle, buf, size, 0);
222 throw SystemError("Reading from socket failed", errno);
224 else if(ret==0 && !eof_flag)
227 signal_end_of_file.emit();
228 set_events(IO::P_NONE);