Hi arthurprs,
I just wrote a quick project containing the stream reader/writer I already presented, but now also has synapse UDP reader/writer classes too
It has a test for both types of reader/writer combos, and both tests work for me (including the UDP)...
Code:
program reader_writer_test;
{$APPTYPE CONSOLE}
uses
SysUtils,
packet_types,
packet_classes,
Classes,
blcksock;
Type
{..............................................................................}
TPacketReader_Stream = Class(TPacketReader)
Private
FStream : TStream;
Protected
Function ReadData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean; Override;
Public
Constructor Create(Const AStream : TStream);
End;
{..............................................................................}
{..............................................................................}
TPacketWriter_Stream = Class(TPacketWriter)
Private
FStream : TStream;
Protected
Function WriteData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean; Override;
Public
Constructor Create(Const AStream : TStream);
End;
{..............................................................................}
{..............................................................................}
TPacketReader_Synapse_UDP = Class(TPacketReader)
Private
FSocket : TUDPBlockSocket;
Protected
Function ReadData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean; Override;
Public
Constructor Create(Const ASocket : TUDPBlockSocket;
Const APort : AnsiString);
End;
{..............................................................................}
{..............................................................................}
TPacketWriter_Synapse_UDP = Class(TPacketWriter)
Private
FSocket : TUDPBlockSocket;
FIP : AnsiString;
FPort : AnsiString;
Protected
Function WriteData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean; Override;
Public
Constructor Create(Const ASocket : TUDPBlockSocket;
Const AIP, APort : AnsiString);
End;
{..............................................................................}
Constructor TPacketReader_Stream.Create(Const AStream : TStream);
Begin
Inherited Create;
FStream := AStream;
End;
{..............................................................................}
{..............................................................................}
Function TPacketReader_Stream.ReadData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean;
Type
PByte = ^Byte;
Begin
Result := FStream.Read(PByte(AData)^,ADataSize) = ADataSize;
End;
{..............................................................................}
{..............................................................................}
Constructor TPacketWriter_Stream.Create(Const AStream : TStream);
Begin
Inherited Create;
FStream := AStream;
End;
{..............................................................................}
{..............................................................................}
Function TPacketWriter_Stream.WriteData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean;
Type
PByte = ^Byte;
Begin
Result := FStream.Write(PByte(AData)^,ADataSize) = ADataSize;
End;
{..............................................................................}
{..............................................................................}
Constructor TPacketReader_Synapse_UDP.Create(Const ASocket : TUDPBlockSocket;
Const APort : AnsiString);
Begin
Inherited Create;
FSocket := ASocket;
FSocket.Bind('0.0.0.0',APort);
End;
{..............................................................................}
{..............................................................................}
Function TPacketReader_Synapse_UDP.ReadData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean;
Type
PByte = ^Byte;
Begin
FSocket.RecvBuffer(AData,ADataSize);
Result := FSocket.LastError = 0;
If Not Result Then WriteLn('TPacketReader_Synapse_UDP: ',FSocket.LastErrorDesc);
End;
{..............................................................................}
{..............................................................................}
Constructor TPacketWriter_Synapse_UDP.Create(Const ASocket : TUDPBlockSocket;
Const AIP, APort : AnsiString);
Begin
Inherited Create;
FSocket := ASocket;
FIP := AIP;
FPort := APort;
End;
{..............................................................................}
{..............................................................................}
Function TPacketWriter_Synapse_UDP.WriteData(Const AData : Pointer; Const ADataSize : LongInt) : Boolean;
Begin
FSocket.Connect(FIP,FPort);
FSocket.SendBuffer(AData,ADataSize);
Result := FSocket.LastError = 0;
If Not Result Then WriteLn('TPacketWriter_Synapse_UDP: ',FSocket.LastErrorDesc);
End;
{..............................................................................}
{..............................................................................}
Procedure WritePackets(Const APacketWriter : TPacketWriter);
Begin
APacketWriter.WriteLoginPacket ('UserName','PassWord');
APacketWriter.WriteChatPacket ('Message#1');
APacketWriter.WriteChatPacket ('Message#2');
APacketWriter.WriteChatPacket ('Message#3');
APacketWriter.WriteChatPacket ('Message#4');
APacketWriter.WriteChatPacket ('Message#5');
APacketWriter.WriteChatPacket ('Message#6');
APacketWriter.WriteLogoffPacket('UserName','PassWord');
End;
{..............................................................................}
{..............................................................................}
Procedure ProcessLoginPacket(Const APacket : PPacket_Login);
Begin
WriteLn('Login Packet:');
WriteLn(' ',APacket^.UserName);
WriteLn(' ',APacket^.PassWord);
End;
{..............................................................................}
{..............................................................................}
Procedure ProcessLogoffPacket(Const APacket : PPacket_Logoff);
Begin
WriteLn('Logoff Packet:');
WriteLn(' ',APacket^.UserName);
WriteLn(' ',APacket^.PassWord);
End;
{..............................................................................}
{..............................................................................}
Procedure ProcessChatPacket(Const APacket : PPacket_Chat);
Begin
WriteLn('Chat Packet: ');
WriteLn(' ',APacket^.Msg);
End;
{..............................................................................}
{..............................................................................}
Procedure ReadPackets(Const APacketReader : TPacketReader);
Var
Packet : Pointer;
Finished : Boolean;
Begin
Finished := False;
While Not Finished Do
Begin
If APacketReader.ReadPacket(Packet) Then
Begin
Case PPacket(Packet)^.PacketType Of
ePacketType_Login : ProcessLoginPacket (Packet);
ePacketType_Logoff : ProcessLogoffPacket(Packet);
ePacketType_Chat : ProcessChatPacket (Packet);
Else
{unknown packet so do error or something}
End;
If PPacket(Packet)^.PacketType = ePacketType_Logoff Then
Finished := True;
APacketReader.FreePacket(Packet);
End;
End;
End;
{..............................................................................}
{..............................................................................}
Procedure RunTest_Stream;
Var
MemoryStream : TMemoryStream;
PacketReader : TPacketReader;
PacketWriter : TPacketWriter;
Begin
MemoryStream := TMemoryStream.Create;
PacketWriter := TPacketWriter_Stream.Create(MemoryStream);
PacketReader := TPacketReader_Stream.Create(MemoryStream);
WritePackets(PacketWriter);
MemoryStream.Position := 0;
ReadPackets(PacketReader);
MemoryStream.Free;
PacketReader.Free;
PacketWriter.Free;
End;
{..............................................................................}
{..............................................................................}
Procedure RunTest_UDP;
Var
ClientSocket : TUDPBlockSocket;
ServerSocket : TUDPBlockSocket;
PacketReader : TPacketReader;
PacketWriter : TPacketWriter;
Begin
ClientSocket := TUDPBlockSocket.Create;
ServerSocket := TUDPBlockSocket.Create;
PacketWriter := TPacketWriter_Synapse_UDP.Create(ClientSocket,'LocalHost','12000');
PacketReader := TPacketReader_Synapse_UDP.Create(ServerSocket,'12000');
WritePackets(PacketWriter);
ReadPackets(PacketReader);
ClientSocket.Free;
ServerSocket.Free;
PacketReader.Free;
PacketWriter.Free;
End;
{..............................................................................}
{..............................................................................}
Begin
WriteLn('*********************');
WriteLn('**** stream test ****');
WriteLn('*********************');
RunTest_Stream;
WriteLn('******************');
WriteLn('**** UDP test ****');
WriteLn('******************');
RunTest_UDP;
WriteLn('Press Enter to continue!');
ReadLn;
End.
cheers,
Paul
Bookmarks