-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathThreadServer.hx
More file actions
100 lines (83 loc) · 2.17 KB
/
ThreadServer.hx
File metadata and controls
100 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package openlife.server;
import openlife.macros.Macro;
import haxe.io.Eof;
import openlife.settings.ServerSettings;
#if (target.threaded)
import haxe.Timer;
import haxe.Exception;
import sys.net.Socket;
import sys.thread.Thread;
import sys.thread.Mutex;
import sys.net.Host;
class ThreadServer {
public var socket:Socket;
public var port:Int = 8005;
public var maxCount:Int = -1;
public var listenCount:Int = 10;
public static inline var setTimeout:Int = 30;
public var server:Server;
public function new(server:Server, port:Int) {
socket = new Socket();
this.port = port;
this.server = server;
}
public function create() {
socket.bind(new Host("0.0.0.0"), port);
trace('listening on port: $port');
socket.listen(listenCount);
while (true) {
Thread.create(connection).sendMessage(socket.accept());
}
}
private function connection() {
var socket:Socket = cast Thread.readMessage(true);
trace('start connection ${Server.server.lastCommand}');
socket.setBlocking(ServerSettings.UseBlockingSockets);
socket.setFastSend(true);
var connection = new Connection(socket, server);
var message:String = "";
var ka:Float = Timer.stamp();
while (connection.running) {
try {
Sys.sleep(ServerSettings.PlayerResponseSleepTime);
message = socket.input.readUntil("#".code);
// trace(message);
ka = Timer.stamp();
} catch (e:Dynamic) {
// error("---STACK---\n" + e.details());
if (e != haxe.io.Error.Blocked) {
if ('$e' == 'Eof') {
trace('Client closed connection / EOF');
}
else trace('WARNING: EXEPTION: ' + e);
Macro.exception(connection.close());
break;
}
else {
if (Timer.stamp() - ka > 20) {
Macro.exception(connection.close());
}
}
}
if (message.length == 0) continue;
if (ServerSettings.debug) {
server.process(connection, message);
message = "";
}
else {
try {
server.process(connection, message);
message = "";
} catch (e:Exception) {
trace(e.details());
error("---STACK---\n" + e.details());
// connection.close();
continue;
}
}
}
trace("end connection");
}
private function error(message:String) {}
}
#end