4 #include <sys/socket.h>
7 #include <msp/core/systemerror.h>
8 #include <msp/io/handle_private.h>
9 #include <msp/strings/format.h>
10 #include <msp/time/rawtime_private.h>
11 #include <msp/time/units.h>
23 int err = WSAStartup(0x0002, &wsa_data);
25 std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
42 Socket::Socket(SocketHandle h, const SockAddr &paddr):
46 peer_addr(paddr.copy())
49 socklen_t size = sizeof(sockaddr_storage);
50 getsockname(handle, reinterpret_cast<sockaddr *>(&sa), &size);
51 local_addr = SockAddr::create(sa);
54 event = CreateEvent(0, false, false, 0);
60 Socket::Socket(Family af, int type, int proto):
65 handle = socket(af, type, proto);
68 event = CreateEvent(0, false, false, 0);
79 void Socket::set_block(bool b)
81 mode = (mode&~IO::M_NONBLOCK);
83 mode = (mode|IO::M_NONBLOCK);
87 ioctlsocket(handle, FIONBIO, &flag);
89 int flags = fcntl(handle, F_GETFL);
90 fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
94 const IO::Handle &Socket::get_event_handle()
100 void Socket::bind(const SockAddr &addr)
105 unsigned size = addr.fill_sockaddr(sa);
107 int err = ::bind(handle, reinterpret_cast<sockaddr *>(&sa), size);
109 throw system_error("bind");
112 local_addr = addr.copy();
117 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
120 set_events(IO::P_NONE);
122 signal_flush_required.emit();
129 handle = MSP_NET_INVALID_SOCKET_HANDLE;
131 signal_closed.emit();
139 void Socket::set_timeout(const Time::TimeDelta &timeout)
142 timeval tv = Time::rawtime_to_timeval(timeout.raw());
143 set_option(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(timeval));
144 set_option(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(timeval));
146 DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
147 set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
148 set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
152 const SockAddr &Socket::get_local_address() const
155 throw bad_socket_state("not bound");
159 const SockAddr &Socket::get_peer_address() const
162 throw bad_socket_state("not connected");
166 void Socket::check_state(bool conn) const
168 if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
169 throw bad_socket_state("socket is closed");
170 if(conn && !connected)
171 throw bad_socket_state("not connected");
174 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen)
177 return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
179 return setsockopt(handle, level, optname, optval, optlen);
183 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) const
186 return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
188 return getsockopt(handle, level, optname, optval, optlen);
192 unsigned Socket::do_write(const char *buf, unsigned size)
199 int ret = ::send(handle, buf, size, 0);
205 throw system_error("send");
211 unsigned Socket::do_read(char *buf, unsigned size)
218 int ret = ::recv(handle, buf, size, 0);
224 throw system_error("recv");
226 else if(ret==0 && !eof_flag)
229 signal_end_of_file.emit();
230 set_events(IO::P_NONE);