点击或拖拽改变大小

NetworkDoubleBaseInitializationOnConnectAsync 方法

根据实际的协议选择是否重写本方法,有些协议在创建连接之后,需要进行一些初始化的信号握手,才能最终建立网络通道。
Whether to rewrite this method is based on the actual protocol. Some protocols require some initial signal handshake to establish a network channel after the connection is created.

命名空间:  HslCommunication.Core.Net
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:9.6.6.0 (9.6.6.0)
语法
protected virtual Task<OperateResult> InitializationOnConnectAsync(
	Socket socket
)

参数

socket
类型:System.Net.SocketsSocket
网络套接字

返回值

类型:TaskOperateResult
是否初始化成功,依据具体的协议进行重写
示例
有些协议不需要握手信号,比如三菱的MC协议,Modbus协议,西门子和欧姆龙就存在握手信息,此处的例子是继承本类后重写的西门子的协议示例
西门子重连示例
        /// <inheritdoc/>
        public override OperateResult<byte[]> ReadFromCoreServer( Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true )
        {
            while (true)
            {
                OperateResult<byte[]> read = base.ReadFromCoreServer( socket, send, hasResponseData, usePackHeader );
                if (!read.IsSuccess) return read;

                if ((read.Content[2] * 256 + read.Content[3]) != 0x07) return read;
            }
        }

        /// <inheritdoc/>
        protected override OperateResult InitializationOnConnect( Socket socket )
        {
            // 第一次握手 -> First handshake
            OperateResult<byte[]> read_first = ReadFromCoreServer( socket, plcHead1 );
            if (!read_first.IsSuccess) return read_first;

            // 第二次握手 -> Second handshake
            OperateResult<byte[]> read_second = ReadFromCoreServer( socket, plcHead2 );
            if (!read_second.IsSuccess) return read_second;

            // 调整单次接收的pdu长度信息
            pdu_length = ByteTransform.TransUInt16( read_second.Content.SelectLast( 2 ), 0 ) - 28;
            if (pdu_length < 200) pdu_length = 200;

            // 返回成功的信号 -> Return a successful signal
            return OperateResult.CreateSuccessResult( );
        }

#if !NET35 && !NET20
        /// <inheritdoc/>
        public async override Task<OperateResult<byte[]>> ReadFromCoreServerAsync( Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true )
        {
            while (true)
            {
                OperateResult<byte[]> read = await base.ReadFromCoreServerAsync( socket, send, hasResponseData, usePackHeader );
                if (!read.IsSuccess) return read;

                if ((read.Content[2] * 256 + read.Content[3]) != 0x07) return read;
            }
        }

        /// <inheritdoc/>
        protected override async Task<OperateResult> InitializationOnConnectAsync( Socket socket )
        {
            // 第一次握手 -> First handshake
            OperateResult<byte[]> read_first = await ReadFromCoreServerAsync( socket, plcHead1 );
            if (!read_first.IsSuccess) return read_first;

            // 第二次握手 -> Second handshake
            OperateResult<byte[]> read_second = await ReadFromCoreServerAsync( socket, plcHead2 );
            if (!read_second.IsSuccess) return read_second;

            // 调整单次接收的pdu长度信息
            pdu_length = ByteTransform.TransUInt16( read_second.Content.SelectLast( 2 ), 0 ) - 28;
            if (pdu_length < 200) pdu_length = 200;

            // 返回成功的信号 -> Return a successful signal
            return OperateResult.CreateSuccessResult( );
        }
#endif
参见