Changeset 302 for PinConnection/UCommDelay.pas
- Timestamp:
- Dec 5, 2011, 2:20:39 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PinConnection/UCommDelay.pas
r296 r302 24 24 TCommDelayThread = class(TTermThread) 25 25 Parent: TCommDelay; 26 PacketQueue: TListObject; 27 Pin: TCommPin; 28 Lock: TCriticalSection; 26 29 procedure Execute; override; 27 30 end; … … 35 38 PacketQueue1: TListObject; // TListObject<TDelayedPacket> 36 39 PacketQueue2: TListObject; // TListObject<TDelayedPacket> 37 Thread: TCommDelayThread; 40 Thread1: TCommDelayThread; 41 Thread2: TCommDelayThread; 38 42 procedure ReceiveData1(Sender: TCommPin; AStream: TStream); 39 43 procedure ReceiveData2(Sender: TCommPin; AStream: TStream); 40 44 procedure SetActive(AValue: Boolean); 41 45 public 42 Lock: TCriticalSection; 46 Lock1: TCriticalSection; 47 Lock2: TCriticalSection; 43 48 Pin1: TCommPin; 44 49 Pin2: TCommPin; … … 58 63 CurrentTime: TDateTime; 59 64 SendData: TStreamHelper; 65 DoSleep: Boolean; 60 66 begin 61 67 try 62 68 SendData := TStreamHelper.Create; 63 69 repeat 64 with Parent do begin70 DoSleep := True; 65 71 try 66 72 Lock.Acquire; 67 73 CurrentTime := Now; 68 for I := PacketQueue1.Count - 1 downto 0 do 69 if TDelayedPacket(PacketQueue1[I]).ReceiveTime < (CurrentTime - Delay) then begin 74 I := 0; 75 while (I < PacketQueue.Count) do 76 if TDelayedPacket(PacketQueue[I]).ReceiveTime < (CurrentTime - Parent.Delay) then begin 77 DoSleep := False; 70 78 SendData.Clear; 71 SendData.WriteStream(TDelayedPacket(PacketQueue 1[I]).Data, TDelayedPacket(PacketQueue1[I]).Data.Size);72 PacketQueue 1.Delete(I);79 SendData.WriteStream(TDelayedPacket(PacketQueue[I]).Data, TDelayedPacket(PacketQueue[I]).Data.Size); 80 PacketQueue.Delete(I); 73 81 try 74 82 Lock.Release; 75 Pin 1.Send(SendData.Stream);83 Pin.Send(SendData.Stream); 76 84 finally 77 85 Lock.Acquire; 78 86 end; 79 end; 80 81 for I := PacketQueue2.Count - 1 downto 0 do 82 if TDelayedPacket(PacketQueue2[I]).ReceiveTime < (CurrentTime - Delay) then begin 83 SendData.Clear; 84 SendData.WriteStream(TDelayedPacket(PacketQueue2[I]).Data, TDelayedPacket(PacketQueue2[I]).Data.Size); 85 PacketQueue2.Delete(I); 86 try 87 Lock.Release; 88 Pin2.Send(SendData.Stream); 89 finally 90 Lock.Acquire; 91 end; 92 end; 87 end else Inc(I); 93 88 finally 94 89 Lock.Release; 95 90 end; 96 end; 97 if not Terminated then Sleep(1); 91 if not Terminated and DoSleep then Sleep(1); 98 92 until Terminated; 99 93 finally … … 120 114 begin 121 115 try 122 Lock .Acquire;116 Lock2.Acquire; 123 117 if Delay = 0 then Pin2.Send(AStream) 124 118 else … … 128 122 end; 129 123 finally 130 Lock .Release;124 Lock2.Release; 131 125 end; 132 126 end; … … 135 129 begin 136 130 try 137 Lock .Acquire;131 Lock1.Acquire; 138 132 if Delay = 0 then Pin1.Send(AStream) 139 133 else … … 143 137 end; 144 138 finally 145 Lock .Release;139 Lock1.Release; 146 140 end; 147 141 end; … … 152 146 FActive := AValue; 153 147 if AValue then begin 154 Thread := TCommDelayThread.Create(True); 155 Thread.FreeOnTerminate := False; 156 Thread.Parent := Self; 157 Thread.Name := 'CommDelay'; 158 Thread.Start; 148 Thread1 := TCommDelayThread.Create(True); 149 Thread1.FreeOnTerminate := False; 150 Thread1.Parent := Self; 151 Thread1.Name := 'CommDelay1'; 152 Thread1.PacketQueue := PacketQueue1; 153 Thread1.Pin := Pin1; 154 Thread1.Lock := Lock1; 155 Thread1.Start; 156 157 Thread2 := TCommDelayThread.Create(True); 158 Thread2.FreeOnTerminate := False; 159 Thread2.Parent := Self; 160 Thread2.Name := 'CommDelay2'; 161 Thread2.PacketQueue := PacketQueue2; 162 Thread2.Pin := Pin2; 163 Thread2.Lock := Lock2; 164 Thread2.Start; 159 165 end else begin 160 FreeAndNil(Thread); 166 FreeAndNil(Thread1); 167 FreeAndNil(Thread2); 161 168 end; 162 169 end; … … 164 171 constructor TCommDelay.Create; 165 172 begin 166 Lock := TCriticalSection.Create; 173 Lock1 := TCriticalSection.Create; 174 Lock2 := TCriticalSection.Create; 167 175 PacketQueue1 := TListObject.Create; 168 176 PacketQueue2 := TListObject.Create; … … 180 188 PacketQueue1.Free; 181 189 PacketQueue2.Free; 182 Lock.Free; 190 Lock1.Free; 191 Lock2.Free; 183 192 inherited Destroy; 184 193 end;
Note:
See TracChangeset
for help on using the changeset viewer.