Changeset 119 for Comm/UPacketBurst.pas
- Timestamp:
- Jan 5, 2011, 1:16:30 PM (14 years ago)
- Location:
- Comm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Comm
-
Property svn:ignore
set to
lib
-
Property svn:ignore
set to
-
Comm/UPacketBurst.pas
r35 r119 1 1 unit UPacketBurst; 2 3 {$mode Delphi}{$H+} 2 4 3 5 interface 4 6 5 7 uses 6 Classes, CommPort, UPin, UMemoryStreamEx, SyncObjs;8 Classes, UCommPin, SyncObjs, UStreamHelper; 7 9 8 10 type … … 19 21 SendThread: TPacketBurstSendThread; 20 22 SendStreamLock: TCriticalSection; 21 SendStream: T MemoryStreamEx;22 ReceiveStream: T MemoryStreamEx;23 procedure PacketSingleReceive(S tream: TStream);24 procedure PacketBurstReceive(S tream: TStream);23 SendStream: TStreamHelper; 24 ReceiveStream: TStreamHelper; 25 procedure PacketSingleReceive(Sender: TCommPin; Stream: TStream); 26 procedure PacketBurstReceive(Sender: TCommPin; Stream: TStream); 25 27 public 26 28 SendPeriod: Integer; // ms 27 29 SendBurstSize: Integer; 28 PacketSinglePin: T Pin;29 PacketBurstPin: T Pin;30 PacketSinglePin: TCommPin; 31 PacketBurstPin: TCommPin; 30 32 destructor Destroy; override; 31 33 constructor Create; … … 38 40 constructor TPacketBurst.Create; 39 41 begin 40 PacketSinglePin := T Pin.Create;42 PacketSinglePin := TCommPin.Create; 41 43 PacketSinglePin.OnReceive := PacketSingleReceive; 42 PacketBurstPin := T Pin.Create;44 PacketBurstPin := TCommPin.Create; 43 45 PacketBurstPin.OnReceive := PacketBurstReceive; 44 46 SendThread := TPacketBurstSendThread.Create(True); 45 47 SendThread.PacketBurst := Self; 46 SendThread. Resume;48 SendThread.Start; 47 49 end; 48 50 … … 55 57 end; 56 58 57 procedure TPacketBurst.PacketBurstReceive(S tream: TStream);59 procedure TPacketBurst.PacketBurstReceive(Sender: TCommPin; Stream: TStream); 58 60 var 59 PacketStream: T MemoryStreamEx;61 PacketStream: TStreamHelper; 60 62 Size: Word; 61 63 begin 62 PacketStream := TMemoryStreamEx.Create; 63 ReceiveStream.Seek(0, soFromEnd); 64 ReceiveStream.WriteStream(Stream); 65 ReceiveStream.Position := 0; 66 Size := ReceiveStream.ReadWord; 67 while Size < ReceiveStream.Size do begin 68 PacketStream.Clear; 69 PacketStream.ReadStream(TStream(ReceiveStream), Size); 70 PacketSinglePin.Send(PacketStream); 64 try 65 PacketStream := TStreamHelper.Create; 66 ReceiveStream.Seek(0, soFromEnd); 67 ReceiveStream.WriteStream(Stream, Stream.Size); 68 ReceiveStream.Position := 0; 71 69 Size := ReceiveStream.ReadWord; 70 while Size < ReceiveStream.Size do begin 71 PacketStream.Stream.Size := 0; 72 PacketStream.ReadStream(TStream(ReceiveStream), Size); 73 PacketSinglePin.Send(PacketStream); 74 Size := ReceiveStream.ReadWord; 75 end; 76 finally 77 PacketStream.Free; 72 78 end; 73 PacketStream.Free;74 79 end; 75 80 76 procedure TPacketBurst.PacketSingleReceive(S tream: TStream);81 procedure TPacketBurst.PacketSingleReceive(Sender: TCommPin; Stream: TStream); 77 82 var 78 83 SignalEvent: Boolean; 79 84 begin 80 SendStreamLock.Acquire; 81 SendStream.WriteWord(Stream.Size); 82 Stream.Position := 0; 83 SendStream.WriteStream(Stream); 84 SignalEvent := SendStream.Size > SendBurstSize; 85 SendStreamLock.Release; 85 try 86 SendStreamLock.Acquire; 87 SendStream.WriteWord(Stream.Size); 88 Stream.Position := 0; 89 SendStream.WriteStream(Stream, Stream.Size); 90 SignalEvent := SendStream.Size > SendBurstSize; 91 finally 92 SendStreamLock.Release; 93 end; 86 94 if SignalEvent then SendThreadEvent.SetEvent; 87 95 end; … … 91 99 procedure TPacketBurstSendThread.Execute; 92 100 var 93 Stream: T MemoryStreamEx;101 Stream: TStreamHelper; 94 102 begin 95 103 inherited; 96 Stream := TMemoryStreamEx.Create; 97 with PacketBurst do 98 repeat 99 if SendThreadEvent.WaitFor(SendPeriod) = wrSignaled then begin 100 SendStreamLock.Acquire; 101 SendStream.Position := 0; 102 if SendStream.Size < SendBurstSize then begin 103 PacketBurstPin.Send(SendStream); 104 SendStream.Clear; 105 end else 106 while (SendStream.Size - SendStream.Position) > SendBurstSize do begin 107 Stream.Clear; 108 SendStream.ReadStream(TStream(Stream), SendBurstSize); 109 PacketBurstPin.Send(Stream); 104 try 105 Stream := TStreamHelper.Create; 106 with PacketBurst do 107 repeat 108 if SendThreadEvent.WaitFor(SendPeriod) = wrSignaled then 109 try 110 SendStreamLock.Acquire; 111 SendStream.Position := 0; 112 if SendStream.Size < SendBurstSize then begin 113 PacketBurstPin.Send(SendStream); 114 SendStream.Stream.Size := 0; 115 end else 116 while (SendStream.Size - SendStream.Position) > SendBurstSize do begin 117 Stream.Stream.Size := 0; 118 SendStream.ReadStream(TStream(Stream), SendBurstSize); 119 PacketBurstPin.Send(Stream); 120 end; 121 finally 122 SendStreamLock.Release; 110 123 end; 111 SendStreamLock.Release;112 end;113 until Terminated;114 Stream.Free;124 until Terminated; 125 finally 126 Stream.Free; 127 end; 115 128 end; 116 129
Note:
See TracChangeset
for help on using the changeset viewer.