]> git.tdb.fi Git - libs/net.git/blob - source/net/streamsocket.cpp
Acutally implement Protocol::get_hash
[libs/net.git] / source / net / streamsocket.cpp
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"
9
10 namespace Msp {
11 namespace Net {
12
13 StreamSocket::StreamSocket(const Private &p, const SockAddr &paddr):
14         ClientSocket(p, paddr)
15 {
16         set_socket_events(S_INPUT);
17 }
18
19 StreamSocket::StreamSocket(Family af, int proto):
20         ClientSocket(af, SOCK_STREAM, proto)
21 { }
22
23 bool StreamSocket::connect(const SockAddr &addr)
24 {
25         if(connected)
26                 throw bad_socket_state("already connected");
27
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);
31         if(!finished)
32         {
33                 connecting = true;
34                 set_socket_events(S_CONNECT);
35         }
36
37         delete peer_addr;
38         peer_addr = addr.copy();
39
40         delete local_addr;
41         SockAddr::SysAddr lsa;
42         getsockname(priv->handle, reinterpret_cast<sockaddr *>(&lsa.addr), &lsa.size);
43         local_addr = SockAddr::new_from_sys(lsa);
44
45         if(finished)
46         {
47                 connected = true;
48                 set_socket_events(S_INPUT);
49                 signal_connect_finished.emit(0);
50         }
51
52         return connected;
53 }
54
55 bool StreamSocket::poll_connect(const Time::TimeDelta &timeout)
56 {
57         if(!connecting)
58                 return false;
59
60         IO::PollEvent res = poll(*this, IO::P_OUTPUT, timeout);
61         if(res&IO::P_OUTPUT)
62         {
63                 connecting = false;
64
65                 int err;
66                 socklen_t len = sizeof(int);
67                 priv->get_option(SOL_SOCKET, SO_ERROR, &err, &len);
68
69                 if(err!=0)
70                 {
71                         set_socket_events(S_NONE);
72                         throw system_error("connect", err);
73                 }
74
75                 set_socket_events(S_INPUT);
76
77                 connected = true;
78         }
79
80         return connected;
81 }
82
83 void StreamSocket::on_event(IO::PollEvent ev)
84 {
85         if((ev&(IO::P_OUTPUT|IO::P_ERROR)) && connecting)
86         {
87                 int err;
88                 socklen_t len = sizeof(err);
89                 priv->get_option(SOL_SOCKET, SO_ERROR, &err, &len);
90
91                 connecting = false;
92                 connected = (err==0);
93                 if(err)
94                 {
95                         system_error exc("connect", err);
96                         signal_connect_finished.emit(&exc);
97                 }
98                 else
99                         signal_connect_finished.emit(0);
100
101                 if(err!=0)
102                 {
103                         delete peer_addr;
104                         peer_addr = 0;
105                 }
106
107                 set_socket_events((err==0) ? S_INPUT : S_NONE);
108         }
109 }
110
111 } // namespace Net
112 } // namespace Msp