【原创】TROSynapseTCPChannel通讯管道,可替换TROIndyTCPChannel

本文介绍了一个使用Synapse库实现的TCP通道组件TROSynapseTCPChannel,该组件旨在替代原有的Indy组件,提高连接速度并简化接口。文章提供了源码及配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Indy由于版本过多,接口混乱,而且Synapse连接速度比Indy快很多,因此早想使用Synapse做一个TCP的Channel替换掉TROIndyTCPChannel。例子稍后几天补上,并提供测试对比数据。如果你有新的完善可以共享的话,请发到我邮箱里!
TROSynapseTCPChannel源码如下:
unit uROSynpaseTCPChannel;
interface
uses Classes, uROClient, {IdBaseComponent,} uROClientIntf, blcksock, synsock;
type
TSynapseClient = class(TTCPBlockSocket)
private
      FConnected: Boolean;
protected
      procedure DoStatus(Reason: THookSocketReason; const Value: string);override;
published
      property Connected:Boolean read FConnected default False;
end;
{ TROCustomIndyTCPChannel }
TROCustomSynapseTCPChannel = class(TROTransportChannel, IROTransport, IROTCPTransport)
private
     fSynapseClient : TSynapseClient;
     fKeepAlive: boolean;
     fDisableNagle: boolean;
     FConnectTimeout: Integer;
     FConnectFromIndy: Boolean;
     FPort: integer;
     FHost: string;
     FUsingIP6: Boolean;
     FRecvTimeout: Integer;
     FSendTimeout: Integer;
     procedure SetHost(const Value: string);
     procedure SetPort(const Value: integer);
     procedure SetConnectTimeout(const Value: Integer);
     procedure SetConnectFromIndy(const Value: Boolean);
     function GetHost: string;
     function GetPort: integer;
     procedure SetUsingIP6(const Value: Boolean);
     procedure SetRecvTimeout(const Value: Integer);
     procedure SetSendTimeout(const Value: Integer);
protected
     procedure IntDispatch(aRequest, aResponse : TStream); override;
     procedure IntSetServerLocator(aServerLocator : TROServerLocator); override;
     function CreateSynapseClient: TSynapseClient; virtual;
     { IROTransport }
     function GetTransportObject : TObject; override;
     { IROTCPTransport }
     function GetClientAddress : string;
public
     constructor Create(aOwner : TComponent); override;
     property Port : integer read FPort write SetPort;
     property Host : string read FHost write SetHost;
     property DisableNagle : boolean read fDisableNagle write fDisableNagle default FALSE;
     property SynapseClient : TSynapseClient read fSynapseClient;
     property KeepAlive : boolean read fKeepAlive write fKeepAlive default false;
     property ConnectTimeout:Integer read FConnectTimeout write SetConnectTimeout;
     property RecvTimeout:Integer read FRecvTimeout write SetRecvTimeout;
     property SendTimeout:Integer read FSendTimeout write SetSendTimeout;
     property ConnectFromIndy:Boolean read FConnectFromIndy write SetConnectFromIndy default False;
     property UsingIP6:Boolean read FUsingIP6 write SetUsingIP6 default False;
published
     property SynchronizedProbing;
     property OnSendStream;
     property OnReceiveStream;
     property ServerLocators;
     property DispatchOptions;
     property OnServerLocatorAssignment;
     property ProbeServers;
     property ProbeFrequency;
     property OnBeforeProbingServers;
     property OnAfterProbingServers;
     property OnBeforeProbingServer;
     property OnAfterProbingServer;
     property OnLoginNeeded;
end;
{ TROIndyTCPChannel }
TROSynapseTCPChannel = class(TROCustomSynapseTCPChannel, IROTransport, IROTCPTransport)
private
protected
published
     property Port;
     property Host;
     property DisableNagle;
     property SynapseClient;
     property KeepAlive;
     property ConnectTimeout;
     property RecvTimeout;
     property SendTimeout;
