// SERVER
procedure TForm1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var ctr : TControlRec;
tmpint : integer;
cMSG : TChatRec;
LoginRec : TLoginRec;
ServerRec : TServerInfoRec;
size : LongWord;
nRead : Integer;
Buff :
Array[Word]
Of Byte;
OldPos : Integer;
fdata : TMemoryStream;
begin
tmpint := GetIndexForSocketHandle(Socket.SocketHandle);
if tmpint <> -1
then
begin
if server_type[tmpint].TypeID = QUERY_CONTROL
then
begin
Socket.ReceiveBuf(ctr, sizeof(ctr));
server_type[tmpint].TypeID := ctr.QUERY;
if ctr.QTYPE = CLIENT_RESPO
then
Socket.SendBuf(ctr, sizeOf(TControlRec));
end
else if server_type[tmpint].TypeID = QUERY_CHATMSG
then
begin
Socket.ReceiveBuf(cMSG, sizeOF(cMsg));
SendToAllClients(cMSG);
server_type[tmpint].TypeID := 0;
end
else if server_type[tmpint].TypeID = QUERY_LOGINOK
then
BroadCast('
User '+server_type[tmpint].UserRec.Username+'
logged in')
else if server_type[tmpint].TypeID = QUERY_LOGIN
then
begin
fdata := TMemoryStream.Create;
OldPos := fdata.Position;
fData.Position := fData.Size;
Repeat
nRead := Socket.ReceiveBuf(buff, High(Buff) - Low(Buff));
fData.WriteBuffer(buff, nRead);
Until ( Socket.ReceiveLength = 0 );
fData.Position := OldPos;
fdata.
Read(LoginRec.UserId , sizeof(LongWord));
fdata.
read(size, SizeOf(size));
SetLength(LoginRec.UserName, Size);
fdata.
read(Pointer(LoginRec.UserName)^, size);
fdata.
read(size, SizeOf(size));
SetLength(LoginRec.Password, Size);
fdata.
read(Pointer(LoginRec.Password)^, size);
fdata.Free;
if CheckUser(LoginRec)
then
begin
ServerResponceQuery(Socket, QUERY_LOGINOK);
server_type[tmpint].UserRec := LoginRec;
end
else
ServerQuery(Socket, QUERY_LOGINERR);
server_type[tmpint].TypeID := 0;
end
else if server_type[tmpint].TypeID = QUERY_SERVERINFO
then
begin
ServerRec := thisserver;
Socket.SendBuf(ServerRec, SizeOf(TServerInfoRec));
server_type[tmpint].TypeID := 0;
end else
Socket.ReceiveBuf(ctr, sizeof(ctr));
end;
end;
// CLIENT
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var cMSG : TChatRec;
sText :
String;
rxTxt : TRxTextAttributes;
ctr : TControlRec;
size : LongWord;
nRead : Integer;
Buff :
Array[Word]
Of Byte;
OldPos : Integer;
fdata : TMemoryStream;
begin
if client_type = QUERY_CONTROL
then
begin
Socket.ReceiveBuf(ctr, SizeOf(ctr));
client_type := ctr.QUERY;
if ctr.QTYPE = SERVER_RESPO
then
ClientResponceQuery(ctr.QUERY);
end
else if client_type = QUERY_LOGIN
then
begin
ClientSocket1.Socket.SendBuf(tmpLOGIN, SizeOf(TLoginRec));
client_type := 0;
end
else if client_type = QUERY_RECEIVEMESSAGE
then
begin
fdata := TMemoryStream.Create;
OldPos := fdata.Position;
fData.Position := fData.Size;
Repeat
nRead := Socket.ReceiveBuf(buff, High(Buff) - Low(Buff));
fData.WriteBuffer(buff, nRead);
Until ( Socket.ReceiveLength = 0 );
fData.Position := OldPos;
fdata.
Read(cmsg.UserId, sizeof(LongWord));
fdata.
Read(cmsg.UserToId, sizeof(LongWord));
fdata.
Read(cmsg.Color, sizeof(TColor));
fdata.
read(size, SizeOf(size));
SetLength(cmsg.Msg, Size);
fdata.
read(Pointer(cmsg.Msg)^, size);
fdata.
Read(cmsg.UserToId, sizeof(LongWord));
fdata.Free;
client_type := 0;
WriteChatMessage(CMSG);
end
else if client_type = QUERY_CHATMSG
then
begin
ClientSocket1.Socket.SendBuf(tmpCMSG, SizeOf(TChatRec));
client_type := 0;
end
else if client_type = QUERY_LOGINOK
then
begin
Socket.ReceiveBuf(ctr,sizeof(ctr));
FSmilies.Add('
Login Accepted');
ClientResponceQuery(QUERY_LOGINOK);
client_type := 0;
end
else if client_type = QUERY_LOGINERR
then
begin
FSmilies.Add('
Couldn''
t Login...');
ClientSocket1.Active := FALSE;
end;
Socket.Data :=
nil;
end;
// Query
procedure TForm1.Query(AType: Integer);
var ctr : TControlRec;
begin
ctr.QUERY := AType;
ctr.QTYPE := CLIENT_QUERY;
if ClientSocket1.Active = TRUE
then
ClientSocket1.Socket.SendBuf(ctr, sizeOf(TControlRec));
end;