source: PinConnection/CommSocket.pas

Last change on this file was 575, checked in by chronos, 6 months ago
  • Modified: Remove U prefix from unit names of PinConnection package.
File size: 2.9 KB
Line 
1unit CommSocket;
2
3interface
4
5uses
6 Classes, SysUtils, blcksock, UCommPin, UCommon, UThreading,
7 DateUtils;
8
9type
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
43implementation
44
45{ TCommSocket }
46
47procedure TCommSocket.ReceiveData(Sender: TCommPin; Stream:TStream);
48begin
49 if FActive then Socket.SendStreamRaw(Stream);
50end;
51
52procedure TCommSocket.SetActive(const AValue: Boolean);
53begin
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;
67end;
68
69constructor TCommSocket.Create;
70begin
71 inherited Create;
72 Socket := TTCPBlockSocket.Create;
73 Pin := TCommPin.Create;
74 Pin.OnReceive := ReceiveData;
75end;
76
77destructor TCommSocket.Destroy;
78begin
79 Active := False;
80 Socket.Free;
81 Pin.Free;
82 inherited Destroy;
83end;
84
85{ TCommSocketReceiveThread }
86
87procedure TCommSocketReceiveThread.Execute;
88var
89 InBufferUsed: Integer;
90 Buffer: array of Byte;
91begin
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;
111end;
112
113constructor TCommSocketReceiveThread.Create(CreateSuspended: Boolean;
114 const StackSize: SizeUInt);
115begin
116 inherited;
117 Stream := TMemoryStream.Create;
118end;
119
120destructor TCommSocketReceiveThread.Destroy;
121begin
122 Stream.Free;
123 inherited;
124end;
125
126end.
127
Note: See TracBrowser for help on using the repository browser.