private unsafe AppendBuffer getAppendBuffer(ref valueType value, BinarySerialize.Serializer serializer, ref SubBuffer.PoolBufferFull buffer, out int size) { fixed(byte *bufferFixed = buffer.Buffer) { byte *start = bufferFixed + buffer.StartIndex; serializer.SerializeNotNull(ref value, start, buffer.PoolBuffer.Pool.Size, ClientConfig.BinarySerializeConfig); size = serializer.Stream.ByteSize; if (serializer.Stream.Data.Data == start) { return new AppendBuffer { Buffer = new SubArray <byte> { Array = buffer.Buffer, Start = buffer.StartIndex, Length = size }, Index = size == Size ? Index : 0 } } ; else { return new AppendBuffer { Buffer = new SubArray <byte> { Array = serializer.Stream.GetArray(), Length = size }, Index = size == Size ? Index : 0 } }; } } }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="host">监听主机名称</param> /// <param name="port">监听端口</param> /// <param name="serviceName">服务名称</param> /// <param name="sendBufferMaxSize">发送数据缓存区最大字节大小</param> /// <param name="minCompressSize">压缩启用最低字节数量</param> /// <param name="log">日志接口</param> /// <param name="maxInputSize">最大输入数据字节数</param> internal Client(string host, int port, string serviceName, int sendBufferMaxSize, int minCompressSize, ILog log, int maxInputSize) : base(serviceName, sendBufferMaxSize, minCompressSize, log) { this.Host = host; this.Port = port; IpAddress = HostPort.HostToIPAddress(this.Host, Log); this.maxInputSize = maxInputSize <= 0 ? int.MaxValue : maxInputSize; outputStream = (outputSerializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer()).SetTcpServer(); }
/// <summary> /// 设置远程对象 /// </summary> /// <param name="value"></param> /// <param name="blockIndex">磁盘块编号</param> /// <param name="bufferSize">序列化缓冲区大小</param> /// <returns></returns> public unsafe bool Set(valueType value, int blockIndex, SubBuffer.Size bufferSize = SubBuffer.Size.Kilobyte4) { if (value == null) { SetNull(); return(true); } Server.TcpInternalClient client = ClientPool.Get(blockIndex); if (client != null) { BinarySerialize.Serializer serializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer(); SubBuffer.PoolBufferFull buffer = default(SubBuffer.PoolBufferFull); SubBuffer.Pool.GetPool(bufferSize).Get(ref buffer); try { fixed(byte *bufferFixed = buffer.Buffer) { byte *start = bufferFixed + buffer.StartIndex; serializer.SerializeNotNull(value, start, buffer.PoolBuffer.Pool.Size, ClientConfig.BinarySerializeConfig); AutoCSer.Net.TcpServer.ReturnValue <ulong> index; int size = serializer.Stream.ByteSize; if (serializer.Stream.Data.Data == start) { index = client.append(new AppendBuffer { Buffer = new SubArray <byte> { Array = buffer.Buffer, Start = buffer.StartIndex, Length = size }, Index = size == Size ? Index : 0, BlockIndex = (ushort)blockIndex }); } else { index = client.append(new AppendBuffer { Buffer = new SubArray <byte> { Array = serializer.Stream.GetArray(), Length = size }, Index = size == Size ? Index : 0, BlockIndex = (ushort)blockIndex }); } if (index.Type == Net.TcpServer.ReturnType.Success && index.Value != 0) { Index = index.Value; Size = size; value = Value; state = MemberState.Remote; return(true); } } } finally { buffer.Free(); serializer.Free(); } } return(false); }
protected void freeSerializer() { if (OutputSerializer != null) { OutputSerializer.Free(); OutputSerializer = null; if (OutputJsonSerializer != null) { OutputJsonSerializer.Free(); OutputJsonSerializer = null; } } }
/// <summary> /// TCP 服务客户端 /// </summary> /// <param name="attribute">TCP服务调用配置</param> /// <param name="log">日志接口</param> /// <param name="maxInputSize">最大输入数据字节数</param> internal Client(TcpServer.ServerBaseAttribute attribute, ILog log, int maxInputSize) : base(attribute, attribute.ClientSendBufferMaxSize, log) { this.Host = attribute.Host; this.Port = attribute.Port; IpAddress = HostPort.HostToIPAddress(this.Host, Log); this.maxInputSize = maxInputSize <= 0 ? int.MaxValue : maxInputSize; outputStream = (outputSerializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer()).SetTcpServer(); SubBuffer.Pool.GetPool(attribute.GetSendBufferSize).Get(ref Buffer); if (attribute.IsRemoteExpression) { remoteExpressionServerNodeIdChecker = new RemoteExpressionServerNodeIdChecker { Client = this } } ; }
/// <summary> /// 设置远程对象 /// </summary> /// <param name="value"></param> /// <param name="blockIndex">磁盘块编号</param> /// <param name="bufferSize">序列化缓冲区大小</param> /// <returns></returns> public async Task <bool> SetAsync(valueType value, int blockIndex, SubBuffer.Size bufferSize = SubBuffer.Size.Kilobyte4) { if (value == null) { SetNull(); return(true); } Server.TcpInternalClient client = ClientPool.Get(blockIndex); if (client != null) { BinarySerialize.Serializer serializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer(); SubBuffer.PoolBufferFull buffer = default(SubBuffer.PoolBufferFull); SubBuffer.Pool.GetPool(bufferSize).Get(ref buffer); try { int size; AppendBuffer appendBuffer = getAppendBuffer(ref value, serializer, ref buffer, out size); appendBuffer.BlockIndex = (ushort)blockIndex; AutoCSer.Net.TcpServer.ReturnValue <ulong> index = await client.appendAwaiter(appendBuffer); if (index.Type == Net.TcpServer.ReturnType.Success && index.Value != 0) { Index = index.Value; Size = size; value = Value; state = MemberState.Remote; return(true); } } finally { buffer.Free(); serializer.Free(); } } return(false); }
internal void FreeSerializer() { OutputStream = null; if (ReceiveDeSerializer != null) { ReceiveDeSerializer.Free(); ReceiveDeSerializer = null; } if (ReceiveJsonParser != null) { ReceiveJsonParser.Free(); ReceiveJsonParser = null; } if (OutputSerializer != null) { OutputSerializer.Free(); OutputSerializer = null; } if (OutputJsonSerializer != null) { OutputJsonSerializer.Free(); OutputJsonSerializer = null; } }