132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: March 2004
|
|
8 Outback release: December 2006
|
|
9
|
|
10 author: Kris
|
|
11
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module tango.net.ServerSocket;
|
|
15
|
|
16 private import tango.net.Socket,
|
|
17 tango.net.SocketConduit;
|
|
18
|
|
19 private import tango.io.model.IConduit;
|
|
20
|
|
21 public import tango.net.InternetAddress;
|
|
22
|
|
23 /*******************************************************************************
|
|
24
|
|
25 ServerSocket is a wrapper upon the basic socket functionality to
|
|
26 simplify the API somewhat. You use a ServerSocket to listen for
|
|
27 inbound connection requests, and get back a SocketConduit when a
|
|
28 connection is made.
|
|
29
|
|
30 Accepted SocketConduit instances are held in a free-list to help
|
|
31 avoid heap activity. These instances are recycled upon invoking
|
|
32 the close() method, and one should ensure that occurs
|
|
33
|
|
34 *******************************************************************************/
|
|
35
|
|
36 class ServerSocket : ISelectable
|
|
37 {
|
|
38 private Socket socket_;
|
|
39 private int linger = -1;
|
|
40
|
|
41 /***********************************************************************
|
|
42
|
|
43 Construct a ServerSocket on the given address, with the
|
|
44 specified number of backlog connections supported. The
|
|
45 socket is bound to the given address, and set to listen
|
|
46 for incoming connections. Note that the socket address
|
|
47 can be setup for reuse, so that a halted server may be
|
|
48 restarted immediately.
|
|
49
|
|
50 ***********************************************************************/
|
|
51
|
|
52 this (InternetAddress addr, int backlog=32, bool reuse=false)
|
|
53 {
|
|
54 socket_ = new Socket (AddressFamily.INET, SocketType.STREAM, ProtocolType.IP);
|
|
55 socket_.setAddressReuse(reuse).bind(addr).listen(backlog);
|
|
56 }
|
|
57
|
|
58 /***********************************************************************
|
|
59
|
|
60 Models a handle-oriented device.
|
|
61
|
|
62 TODO: figure out how to avoid exposing this in the general
|
|
63 case
|
|
64
|
|
65 ***********************************************************************/
|
|
66
|
|
67 Handle fileHandle ()
|
|
68 {
|
|
69 return cast(Handle) socket_.fileHandle;
|
|
70 }
|
|
71
|
|
72 /***********************************************************************
|
|
73
|
|
74 Set the period in which dead sockets are left lying around
|
|
75 by the O/S
|
|
76
|
|
77 ***********************************************************************/
|
|
78
|
|
79 void setLingerPeriod (int period)
|
|
80 {
|
|
81 linger = period;
|
|
82 }
|
|
83
|
|
84 /***********************************************************************
|
|
85
|
|
86 Return the wrapped socket
|
|
87
|
|
88 ***********************************************************************/
|
|
89
|
|
90 Socket socket ()
|
|
91 {
|
|
92 return socket_;
|
|
93 }
|
|
94
|
|
95 /***********************************************************************
|
|
96
|
|
97 Is this server still alive?
|
|
98
|
|
99 ***********************************************************************/
|
|
100
|
|
101 bool isAlive ()
|
|
102 {
|
|
103 return socket_.isAlive;
|
|
104 }
|
|
105
|
|
106 /***********************************************************************
|
|
107
|
|
108 Wait for a client to connect to us, and return a connected
|
|
109 SocketConduit.
|
|
110
|
|
111 ***********************************************************************/
|
|
112
|
|
113 SocketConduit accept ()
|
|
114 {
|
|
115 auto wrapper = SocketConduit.allocate();
|
|
116 auto accepted = socket_.accept (wrapper.socket);
|
|
117
|
|
118 // force abortive closure to avoid prolonged OS scavenging?
|
|
119 if (linger >= 0)
|
|
120 accepted.setLingerPeriod (linger);
|
|
121
|
|
122 return wrapper;
|
|
123 }
|
|
124 }
|