1 #include "platform_api.h"
2 #include <msp/core/systemerror.h>
3 #include <msp/io/handle_private.h>
4 #include <msp/io/poll.h>
5 #include <msp/strings/format.h>
6 #include "sockaddr_private.h"
7 #include "socket_private.h"
8 #include "streamsocket.h"
13 StreamSocket::StreamSocket(const Private &p, const SockAddr &paddr):
14 ClientSocket(p, paddr)
16 set_socket_events(S_INPUT);
19 StreamSocket::StreamSocket(Family af, int proto):
20 ClientSocket(af, SOCK_STREAM, proto)
23 bool StreamSocket::connect(const SockAddr &addr)
26 throw bad_socket_state("already connected");
28 SockAddr::SysAddr sa = addr.to_sys();
29 int err = ::connect(priv->handle, reinterpret_cast<const sockaddr *>(&sa.addr), sa.size);
30 bool finished = check_sys_connect_error(err);
34 set_socket_events(S_CONNECT);
38 peer_addr = addr.copy();
41 SockAddr::SysAddr lsa;
42 getsockname(priv->handle, reinterpret_cast<sockaddr *>(&lsa.addr), &lsa.size);
43 local_addr = SockAddr::new_from_sys(lsa);
48 set_socket_events(S_INPUT);
49 signal_connect_finished.emit(0);
55 bool StreamSocket::poll_connect(const Time::TimeDelta &timeout)
60 IO::PollEvent res = poll(*this, IO::P_OUTPUT, timeout);
66 socklen_t len = sizeof(int);
67 priv->get_option(SOL_SOCKET, SO_ERROR, &err, &len);
71 set_socket_events(S_NONE);
72 throw system_error("connect", err);
75 set_socket_events(S_INPUT);
83 void StreamSocket::on_event(IO::PollEvent ev)
85 if((ev&(IO::P_OUTPUT|IO::P_ERROR)) && connecting)
88 socklen_t len = sizeof(err);
89 priv->get_option(SOL_SOCKET, SO_ERROR, &err, &len);
95 system_error exc("connect", err);
96 signal_connect_finished.emit(&exc);
99 signal_connect_finished.emit(0);
107 set_socket_events((err==0) ? S_INPUT : S_NONE);