unit uROZeroConfHubSynapse; {$I RemObjects.inc} {.$DEFINE TROHubSynapseTcpClient_DEBUG} interface uses SysUtils, synsock, blcksock, uROZeroConfHub, uROZeroConfStreamWorker; type TROHubSynapseTCPClient = class(TTCPStream) private fConnection: TTCPBlockSocket; procedure ProcessSynapseErrors; protected function GetConnected: Boolean;override; public procedure Connect(AHost: string; aPort: Integer);override; procedure Stop;override; function Read(Buffer: Pointer; Count: Longint): Longint; override; function Write(const Buffer; Count: Longint): Longint; override; end; TZeroConfHubSynapseClient = class(TZeroConfHubClient) protected function CreateTCPClient: TTCPStream; override; end; EZeroConfSynapseHubClient = class(EROZeroConfHubException) end; implementation {$IFDEF TROHubSynapseTcpClient_DEBUG} uses Windows,Classes; var debug_cnt:integer = 0; {$ENDIF} { TZeroConfHubSynapseClient } function TZeroConfHubSynapseClient.CreateTCPClient: TTCPStream; begin Result := TROHubSynapseTCPClient.Create; end; { TROHubSynapseTCPClient } procedure TROHubSynapseTCPClient.Connect(AHost: string; aPort: Integer); begin if fConnection = nil then begin fConnection := TTCPBlockSocket.Create; try fConnection.Connect(AHost,IntToStr(APort)); ProcessSynapseErrors; except FreeAndNil(fConnection); raise; end; end; end; function TROHubSynapseTCPClient.GetConnected: Boolean; begin Result := fConnection <> nil; end; procedure TROHubSynapseTCPClient.ProcessSynapseErrors; begin if fConnection <> nil then begin if fConnection.LastError <> 0 then raise EZeroConfSynapseHubClient.Create(fConnection.LastErrorDesc, fConnection.LastError); end else begin raise Exception.Create('not connected!'); end; end; function TROHubSynapseTCPClient.Read(Buffer: Pointer; Count: Integer): Longint; begin // hide exception if not Connected then begin Result := 0; exit; end; {$IFDEF TROHubSynapseTcpClient_DEBUG} OutputDebugString(Pchar('Read - start, cnt = '+inttoStr(Count))); try {$ENDIF} if not Connected then raise Exception.Create('not connected!'); Result := fConnection.RecvBuffer(Buffer, Count); {$IFDEF TROHubSynapseTCPClient_DEBUG} InterlockedIncrement(debug_cnt); with TFileStream.Create( ExtractFileName(ParamStr(0)) +' '+IntToHex(cardinal(self),8) +' log '+inttostr(debug_cnt)+'_read.txt', fmCreate) do try Write(Buffer^,Count); finally Free; end; finally OutputDebugString(Pchar('Read - end, Result = '+inttoStr(Result))); end; {$ENDIF} end; procedure TROHubSynapseTCPClient.Stop; begin if fConnection <> nil then begin sleep(100); // fConnection.CloseSocket; FreeAndNil(fConnection); end; end; function TROHubSynapseTCPClient.Write(const Buffer; Count: Integer): Longint; begin {$IFDEF TROHubSynapseTcpClient_DEBUG} OutputDebugString(Pchar('Write - start, cnt = '+inttoStr(Count))); try {$ENDIF} if not Connected then raise Exception.Create('not connected!'); {$IFDEF TROHubSynapseTcpClient_DEBUG} InterlockedIncrement(debug_cnt); with TFileStream.Create( ExtractFileName(ParamStr(0)) +' '+IntToHex(cardinal(self),8) +' log '+inttostr(debug_cnt)+'_write.txt', fmCreate) do try Write(Buffer,Count); finally Free; end; {$ENDIF} Result := fConnection.SendBuffer(@Buffer,Count); ProcessSynapseErrors; {$IFDEF TROHubSynapseTcpClient_DEBUG} finally OutputDebugString(Pchar('Write - end, Result = '+inttoStr(Result))); end; {$ENDIF} end; end.