Originally Posted by
arthurprs
uhm, you don't need to specify the amount of data you want to receive?
If you look at the code I posted, then you can see that I do.
Code:
Function TPacketWriter.WritePacket(Const APacket : PPacket) : Boolean;
Var
DataSize : LongInt;
Begin
Result := False;
Case APacket^.PacketType Of
ePacketType_Login : DataSize := SizeOf(TPacket_Login);
ePacketType_Logoff : DataSize := SizeOf(TPacket_Logoff);
ePacketType_Chat : DataSize := SizeOf(TPacket_Chat);
Else
Exit;
End;
If Not WriteData(@DataSize,SizeOf(DataSize)) Then Exit;
If Not WriteData(APacket,DataSize) Then Exit;
Result := True;
End;
You can see that I send the size of the packet first, then the packet itself...
On the receiving end I read the datasize first, then use that datasize to read the packet itself.
Code:
Function TPacketReader.ReadPacket(Var APacket : Pointer) : Boolean;
Var
DataSize : LongInt;
Packet : Pointer;
Begin
Result := False;
If Not ReadData(@DataSize,SizeOf(DataSize)) Then Exit;
GetMem(Packet,DataSize);
If Not ReadData(Packet,DataSize) Then
Begin
FreeMem(Packet);
Exit;
End;
APacket := Packet;
Result := True;
End;
As you can hopefully see, this does not depend on the type of Packet reader/writer descendant you write :-)
I hope this helps :-)
cheers,
Paul
Bookmarks