1 | unit CommSocket;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, blcksock, UCommPin, UCommon, UThreading,
|
---|
7 | DateUtils;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TCommSocket = class;
|
---|
11 |
|
---|
12 | TReceiveDataEvent = procedure(Stream: TMemoryStream) of object;
|
---|
13 |
|
---|
14 | { TCommSocketReceiveThread }
|
---|
15 |
|
---|
16 | TCommSocketReceiveThread = class(TListedThread)
|
---|
17 | public
|
---|
18 | Parent: TCommSocket;
|
---|
19 | Stream: TMemoryStream;
|
---|
20 | procedure Execute; override;
|
---|
21 | constructor Create(CreateSuspended: Boolean;
|
---|
22 | const StackSize: SizeUInt = DefaultStackSize);
|
---|
23 | destructor Destroy; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 | { TCommSocket }
|
---|
27 |
|
---|
28 | TCommSocket = class
|
---|
29 | private
|
---|
30 | FActive: Boolean;
|
---|
31 | FOnReceiveData: TReceiveDataEvent;
|
---|
32 | FReceiveThread: TCommSocketReceiveThread;
|
---|
33 | procedure ReceiveData(Sender: TCommPin; Stream: TStream);
|
---|
34 | procedure SetActive(const AValue: Boolean);
|
---|
35 | public
|
---|
36 | Socket: TBlockSocket;
|
---|
37 | Pin: TCommPin;
|
---|
38 | property Active: Boolean read FActive write SetActive;
|
---|
39 | constructor Create;
|
---|
40 | destructor Destroy; override;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | implementation
|
---|
44 |
|
---|
45 | { TCommSocket }
|
---|
46 |
|
---|
47 | procedure TCommSocket.ReceiveData(Sender: TCommPin; Stream:TStream);
|
---|
48 | begin
|
---|
49 | if FActive then Socket.SendStreamRaw(Stream);
|
---|
50 | end;
|
---|
51 |
|
---|
52 | procedure TCommSocket.SetActive(const AValue: Boolean);
|
---|
53 | begin
|
---|
54 | if FActive = AValue then Exit;
|
---|
55 | FActive := AValue;
|
---|
56 |
|
---|
57 | if AValue then begin
|
---|
58 | FReceiveThread := TCommSocketReceiveThread.Create(True);
|
---|
59 | FReceiveThread.FreeOnTerminate := False;
|
---|
60 | FReceiveThread.Parent := Self;
|
---|
61 | FReceiveThread.Start;
|
---|
62 | end else begin
|
---|
63 | FReceiveThread.Terminate;
|
---|
64 | FReceiveThread.WaitFor;
|
---|
65 | FreeAndNil(FReceiveThread);
|
---|
66 | end;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | constructor TCommSocket.Create;
|
---|
70 | begin
|
---|
71 | inherited Create;
|
---|
72 | Socket := TTCPBlockSocket.Create;
|
---|
73 | Pin := TCommPin.Create;
|
---|
74 | Pin.OnReceive := ReceiveData;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | destructor TCommSocket.Destroy;
|
---|
78 | begin
|
---|
79 | Active := False;
|
---|
80 | Socket.Free;
|
---|
81 | Pin.Free;
|
---|
82 | inherited Destroy;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | { TCommSocketReceiveThread }
|
---|
86 |
|
---|
87 | procedure TCommSocketReceiveThread.Execute;
|
---|
88 | var
|
---|
89 | InBufferUsed: Integer;
|
---|
90 | Buffer: array of Byte;
|
---|
91 | begin
|
---|
92 | InBufferUsed := 0;
|
---|
93 | with Parent do repeat
|
---|
94 | if InBufferUsed = 0 then Sleep(1);
|
---|
95 | //else Yield;
|
---|
96 | if Assigned(Socket) then
|
---|
97 | with Socket do
|
---|
98 | if CanRead(0) then begin
|
---|
99 | InBufferUsed := WaitingData;
|
---|
100 | if InBufferUsed > 0 then begin
|
---|
101 | SetLength(Buffer, InBufferUsed);
|
---|
102 | RecvBuffer(Buffer, Length(Buffer));
|
---|
103 |
|
---|
104 | Stream.Size := Length(Buffer);
|
---|
105 | Stream.Position := 0;
|
---|
106 | Stream.Write(Buffer[0], Length(Buffer));
|
---|
107 | Pin.Send(Stream);
|
---|
108 | end else InBufferUsed := 0;
|
---|
109 | end else InBufferUsed := 0;
|
---|
110 | until Terminated;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | constructor TCommSocketReceiveThread.Create(CreateSuspended: Boolean;
|
---|
114 | const StackSize: SizeUInt);
|
---|
115 | begin
|
---|
116 | inherited;
|
---|
117 | Stream := TMemoryStream.Create;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | destructor TCommSocketReceiveThread.Destroy;
|
---|
121 | begin
|
---|
122 | Stream.Free;
|
---|
123 | inherited;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | end.
|
---|
127 |
|
---|