2013-08-17 19:27:16 -07:00
|
|
|
Unit m_TCP_Client;
|
|
|
|
|
|
|
|
{$I M_OPS.PAS}
|
|
|
|
|
|
|
|
Interface
|
|
|
|
|
2013-09-21 22:32:34 -07:00
|
|
|
{$DEFINE USESTRINGLIST}
|
2013-09-07 20:52:11 -07:00
|
|
|
|
2013-08-17 19:27:16 -07:00
|
|
|
Uses
|
2013-09-07 20:52:11 -07:00
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
Classes,
|
|
|
|
{$ENDIF}
|
2013-08-17 19:27:16 -07:00
|
|
|
m_Strings,
|
|
|
|
m_IO_Sockets;
|
|
|
|
|
|
|
|
Type
|
|
|
|
TTCPClient = Class
|
|
|
|
Client : TIOSocket;
|
|
|
|
ResponseType : Integer;
|
2013-09-05 18:09:05 -07:00
|
|
|
ResponseStr : String;
|
2013-09-07 20:52:11 -07:00
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
ResponseData : TStringList;
|
|
|
|
{$ENDIF}
|
2013-09-07 14:25:43 -07:00
|
|
|
NetInterface : String;
|
2013-08-17 19:27:16 -07:00
|
|
|
|
2013-09-07 14:56:12 -07:00
|
|
|
Constructor Create (NetI: String); Virtual;
|
|
|
|
Destructor Destroy; Override;
|
|
|
|
Function Connect (Address: String; Port: Word) : Boolean; Virtual;
|
2013-08-17 19:27:16 -07:00
|
|
|
Function SendCommand (Str: String) : Integer;
|
|
|
|
Function GetResponse : Integer;
|
|
|
|
End;
|
|
|
|
|
|
|
|
Implementation
|
|
|
|
|
2013-09-07 14:25:43 -07:00
|
|
|
Constructor TTCPClient.Create (NetI: String);
|
2013-08-17 19:27:16 -07:00
|
|
|
Begin
|
|
|
|
Inherited Create;
|
|
|
|
|
|
|
|
Client := NIL;
|
2013-09-07 14:25:43 -07:00
|
|
|
NetInterface := NetI;
|
2013-09-07 20:52:11 -07:00
|
|
|
|
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
ResponseData := TStringList.Create;
|
|
|
|
{$ENDIF}
|
2013-08-17 19:27:16 -07:00
|
|
|
End;
|
|
|
|
|
|
|
|
Destructor TTCPClient.Destroy;
|
|
|
|
Begin
|
|
|
|
Client.Free;
|
2013-09-07 20:52:11 -07:00
|
|
|
|
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
ResponseData.Free;
|
|
|
|
{$ENDIF}
|
2013-08-17 19:27:16 -07:00
|
|
|
|
|
|
|
Inherited Destroy;
|
|
|
|
End;
|
|
|
|
|
|
|
|
Function TTCPClient.Connect (Address: String; Port: Word) : Boolean;
|
|
|
|
Begin
|
|
|
|
Client := TIOSocket.Create;
|
|
|
|
|
|
|
|
Result := Client.Connect(Address, Port);
|
|
|
|
End;
|
|
|
|
|
|
|
|
Function TTCPClient.SendCommand (Str: String) : Integer;
|
|
|
|
Begin
|
|
|
|
Result := -1;
|
|
|
|
|
|
|
|
If Client.FSocketHandle = -1 Then Exit;
|
|
|
|
|
2013-09-18 15:52:23 -07:00
|
|
|
// WriteLn ('DEBUG SENT ' + Str);
|
2013-09-07 12:52:31 -07:00
|
|
|
|
2013-09-05 18:09:05 -07:00
|
|
|
Client.PurgeInputData(1);
|
|
|
|
|
2013-08-17 19:27:16 -07:00
|
|
|
Client.WriteLine(Str);
|
|
|
|
|
|
|
|
Result := GetResponse;
|
|
|
|
End;
|
|
|
|
|
|
|
|
Function TTCPClient.GetResponse : Integer;
|
|
|
|
Var
|
|
|
|
Str : String;
|
2013-09-05 18:09:05 -07:00
|
|
|
Res : LongInt;
|
2013-08-17 19:27:16 -07:00
|
|
|
Begin
|
|
|
|
Result := -1;
|
|
|
|
|
|
|
|
If Client.FSocketHandle = -1 Then Exit;
|
|
|
|
|
2013-09-05 18:09:05 -07:00
|
|
|
If Client.WaitForData(10000) > 0 Then
|
|
|
|
If Client.ReadLine(ResponseStr) > 0 Then Begin
|
|
|
|
ResponseType := strS2I(Copy(ResponseStr, 1, 3));
|
|
|
|
Result := ResponseType;
|
2013-08-17 19:27:16 -07:00
|
|
|
|
2013-09-18 15:52:23 -07:00
|
|
|
// WriteLn ('DEBUG RECV ' + ResponseStr);
|
2013-09-09 21:48:20 -07:00
|
|
|
|
2013-09-05 18:09:05 -07:00
|
|
|
If ResponseStr[4] = '-' Then Begin
|
2013-09-07 20:52:11 -07:00
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
ResponseData.Clear;
|
|
|
|
{$ENDIF}
|
2013-08-17 19:27:16 -07:00
|
|
|
|
2013-09-05 18:09:05 -07:00
|
|
|
Repeat
|
|
|
|
Res := Client.ReadLine(Str);
|
2013-08-17 19:27:16 -07:00
|
|
|
|
2013-09-05 18:09:05 -07:00
|
|
|
If Res < 0 Then
|
|
|
|
Break;
|
2013-08-17 19:27:16 -07:00
|
|
|
|
2013-09-07 20:52:11 -07:00
|
|
|
{$IFDEF USESTRINGLIST}
|
|
|
|
If Res > 0 Then
|
|
|
|
ResponseData.Add(Str);
|
|
|
|
{$ENDIF}
|
2013-09-05 18:09:05 -07:00
|
|
|
Until Copy(Str, 1, 4) = strI2S(ResponseType) + ' ';
|
|
|
|
End;
|
2013-08-17 19:27:16 -07:00
|
|
|
End;
|
|
|
|
End;
|
|
|
|
|
|
|
|
End.
|