source: PinConnection/CommTCPClient.pas

Last change on this file was 575, checked in by chronos, 5 months ago
  • Modified: Remove U prefix from unit names of PinConnection package.
File size: 3.8 KB
Line 
1unit CommTCPClient;
2
3interface
4
5uses
6 Classes, SysUtils, blcksock, synsock, CommPin, Common, Threading,
7 DateUtils, SpecializedList;
8
9type
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
48implementation
49
50resourcestring
51 SCantConnectToServer = 'Can''t connect to remote server';
52
53{ TCommTCPClient }
54
55procedure TCommTCPClient.ReceiveData(Sender: TCommPin; Stream: TListByte);
56var
57 Mem: TMemoryStream;
58begin
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;
69end;
70
71procedure TCommTCPClient.SetActive(const AValue: Boolean);
72begin
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;
93end;
94
95procedure TCommTCPClient.AssignTo(Dest: TPersistent);
96begin
97 if Dest is TCommTCPClient then begin
98 TCommTCPClient(Dest).Address := Address;
99 TCommTCPClient(Dest).Port := Port;
100 end
101 else inherited;
102end;
103
104constructor TCommTCPClient.Create(AOwner: TComponent);
105begin
106 inherited;
107 Socket := TTCPBlockSocket.Create;
108 Pin := TCommPin.Create;
109 Pin.OnReceive := ReceiveData;
110 Pin.Node := Self;
111end;
112
113destructor TCommTCPClient.Destroy;
114begin
115 Active := False;
116 FreeAndNil(Socket);
117 FreeAndNil(Pin);
118 inherited;
119end;
120
121{ TCommSocketReceiveThread }
122
123procedure TCommSocketReceiveThread.Execute;
124var
125 InBufferUsed: Integer;
126 Buffer: array of Byte;
127begin
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;
148end;
149
150constructor TCommSocketReceiveThread.Create(CreateSuspended: Boolean;
151 const StackSize: SizeUInt);
152begin
153 inherited;
154 Stream := TListByte.Create;
155end;
156
157destructor TCommSocketReceiveThread.Destroy;
158begin
159 FreeAndNil(Stream);
160 inherited;
161end;
162
163end.
164
Note: See TracBrowser for help on using the repository browser.