]> git.tdb.fi Git - libs/net.git/blob - source/streamlistensocket.cpp
Fix a comparison operator on win32
[libs/net.git] / source / streamlistensocket.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 #include <cerrno>
9 #include <msp/core/refptr.h>
10 #include <msp/strings/formatter.h>
11 #include "streamlistensocket.h"
12 #include "streamsocket.h"
13
14 namespace Msp {
15 namespace Net {
16
17 StreamListenSocket::StreamListenSocket(Family af, int proto):
18         Socket(af, SOCK_STREAM, proto),
19         listening(false)
20 { }
21
22 int StreamListenSocket::connect(const SockAddr &)
23 {
24         throw Exception("Can't connect a listen socket");
25 }
26
27 void StreamListenSocket::listen(const SockAddr &addr, unsigned backlog)
28 {
29         bind(addr);
30
31         int err=::listen(handle, backlog);
32         if(err==-1)
33                 throw SystemError("Unable to listen", errno);
34
35 #ifdef WIN32
36         WSAEventSelect(handle, event, FD_ACCEPT);
37 #endif
38         set_events(IO::P_INPUT);
39
40         listening=true;
41 }
42
43 StreamSocket *StreamListenSocket::accept()
44 {
45         if(!listening)
46                 throw InvalidState("Socket is not listening");
47
48         sockaddr sa;
49         socklen_t size=sizeof(sockaddr);
50         SocketHandle new_h=::accept(handle, &sa, &size);
51
52         RefPtr<SockAddr> paddr=SockAddr::create(sa);
53         return new StreamSocket(new_h, *paddr);
54 }
55
56 } // namespace Net
57 } // namespace Msp