-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.hx
153 lines (126 loc) · 3.68 KB
/
Socket.hx
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import js.node.Buffer;
import js.node.net.Socket;
import js.node.net.Server;
import js.node.Net;
private class SocketInput extends haxe.io.BytesInput {
public function new(b:haxe.io.Bytes) {
super(b);
this.bigEndian = true;
}
public var available(get, never) : Int;
function get_available() {
return length-position;
}
}
private class SocketOutput extends haxe.io.Output {
var s : js.node.net.Socket;
public function new(s) {
this.s = s;
}
public function wait() {
s.cork();
}
override public function flush() {
s.uncork();
}
override function writeByte( c : Int ) {
var bf = Buffer.alloc(1);
bf.fill(c);
s.write(bf);
}
override function write( b : haxe.io.Bytes ) {
var bf = Buffer.hxFromBytes(b);
s.write(bf);
}
override function writeBytes( b : haxe.io.Bytes, pos : Int, len : Int ) : Int {
if( len > 0 ) {
var bf = Buffer.hxFromBytes(b);
s.write(bf);
}
return len;
}
override function writeInt32( i : Int ) {
var bf = Buffer.alloc(4);
bf.writeInt32BE(i,0);
s.write(bf);
}
override function writeString( str : String, ?encoding:haxe.io.Encoding) {
var bf = Buffer.from(str);
s.write(bf);
}
}
class Socket {
static var openedSocks = [];
var client : Bool;
var s : js.node.net.Socket;
var serv : Server;
public var out(default, null) : SocketOutput;
public var input(default, null) : SocketInput;
public var timeout(default, set) : Null<Float>;
public function new() {
client = true;
}
public function set_timeout(t:Null<Float>) {
if( s != null ) s.setTimeout(t == null ? 0x7FFFFFFF : Math.ceil(t * 1000));
return this.timeout = t;
}
public function connect( host : String, port : Int, onConnect : Void -> Void ) {
s = new js.node.net.Socket();
s.connect({host:host,port:port}, function() {
out = new SocketOutput(s);
bindEvents();
onConnect();
});
}
function bindEvents() {
s.on(SocketEvent.Error, function(e:js.lib.Error) {
onError(e.message);
});
s.on(SocketEvent.Data, function(data:Buffer) {
input = new SocketInput(data.hxToBytes());
onData();
});
s.on(SocketEvent.End, function() {
close();
onError("Closed");
});
}
public static inline var ALLOW_BIND = true;
public function bind( host : String, port : Int, onConnect : Socket -> Void, listenCount = 5 ) {
client = false;
close();
openedSocks.push(this);
serv = Net.createServer(function(sock) {
var s = new Socket();
s.s = sock;
s.bindEvents();
s.out = new SocketOutput(sock);
openedSocks.push(s);
onConnect(s);
});
try serv.listen(port, host) catch( e : Dynamic ) {
close();
throw e;
};
serv.maxConnections = listenCount;
}
public function close() {
openedSocks.remove(this);
if (client) {
if( s != null ) {
try s.end() catch( e : Dynamic ) { };
s = null;
}
} else {
if( serv != null ) {
try serv.close() catch( e : Dynamic ) { };
serv = null;
}
}
}
public dynamic function onError(msg:String) {
throw "Socket Error " + msg;
}
public dynamic function onData() {
}
}