Mercurial > projects > dreactor
comparison asyncdreactor/transport/AsyncSocketConduit.d @ 11:5836613d16ac
reorg! reorg!
author | rick@minifunk |
---|---|
date | Tue, 12 Aug 2008 16:59:56 -0400 |
parents | dreactor/transport/AsyncSocketConduit.d@e3dbc9208822 |
children |
comparison
equal
deleted
inserted
replaced
10:e75a2e506b1d | 11:5836613d16ac |
---|---|
1 /******************************************************************************* | |
2 | |
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved | |
4 | |
5 license: BSD style: $(LICENSE) | |
6 | |
7 version: Mar 2004 : Initial release | |
8 version: Jan 2005 : RedShodan patch for timeout query | |
9 version: Dec 2006 : Outback release | |
10 | |
11 author: Kris, modified by Rick Richardson (May 2008) | |
12 | |
13 *******************************************************************************/ | |
14 | |
15 module dreactor.transport.AsyncSocketConduit; | |
16 | |
17 private import tango.time.Time; | |
18 | |
19 public import tango.io.Conduit; | |
20 | |
21 private import tango.net.Socket; | |
22 | |
23 /******************************************************************************* | |
24 | |
25 A wrapper around the bare Socket to implement the IConduit abstraction | |
26 and add socket-specific functionality specifically for multiplexing via | |
27 poll and the ilk. | |
28 | |
29 AsyncSocketConduit data-transfer is typically performed in conjunction with | |
30 an IBuffer, but can happily be handled directly using void array where | |
31 preferred | |
32 | |
33 *******************************************************************************/ | |
34 | |
35 class AsyncSocketConduit : Conduit | |
36 { | |
37 package Socket socket_; | |
38 | |
39 /*********************************************************************** | |
40 | |
41 Create a streaming Internet Socket | |
42 | |
43 ***********************************************************************/ | |
44 | |
45 this () | |
46 { | |
47 this (SocketType.STREAM, ProtocolType.TCP); | |
48 } | |
49 | |
50 /*********************************************************************** | |
51 | |
52 Create an Internet Socket. Used by subclasses and by | |
53 ServerSocket; the latter via method allocate() below | |
54 | |
55 ***********************************************************************/ | |
56 | |
57 protected this (SocketType type, ProtocolType protocol, bool create=true) | |
58 { | |
59 socket_ = new Socket (AddressFamily.INET, type, protocol, create); | |
60 socket_.blocking(false); | |
61 } | |
62 | |
63 /*********************************************************************** | |
64 | |
65 Return the name of this device | |
66 | |
67 ***********************************************************************/ | |
68 | |
69 override char[] toString() | |
70 { | |
71 return socket.toString; | |
72 } | |
73 | |
74 /*********************************************************************** | |
75 | |
76 Return the socket wrapper | |
77 | |
78 ***********************************************************************/ | |
79 | |
80 Socket socket () | |
81 { | |
82 return socket_; | |
83 } | |
84 | |
85 /*********************************************************************** | |
86 | |
87 Return a preferred size for buffering conduit I/O | |
88 | |
89 ***********************************************************************/ | |
90 | |
91 override uint bufferSize () | |
92 { | |
93 return 1024 * 8; | |
94 } | |
95 | |
96 /*********************************************************************** | |
97 | |
98 Models a handle-oriented device. | |
99 | |
100 TODO: figure out how to avoid exposing this in the general | |
101 case | |
102 | |
103 ***********************************************************************/ | |
104 | |
105 override Handle fileHandle () | |
106 { | |
107 return cast(Handle) socket_.fileHandle; | |
108 } | |
109 | |
110 /*********************************************************************** | |
111 | |
112 Is this socket still alive? | |
113 | |
114 ***********************************************************************/ | |
115 | |
116 override bool isAlive () | |
117 { | |
118 return socket_.isAlive; | |
119 } | |
120 | |
121 /*********************************************************************** | |
122 | |
123 Connect to the provided endpoint | |
124 | |
125 ***********************************************************************/ | |
126 | |
127 AsyncSocketConduit connect (Address addr) | |
128 { | |
129 socket_.connect (addr); | |
130 return this; | |
131 } | |
132 | |
133 /*********************************************************************** | |
134 | |
135 Bind the socket. This is typically used to configure a | |
136 listening socket (such as a server or multicast socket). | |
137 The address given should describe a local adapter, or | |
138 specify the port alone (ADDR_ANY) to have the OS assign | |
139 a local adapter address. | |
140 | |
141 ***********************************************************************/ | |
142 | |
143 AsyncSocketConduit bind (Address address) | |
144 { | |
145 socket_.bind (address); | |
146 return this; | |
147 } | |
148 | |
149 /************************************************************************** | |
150 | |
151 Enable the socket for listening | |
152 | |
153 **************************************************************************/ | |
154 AsyncSocketConduit listen(int backlog = 255) | |
155 { | |
156 socket_.listen(backlog); | |
157 return this; | |
158 } | |
159 | |
160 /*********************************************************************** | |
161 | |
162 Inform other end of a connected socket that we're no longer | |
163 available. In general, this should be invoked before close() | |
164 is invoked | |
165 | |
166 The shutdown function shuts down the connection of the socket: | |
167 | |
168 - stops receiving data for this socket. If further data | |
169 arrives, it is rejected. | |
170 | |
171 - stops trying to transmit data from this socket. Also | |
172 discards any data waiting to be sent. Stop looking for | |
173 acknowledgement of data already sent; don't retransmit | |
174 if any data is lost. | |
175 | |
176 ***********************************************************************/ | |
177 | |
178 AsyncSocketConduit shutdown () | |
179 { | |
180 socket_.shutdown (SocketShutdown.BOTH); | |
181 return this; | |
182 } | |
183 | |
184 /*********************************************************************** | |
185 | |
186 Release this AsyncSocketConduit | |
187 | |
188 Note that one should always disconnect a AsyncSocketConduit | |
189 under normal conditions, and generally invoke shutdown | |
190 on all connected sockets beforehand | |
191 | |
192 ***********************************************************************/ | |
193 | |
194 override void detach () | |
195 { | |
196 socket_.detach; | |
197 } | |
198 | |
199 /*********************************************************************** | |
200 | |
201 Read content from the socket. | |
202 | |
203 Returns the number of bytes read from the socket, or | |
204 IConduit.Eof where there's no more content available | |
205 | |
206 Return IConduit.Eof if there is an error with the socket. | |
207 | |
208 ***********************************************************************/ | |
209 override uint read (void[] dst) | |
210 { | |
211 // invoke the actual read op | |
212 return socket_.receive(dst); | |
213 } | |
214 | |
215 | |
216 /*********************************************************************** | |
217 | |
218 Callback routine to write the provided content to the | |
219 socket. | |
220 ***********************************************************************/ | |
221 | |
222 override uint write (void[] src) | |
223 { | |
224 return socket_.send (src); | |
225 } | |
226 } | |
227 |