]> git.tdb.fi Git - libs/net.git/blob - source/socket.cpp
Fix a comparison operator on win32
[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         connected=false;
139         signal_closed.emit();
140
141         delete local_addr;
142         local_addr=0;
143         delete peer_addr;
144         peer_addr=0;
145 }
146
147 const SockAddr &Socket::get_local_address() const
148 {
149         if(local_addr==0)
150                 throw InvalidState("Local address not set");
151         return *local_addr;
152 }
153
154 const SockAddr &Socket::get_peer_address() const
155 {
156         if(peer_addr==0)
157                 throw InvalidState("Peer address not set");
158         return *peer_addr;
159 }
160
161 void Socket::check_state(bool conn) const
162 {
163         if(handle==MSP_NET_INVALID_SOCKET_HANDLE)
164                 throw Exception("Socket is closed");
165         if(conn && !connected)
166                 throw Exception("Socket is not connected");
167 }
168
169 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen)
170 {
171 #ifdef WIN32
172         return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
173 #else
174         return getsockopt(handle, level, optname, optval, optlen);
175 #endif
176 }
177
178 unsigned Socket::do_write(const char *buf, unsigned size)
179 {
180         check_state(true);
181
182         if(size==0)
183                 return 0;
184
185         int ret=::send(handle, buf, size, 0);
186         if(ret<0)
187         {
188                 if(errno==EAGAIN)
189                         return 0;
190                 else
191                         throw SystemError("Writing to socket failed", errno);
192         }
193
194         return ret;
195 }
196
197 unsigned Socket::do_read(char *buf, unsigned size)
198 {
199         check_state(true);
200
201         if(size==0)
202                 return 0;
203
204         int ret=::recv(handle, buf, size, 0);
205         if(ret<0)
206         {
207                 if(errno==EAGAIN)
208                         return 0;
209                 else
210                         throw SystemError("Reading from socket failed", errno);
211         }
212         else if(ret==0 && !eof_flag)
213         {
214                 eof_flag=true;
215                 signal_end_of_file.emit();
216                 set_events(IO::P_NONE);
217         }
218
219         return ret;
220 }
221
222 } // namespace Net
223 } // namespace Msp