1 | unit CommTCPClient;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, blcksock, synsock, CommPin, Common, Threading,
|
---|
7 | DateUtils, SpecializedList;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TCommTCPClient = class;
|
---|
11 |
|
---|
12 | TReceiveDataEvent = procedure(Stream: TMemoryStream) of object;
|
---|
13 |
|
---|
14 | { TCommSocketReceiveThread }
|
---|
15 |
|
---|
16 | TCommSocketReceiveThread = class(TListedThread)
|
---|
17 | public
|
---|
18 | Parent: TCommTCPClient;
|
---|
19 | Stream: TListByte;
|
---|
20 | procedure Execute; override;
|
---|
21 | constructor Create(CreateSuspended: Boolean;
|
---|
22 | const StackSize: SizeUInt = DefaultStackSize);
|
---|
23 | destructor Destroy; override;
|
---|
24 | end;
|
---|
25 |
|
---|
26 | { TCommTCPClient }
|
---|
27 |
|
---|
28 | TCommTCPClient = class(TCommNode)
|
---|
29 | private
|
---|
30 | FActive: Boolean;
|
---|
31 | //FOnReceiveData: TReceiveDataEvent;
|
---|
32 | FReceiveThread: TCommSocketReceiveThread;
|
---|
33 | procedure ReceiveData(Sender: TCommPin; Stream: TListByte);
|
---|
34 | protected
|
---|
35 | procedure SetActive(const AValue: Boolean); override;
|
---|
36 | procedure AssignTo(Dest: TPersistent); override;
|
---|
37 | public
|
---|
38 | Socket: TTCPBlockSocket;
|
---|
39 | Pin: TCommPin;
|
---|
40 | Address: string;
|
---|
41 | Port: Word;
|
---|
42 | property Active: Boolean read FActive write SetActive;
|
---|
43 | constructor Create(AOwner: TComponent); override;
|
---|
44 | destructor Destroy; override;
|
---|
45 | end;
|
---|
46 |
|
---|
47 |
|
---|
48 | implementation
|
---|
49 |
|
---|
50 | resourcestring
|
---|
51 | SCantConnectToServer = 'Can''t connect to remote server';
|
---|
52 |
|
---|
53 | { TCommTCPClient }
|
---|
54 |
|
---|
55 | procedure TCommTCPClient.ReceiveData(Sender: TCommPin; Stream: TListByte);
|
---|
56 | var
|
---|
57 | Mem: TMemoryStream;
|
---|
58 | begin
|
---|
59 | if FActive then begin
|
---|
60 | try
|
---|
61 | Mem := TMemoryStream.Create;
|
---|
62 | Stream.WriteToStream(Mem);
|
---|
63 | Mem.Position := 0;
|
---|
64 | Socket.SendStreamRaw(Mem);
|
---|
65 | finally
|
---|
66 | Mem.Free;
|
---|
67 | end;
|
---|
68 | end;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | procedure TCommTCPClient.SetActive(const AValue: Boolean);
|
---|
72 | begin
|
---|
73 | if FActive = AValue then Exit;
|
---|
74 | FActive := AValue;
|
---|
75 |
|
---|
76 | if AValue then begin
|
---|
77 | Socket.Connect(Address, IntToStr(Port));
|
---|
78 | if Socket.LastError <> 0 then begin
|
---|
79 | FActive := False;
|
---|
80 | raise Exception.Create(SCantConnectToServer);
|
---|
81 | end;
|
---|
82 | FReceiveThread := TCommSocketReceiveThread.Create(True);
|
---|
83 |
|
---|
84 | FReceiveThread.FreeOnTerminate := False;
|
---|
85 | FReceiveThread.Parent := Self;
|
---|
86 | FReceiveThread.Start;
|
---|
87 | end else begin
|
---|
88 | Socket.CloseSocket;
|
---|
89 | FReceiveThread.Terminate;
|
---|
90 | FReceiveThread.WaitFor;
|
---|
91 | FreeAndNil(FReceiveThread);
|
---|
92 | end;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | procedure TCommTCPClient.AssignTo(Dest: TPersistent);
|
---|
96 | begin
|
---|
97 | if Dest is TCommTCPClient then begin
|
---|
98 | TCommTCPClient(Dest).Address := Address;
|
---|
99 | TCommTCPClient(Dest).Port := Port;
|
---|
100 | end
|
---|
101 | else inherited;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | constructor TCommTCPClient.Create(AOwner: TComponent);
|
---|
105 | begin
|
---|
106 | inherited;
|
---|
107 | Socket := TTCPBlockSocket.Create;
|
---|
108 | Pin := TCommPin.Create;
|
---|
109 | Pin.OnReceive := ReceiveData;
|
---|
110 | Pin.Node := Self;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | destructor TCommTCPClient.Destroy;
|
---|
114 | begin
|
---|
115 | Active := False;
|
---|
116 | FreeAndNil(Socket);
|
---|
117 | FreeAndNil(Pin);
|
---|
118 | inherited;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | { TCommSocketReceiveThread }
|
---|
122 |
|
---|
123 | procedure TCommSocketReceiveThread.Execute;
|
---|
124 | var
|
---|
125 | InBufferUsed: Integer;
|
---|
126 | Buffer: array of Byte;
|
---|
127 | begin
|
---|
128 | InBufferUsed := 0;
|
---|
129 | with Parent do begin
|
---|
130 | repeat
|
---|
131 | if InBufferUsed = 0 then Sleep(1);
|
---|
132 | //else Yield;
|
---|
133 | if Assigned(Socket) then
|
---|
134 | with Socket do
|
---|
135 | if CanRead(0) then begin
|
---|
136 | InBufferUsed := WaitingData;
|
---|
137 | if InBufferUsed > 0 then begin
|
---|
138 | SetLength(Buffer, InBufferUsed);
|
---|
139 | RecvBuffer(Buffer, Length(Buffer));
|
---|
140 |
|
---|
141 | Stream.Count := Length(Buffer);
|
---|
142 | Stream.ReplaceBuffer(0, Pointer(Buffer)^, Length(Buffer));
|
---|
143 | Pin.Send(Stream);
|
---|
144 | end else InBufferUsed := 0;
|
---|
145 | end else InBufferUsed := 0;
|
---|
146 | until Terminated;
|
---|
147 | end;
|
---|
148 | end;
|
---|
149 |
|
---|
150 | constructor TCommSocketReceiveThread.Create(CreateSuspended: Boolean;
|
---|
151 | const StackSize: SizeUInt);
|
---|
152 | begin
|
---|
153 | inherited;
|
---|
154 | Stream := TListByte.Create;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | destructor TCommSocketReceiveThread.Destroy;
|
---|
158 | begin
|
---|
159 | FreeAndNil(Stream);
|
---|
160 | inherited;
|
---|
161 | end;
|
---|
162 |
|
---|
163 | end.
|
---|
164 |
|
---|