-
Notifications
You must be signed in to change notification settings - Fork 2
/
messageSend.pas
100 lines (90 loc) · 3.27 KB
/
messageSend.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
unit messageSend;
interface
uses IdContext,Classes,IdTCPClient, IdGlobal,windows;
type
tprotocolpackage =packed record
protocolhead1:Char;
protocolhead2:Char;
body:array[0..2048] of char;
end;
procedure sendbuffer(context: TIdTCPClient; buffer: pchar;size: integer; lock:TRTLCriticalSection );
procedure sendstream(context: TIdTCPClient; lock:TRTLCriticalSection; stream: tmemorystream);
procedure ssendto(AContext:tIdContext;protocol:char;messages:pointer;size:integer);
procedure sendchat(AContext:tIdContext;protocol:char;messages:pointer;size:integer);
implementation
uses sys,ygo_server_userinfo;
procedure ssendto(AContext:tIdContext;protocol:char;messages:pointer;size:integer);
var stream:tmemorystream;
protocolpackage:tprotocolpackage;
begin
stream:=tmemorystream.Create;
try
protocolpackage.protocolhead1:=char(0);
protocolpackage.protocolhead2:=protocol;
move(messages^,protocolpackage.body[0],size);
stream.write(protocolpackage,size+2);
stream.Position:=0;
AContext.Connection.IOHandler.Write(byte(stream.size-1));
AContext.Connection.IOHandler.Write(stream,stream.size,false);
finally
stream.Free;
end;
end;
//Öظ´º¯Êý
procedure peersendstream(context: tuserinfo;sender:TIdTCPClient; stream: tmemorystream);
var buff:tidbytes;
begin
stream.Position:=0;
buff:=ToBytes(word(stream.Size));
if not assigned(context) then
begin
sender.Disconnect;
exit;
end;
if not tuserinfo(context).connected then exit;
context.Connection.IOHandler.Write(buff);
context.Connection.IOHandler.Write(stream,stream.Size,false);
end;
//·¢ËÍÁÄÌì
procedure sendchat(AContext:tIdContext;protocol:char;messages:pointer;size:integer);
var stream:tmemorystream;
protocolpackage:tprotocolpackage;
begin
stream:=tmemorystream.Create;
try
protocolpackage.protocolhead1:=protocol;
protocolpackage.protocolhead2:=char(8);
protocolpackage.body[0]:=char(0);
move(messages^,protocolpackage.body[1],size);
stream.write(protocolpackage,size+3);
stream.Position:=0;
peersendstream(tuserinfo(acontext),nil,stream);
finally
stream.Free;
end;
end;
procedure sendstream(context: TIdTCPClient; lock:TRTLCriticalSection; stream: tmemorystream);
var buff:tidbytes;
begin
stream.Position:=0;
buff:=ToBytes(word(stream.Size));
if assigned(context) and context.Connected then
begin
context.IOHandler.Write(buff);
context.IOHandler.Write(stream,stream.Size,false);
end;
end;
procedure sendbuffer(context:TIdTCPClient;buffer:pchar;size:integer;lock:TRTLCriticalSection);
var stream:tmemorystream;
begin
stream:=tmemorystream.Create;
try
stream.write(buffer[0],size);
stream.Position:=0;
if assigned(context) and context.Connected then
sendstream(context,lock,stream);
finally
stream.Free;
end;
end;
end.