]> git.tdb.fi Git - libs/net.git/blob - source/socket.cpp
Reorder class members
[libs/net.git] / source / socket.cpp
1 /* $Id$
2
3 This file is part of libmspnet
4 Copyright © 2008  Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifndef WIN32
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <sys/socket.h>
12 #endif
13 #include <iostream>
14 #include <msp/strings/formatter.h>
15 #include <msp/time/units.h>
16 #include "socket.h"
17
18 namespace {
19
20 #ifdef WIN32
21 class WinSockHelper
22 {
23 public:
24         WinSockHelper()
25         {
26                 WSADATA wsa_data;
27                 int err=WSAStartup(0x0002, &wsa_data);
28                 if(err)
29                         std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
30         }
31
32         ~WinSockHelper()
33         {
34                 WSACleanup();
35         }
36 };
37
38 WinSockHelper wsh;
39 #endif
40
41 }
42
43 namespace Msp {
44 namespace Net {
45
46 Socket::Socket(SocketHandle h, const SockAddr &paddr):
47         handle(h),
48         connected(true),
49         local_addr(0),
50         peer_addr(paddr.copy())
51 {
52         sockaddr sa;
53         socklen_t size=sizeof(sockaddr);
54         getsockname(handle, &sa, &size);
55         local_addr=SockAddr::create(sa);
56
57 #ifdef WIN32
58         event=CreateEvent(0, false, false, 0);
59 #endif
60 }
61
62 Socket::Socket(Family af, int type, int proto):
63         connected(false),
64         local_addr(0),
65         peer_addr(0)
66 {
67         handle=socket(af, type, proto);
68
69 #ifdef WIN32
70         event=CreateEvent(0, false, false, 0);
71 #endif
72 }
73
74 Socket::~Socket()
75 {
76         close();
77 }
78
79 void Socket::set_block(bool b)
80 {
81         mode=(mode&~IO::M_NONBLOCK);
82         if(b)
83                 mode=(mode|IO::M_NONBLOCK);
84
85 #ifdef WIN32
86         u_long flag=!b;
87         ioctlsocket(handle, FIONBIO, &flag);
88 #else
89         int flags=fcntl(handle, F_GETFL);
90         fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK));
91 #endif
92 }
93
94 IO::Handle Socket::get_event_handle()
95 {
96 #ifdef WIN32
97         return event;
98 #else
99         return handle;
100 #endif
101 }
102
103
104 void Socket::bind(const SockAddr &addr)
105 {
106         check_state(false);
107
108         sockaddr sa;
109         unsigned size=addr.fill_sockaddr(sa);
110
111         int err=::bind(handle, &sa, size);
112         if(err==-1)
113                 throw SystemError("Unable to bind", errno);
114
115         delete local_addr;
116         local_addr=addr.copy();
117 }
118
119 /**
120 Closes the socket.  Most operations on the socket will throw an exception after
121 this.
122 */
123 void Socket::close()
124 {
125         if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
126                 return;
127
128         set_events(IO::P_NONE);
129
130         signal_flush_required.emit();
131 #ifdef WIN32
132         closesocket(handle);
133         CloseHandle(event);
134 #else
135         ::close(handle);
136 #endif
137         handle=MSP_NET_INVALID_SOCKET_HANDLE;
138         signal_closed.emit();
139
140         delete local_addr;
141         local_addr=0;
142         delete peer_addr;
143         peer_addr=0;
144 }
145
146 const SockAddr &Socket::get_local_address() const
147 {
148         if(local_addr==0)
149                 throw InvalidState("Local address not set");
150         return *local_addr;
151 }
152
153 const SockAddr &Socket::get_peer_address() const
154 {
155         if(peer_addr==0)
156                 throw InvalidState("Peer address not set");
157         return *peer_addr;
158 }
159
160 void Socket::check_state(bool conn) const
161 {
162         if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
163                 throw Exception("Socket is closed");
164         if(conn && !connected)
165                 throw Exception("Socket is not connected");
166 }
167
168 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen)
169 {
170 #ifdef WIN32
171         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
172 #else
173         return getsockopt(handle, level, optname, optval, optlen);
174 #endif
175 }
176
177 unsigned Socket::do_write(const char *buf, unsigned size)
178 {
179         check_state(true);
180
181         if(size==0)
182                 return 0;
183
184         int ret=::send(handle, buf, size, 0);
185         if(ret<0)
186         {
187                 if(errno==EAGAIN)
188                         return 0;
189                 else
190                         throw SystemError("Writing to socket failed", errno);
191         }
192
193         return ret;
194 }
195
196 unsigned Socket::do_read(char *buf, unsigned size)
197 {
198         check_state(true);
199
200         if(size==0)
201                 return 0;
202
203         int ret=::recv(handle, buf, size, 0);
204         if(ret<0)
205         {
206                 if(errno==EAGAIN)
207                         return 0;
208                 else
209                         throw SystemError("Reading from socket failed", errno);
210         }
211         else if(ret==0 && !eof_flag)
212         {
213                 eof_flag=true;
214                 signal_end_of_file.emit();
215                 set_events(IO::P_NONE);
216         }
217
218         return ret;
219 }
220
221 } // namespace Net
222 } // namespace Msp