| 1 |
/* |
| 2 |
* Copyright (C)2005-2018 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
package neko.net; |
| 23 |
import sys.net.Socket; |
| 24 |
|
| 25 |
private typedef ServerClient<ClientData> = { |
| 26 |
var sock : Socket; |
| 27 |
var buffer : haxe.io.Bytes; |
| 28 |
var bufbytes : Int; |
| 29 |
var data : ClientData; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
This class enables you to quickly create a custom server that can |
| 34 |
serve several clients in parallel. This server is using a single |
| 35 |
thread and process so the server itself processing is not parallel. |
| 36 |
Non-blocking sockets are used to ensure that a slow client does not |
| 37 |
block the others. |
| 38 |
**/ |
| 39 |
class ServerLoop<ClientData> { |
| 40 |
|
| 41 |
/** |
| 42 |
Each client has an associated buffer. This is the initial buffer size which |
| 43 |
is set to 128 bytes by default. |
| 44 |
**/ |
| 45 |
public static var DEFAULT_BUFSIZE = 128; |
| 46 |
|
| 47 |
/** |
| 48 |
Each client has an associated buffer. This is the maximum buffer size which |
| 49 |
is set to 64K by default. When that size is reached and some data can't be processed, |
| 50 |
the client is disconnected. |
| 51 |
**/ |
| 52 |
public static var MAX_BUFSIZE = (1 << 16); |
| 53 |
|
| 54 |
/** |
| 55 |
This is the value of number client requests that the server socket |
| 56 |
listen for. By default this number is 10 but can be increased for |
| 57 |
servers supporting a large number of simultaneous requests. |
| 58 |
**/ |
| 59 |
public var listenCount : Int; |
| 60 |
|
| 61 |
/** |
| 62 |
See `update`. |
| 63 |
**/ |
| 64 |
public var updateTime : Float; |
| 65 |
|
| 66 |
var newData : Socket -> ClientData; |
| 67 |
var socks : Array<sys.net.Socket>; |
| 68 |
public var clients : List<ClientData>; |
| 69 |
|
| 70 |
/** |
| 71 |
Creates a server instance. The `newData` methods must return |
| 72 |
the data associated with the Client. |
| 73 |
**/ |
| 74 |
public function new( ?newData ) { |
| 75 |
this.newData = if( newData == null ) function(_) { return null; } else newData; |
| 76 |
clients = new List(); |
| 77 |
socks = new Array(); |
| 78 |
listenCount = 10; |
| 79 |
updateTime = 1; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
Closes the client connection and removes it from the client List. |
| 84 |
**/ |
| 85 |
public function closeConnection( s : Socket ) : Bool { |
| 86 |
var cl : ServerClient<ClientData> = untyped s.__client; |
| 87 |
if( cl == null || !clients.remove(cl.data) ) |
| 88 |
return false; |
| 89 |
socks.remove(s); |
| 90 |
try s.close() catch( e : Dynamic ) { }; |
| 91 |
clientDisconnected(cl.data); |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
The `update` method is called after each socket event has been |
| 97 |
processed or when `updateTime` has been reached. It can be used |
| 98 |
to perform time-regular tasks such as pings. By default `updateTime` |
| 99 |
is set to one second. |
| 100 |
**/ |
| 101 |
public function update() { |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
This method is called after a client has been disconnected. |
| 106 |
**/ |
| 107 |
public function clientDisconnected( d : ClientData ) { |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
This method can be used instead of writing directly to the socket. |
| 112 |
It ensures that all the data is correctly sent. If an error occurs |
| 113 |
while sending the data, no exception will occur but the client will |
| 114 |
be gracefully disconnected. |
| 115 |
**/ |
| 116 |
public function clientWrite( s : Socket, buf : haxe.io.Bytes, pos : Int, len : Int ) { |
| 117 |
try { |
| 118 |
while( len > 0 ) { |
| 119 |
var nbytes = s.output.writeBytes(buf,pos,len); |
| 120 |
pos += nbytes; |
| 121 |
len -= nbytes; |
| 122 |
} |
| 123 |
} catch( e : Dynamic ) { |
| 124 |
closeConnection(s); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
This method is called when some data has been read into a Client buffer. |
| 130 |
If the data can be handled, then you can return the number of bytes handled |
| 131 |
that needs to be removed from the buffer. It the data can't be handled (some |
| 132 |
part of the message is missing for example), returns 0. |
| 133 |
**/ |
| 134 |
public function processClientData( d : ClientData, buf : haxe.io.Bytes, bufpos : Int, buflen : Int ) { |
| 135 |
throw "ServerLoop::processClientData is not implemented"; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
Called when an error occurred. This enable you to log the error somewhere. |
| 141 |
By default the error is displayed using `trace`. |
| 142 |
**/ |
| 143 |
public function onError( e : Dynamic ) { |
| 144 |
trace(Std.string(e)+"\n"+haxe.CallStack.toString(haxe.CallStack.exceptionStack())); |
| 145 |
} |
| 146 |
|
| 147 |
function readData( cl : ServerClient<ClientData> ) { |
| 148 |
var buflen = cl.buffer.length; |
| 149 |
// eventually double the buffer size |
| 150 |
if( cl.bufbytes == buflen ) { |
| 151 |
var nsize = buflen * 2; |
| 152 |
if( nsize > MAX_BUFSIZE ) { |
| 153 |
if( buflen == MAX_BUFSIZE ) |
| 154 |
throw "Max buffer size reached"; |
| 155 |
nsize = MAX_BUFSIZE; |
| 156 |
} |
| 157 |
var buf2 = haxe.io.Bytes.alloc(nsize); |
| 158 |
buf2.blit(0,cl.buffer,0,buflen); |
| 159 |
buflen = nsize; |
| 160 |
cl.buffer = buf2; |
| 161 |
} |
| 162 |
// read the available data |
| 163 |
var nbytes = cl.sock.input.readBytes(cl.buffer,cl.bufbytes,buflen - cl.bufbytes); |
| 164 |
cl.bufbytes += nbytes; |
| 165 |
} |
| 166 |
|
| 167 |
function processData( cl : ServerClient<ClientData> ) { |
| 168 |
var pos = 0; |
| 169 |
while( cl.bufbytes > 0 ) { |
| 170 |
var nbytes = processClientData(cl.data,cl.buffer,pos,cl.bufbytes); |
| 171 |
if( nbytes == 0 ) |
| 172 |
break; |
| 173 |
pos += nbytes; |
| 174 |
cl.bufbytes -= nbytes; |
| 175 |
} |
| 176 |
if( pos > 0 ) |
| 177 |
cl.buffer.blit(0,cl.buffer,pos,cl.bufbytes); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
Run the server. This function should never return. |
| 182 |
**/ |
| 183 |
public function run( host : sys.net.Host, port : Int ) { |
| 184 |
var serv = new Socket(); |
| 185 |
serv.bind(host,port); |
| 186 |
serv.listen(listenCount); |
| 187 |
socks = [serv]; |
| 188 |
while( true ) { |
| 189 |
for( s in Socket.select(socks,null,null,updateTime).read ) { |
| 190 |
var cl : ServerClient<ClientData> = untyped s.__client; |
| 191 |
if( cl == null ) { |
| 192 |
// no associated client : it's our server socket |
| 193 |
var sock = serv.accept(); |
| 194 |
sock.setBlocking(true); |
| 195 |
cl = { |
| 196 |
sock : sock, |
| 197 |
data : null, |
| 198 |
buffer : haxe.io.Bytes.alloc(DEFAULT_BUFSIZE), |
| 199 |
bufbytes : 0, |
| 200 |
}; |
| 201 |
// bind the client |
| 202 |
untyped sock.__client = cl; |
| 203 |
// creates the data |
| 204 |
try { |
| 205 |
cl.data = newData(sock); |
| 206 |
} catch( e : Dynamic ) { |
| 207 |
onError(e); |
| 208 |
try sock.close() catch( e : Dynamic ) { }; |
| 209 |
continue; |
| 210 |
} |
| 211 |
// adds the client to the lists |
| 212 |
socks.push(sock); |
| 213 |
clients.add(cl.data); |
| 214 |
} else { |
| 215 |
// read & process the data |
| 216 |
try { |
| 217 |
readData(cl); |
| 218 |
processData(cl); |
| 219 |
} catch( e : Dynamic ) { |
| 220 |
if( !Std.is(e,haxe.io.Eof) ) |
| 221 |
onError(e); |
| 222 |
closeConnection(cl.sock); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
update(); |
| 227 |
} |
| 228 |
serv.close(); |
| 229 |
} |
| 230 |
|
| 231 |
} |