end;
implementation
uses
SysUtils, uROClasses;
{ TROCustomIndyTCPChannel }
constructor TROCustomSynapseTCPChannel.Create(aOwner: TComponent);
begin
inherited;
fSynapseClient := CreateSynapseClient;
//fSynapseClient.Name := 'InternalSynapseClient';
//
// {$IFDEF DELPHI6UP}
// fSynapseClient.SetSubComponent(TRUE);
// {$ENDIF}
FUsingIP6 := False;
FSynapseClient.Family := SF_IP4;
FConnectFromIndy := False;
FConnectTimeout := 10000;
FSendTimeout := 15000;
FRecvTimeout := 60000;
fKeepAlive := True;
end;
function TROCustomSynapseTCPChannel.CreateSynapseClient: TSynapseClient;
begin
result := TSynapseClient.Create;
TSynapseClient(result).Bind('127.0.0.1','8090');
end;
function TROCustomSynapseTCPChannel.GetClientAddress: string;
begin
Result :=    fSynapseClient.GetLocalSinIP;
end;
function TROCustomSynapseTCPChannel.GetTransportObject: TObject;
begin
result := Self;
end;
procedure TROCustomSynapseTCPChannel.IntSetServerLocator(
aServerLocator: TROServerLocator);
begin
Host := aServerLocator.Host;
Port := aServerLocator.Port;
end;
procedure TROCustomSynapseTCPChannel.IntDispatch(aRequest, aResponse: TStream);
begin
try
     if not fSynapseClient.Connected then
       fSynapseClient.Connect(FHost,IntToStr(FPort));
     if FConnectFromIndy then
     begin
       aRequest.Position := 0;
       fSynapseClient.SendStreamIndy(aRequest);
       fSynapseClient.RecvStreamIndy(aResponse,FRecvTimeout);
       aResponse.Position := 0;
     end
     else
     begin
       aRequest.Position := 0;
       fSynapseClient.SendStream(aRequest);
       fSynapseClient.RecvStream(aResponse,FRecvTimeout);
       aResponse.Position := 0;
     end;
finally
     if not KeepAlive then
       fSynapseClient.CloseSocket;
end;
end;
function TROCustomSynapseTCPChannel.GetHost: string;
begin
result := TSynapseClient(SynapseClient).GetRemoteSinIP;
end;
function TROCustomSynapseTCPChannel.GetPort: integer;
begin
result := TSynapseClient(SynapseClient).GetRemoteSinPort;
end;
procedure TROCustomSynapseTCPChannel.SetConnectFromIndy(const Value: Boolean);
begin
FConnectFromIndy := Value;
end;
procedure TROCustomSynapseTCPChannel.SetConnectTimeout(const Value: Integer);
begin
FConnectTimeout := Value;
fSynapseClient.SetTimeout(FConnectTimeout);
end;
procedure TROCustomSynapseTCPChannel.SetHost(const Value: string);
begin
FHost := Value;
fSynapseClient.Bind(Host,IntToStr(FPort));
end;
procedure TROCustomSynapseTCPChannel.SetPort(const Value: integer);
begin
FPort := Value;
fSynapseClient.Bind(Host,IntToStr(FPort));
end;
procedure TROCustomSynapseTCPChannel.SetRecvTimeout(const Value: Integer);
begin
FRecvTimeout := Value;
end;
procedure TROCustomSynapseTCPChannel.SetSendTimeout(const Value: Integer);
begin
FSendTimeout := Value;
fSynapseClient.SetSendTimeout(FSendTimeout);
end;
procedure TROCustomSynapseTCPChannel.SetUsingIP6(const Value: Boolean);
begin
FUsingIP6 := Value;
if FUsingIP6 then
     FSynapseClient.Family := SF_IP6
else
     FSynapseClient.Family := SF_IP4;
end;
{ TSynapseClient }
procedure TSynapseClient.DoStatus(Reason: THookSocketReason;
const Value: string);
begin
inherited;
FConnected := (Reason in [HR_Connect,HR_CanRead,HR_CanWrite,HR_Accept,
                             HR_WriteCount,HR_ReadCount,HR_Listen]);
// FConnected := not (Reason in [HR_Error,HR_SocketClose,HR_SocketCreate]);
end;
initialization
RegisterTransportChannelClass(TROSynapseTCPChannel);
finalization
UnRegisterTransportChannelClass(TROSynapseTCPChannel);
end.


        
        



        







        
          
            
            评论这张
          
        


          
            
               【原创】TROSynapseTCPChannel通讯管道,可替换TROIndyTCPChannel - yyimen - yyimen的博客
            
            转发至微博
          
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值