-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjstcp.pas
183 lines (158 loc) · 3.86 KB
/
jstcp.pas
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
unit jstcp;
interface
uses
windows, System.Win.ScktComp, Generics.Collections, v8, System.SysUtils,
XSuperObject, System.Classes;
function listen(port: integer; eid: string; vm: Pv8Engine): integer;
function close(id: integer): integer;
function kick(id: integer; s: NativeInt): integer;
function write(id: integer; s: NativeInt; data: ansistring): integer;
implementation
type
Tserver = class
private
id: integer;
socket: TServerSocket;
eid: string;
vm: Pv8Engine;
constructor Create(port, id: integer);
destructor free;
procedure ServerSocket1ClientRead(Sender: TObject; socket: TCustomWinSocket);
end;
{
Tclient = class
id: integer;
socket: TClientSocket;
eid: string;
vm: Pv8Engine;
constructor connect(port, id: integer);
destructor free;
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
end;
}
var
id: integer;
servers: TDictionary<integer, Tserver>;
type
TBuff = array of Byte;
procedure Tserver.ServerSocket1ClientRead(Sender: TObject; socket: TCustomWinSocket);
var
X: ISuperObject;
data: UTF8String; // chrome uyumlu
// data:rawbytestring; // judith studio uyumlu
begin
try
if socket.Connected = true then
begin
SetLength(data, socket.ReceiveBuf(Pointer(nil)^, -1));
SetLength(data, socket.ReceiveBuf(Pointer(data)^, Length(data)));
data := trim(data);
end;
finally
X := SO;
X.I['id'] := TServerSocket(Sender).Tag;
X.I['socket'] := socket.SocketHandle;
X.s['ip'] := socket.RemoteAddress;
X.s['data'] := data;
// OutputDebugString(pchar(data));
OutputDebugString(pchar(X.AsJSON(true)));
vm.eval(self.eid + '(' + pchar(X.AsJSON(true)) + ');');
end;
end;
constructor Tserver.Create(port, id: integer);
begin
socket := TServerSocket.Create(nil);
socket.ServerType := stNonBlocking;
// socket.ServerType:=stThreadBlocking;
// socket.ThreadCacheSize := 32;
socket.port := port;
socket.Tag := id;
socket.OnClientRead := ServerSocket1ClientRead;
socket.Active := True;
end;
destructor Tserver.free;
begin
socket.close;
socket.free;
end;
function listen(port: integer; eid: string; vm: Pv8Engine): integer;
var
srv: Tserver;
begin
srv := Tserver.Create(port, id);
srv.eid := eid;
srv.vm := vm;
servers.Add(id, srv);
result := id;
inc(id);
end;
function close(id: integer): integer;
var
srv: Tserver;
begin
if servers.TryGetValue(id, srv) then
begin
try
srv.free;
finally
servers.Remove(id);
end;
end;
end;
function kick(id: integer; s: NativeInt): integer;
var
I: integer;
srv: Tserver;
begin
result := 0;
if servers.TryGetValue(id, srv) then
begin
for I := 0 to srv.socket.socket.ActiveConnections - 1 do
begin
if srv.socket.socket.Connections[I].SocketHandle = s then
begin
try
srv.socket.socket.Connections[I].close;
finally
result := 1
end;
break;
end;
end;
end;
end;
function write(id: integer; s: NativeInt; data: ansistring): integer;
var
I: integer;
srv: Tserver;
begin
result := 0;
if servers.TryGetValue(id, srv) then
begin
for I := 0 to srv.socket.socket.ActiveConnections - 1 do
begin
if srv.socket.socket.Connections[I].SocketHandle = s then
begin
try
result := srv.socket.socket.Connections[I].SendText(data);
finally
end;
break;
end;
end;
end;
end;
procedure freeServers;
var
Item: TPair<integer, Tserver>;
begin
for Item in servers do
Item.Value.free;
servers.Clear;
end;
initialization
servers := TDictionary<integer, Tserver>.Create;
finalization
freeServers;
servers.free;
end.