]> git.tdb.fi Git - libs/net.git/blob - source/net/clientsocket.cpp
Move most platform-specific code into overlay directories
[libs/net.git] / source / net / clientsocket.cpp
1 #include "platform_api.h"
2 #include <msp/core/systemerror.h>
3 #include "clientsocket.h"
4 #include "socket_private.h"
5
6 namespace Msp {
7 namespace Net {
8
9 ClientSocket::ClientSocket(Family af, int type, int proto):
10         Socket(af, type, proto),
11         connecting(false),
12         connected(false),
13         peer_addr(0)
14 { }
15
16 ClientSocket::ClientSocket(const Private &p, const SockAddr &paddr):
17         Socket(p),
18         connecting(false),
19         connected(true),
20         peer_addr(paddr.copy())
21 { }
22
23 ClientSocket::~ClientSocket()
24 {
25         signal_flush_required.emit();
26
27         delete peer_addr;
28 }
29
30 void ClientSocket::shutdown(IO::Mode m)
31 {
32         int how;
33         m = m&IO::M_RDWR;
34 #ifdef _WIN32
35         if(m==IO::M_READ)
36                 how = SD_RECEIVE;
37         else if(m==IO::M_WRITE)
38                 how = SD_SEND;
39         else if(m==IO::M_RDWR)
40                 how = SD_BOTH;
41 #else
42         if(m==IO::M_READ)
43                 how = SHUT_RD;
44         else if(m==IO::M_WRITE)
45                 how = SHUT_WR;
46         else if(m==IO::M_RDWR)
47                 how = SHUT_RDWR;
48 #endif
49         else
50                 return;
51
52         ::shutdown(priv->handle, how);
53         mode = mode&~m;
54 }
55
56 const SockAddr &ClientSocket::get_peer_address() const
57 {
58         if(peer_addr==0)
59                 throw bad_socket_state("not connected");
60         return *peer_addr;
61 }
62
63 unsigned ClientSocket::do_write(const char *buf, unsigned size)
64 {
65         check_access(IO::M_WRITE);
66         if(!connected)
67                 throw bad_socket_state("not connected");
68
69         if(size==0)
70                 return 0;
71
72         return check_sys_error(::send(priv->handle, buf, size, 0), "send");
73 }
74
75 unsigned ClientSocket::do_read(char *buf, unsigned size)
76 {
77         check_access(IO::M_READ);
78         if(!connected)
79                 throw bad_socket_state("not connected");
80
81         if(size==0)
82                 return 0;
83
84         unsigned ret = check_sys_error(::recv(priv->handle, buf, size, 0), "recv");
85         if(ret==0 && !eof_flag)
86         {
87                 eof_flag = true;
88                 signal_end_of_file.emit();
89                 set_socket_events(S_NONE);
90         }
91
92         return ret;
93 }
94
95 } // namespace Net
96 } // namespace Msp