/// <summary> /// TCP 内部服务端套接字任务处理 /// </summary> internal void Start() { bufferSize = Server.ReceiveBufferPool.Size; #if !MONO Socket.ReceiveBufferSize = Socket.SendBufferSize = bufferSize; #endif #if !DOTNET2 asyncEventArgs = SocketAsyncEventArgsPool.Get(); #endif Server.ReceiveBufferPool.Get(ref Buffer); OutputStream = (OutputSerializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer()).SetTcpServer(); #if DOTNET2 asyncCallback = onReceive; #else asyncEventArgs.Completed += onReceive; asyncEventArgs.SetBuffer(Buffer.Buffer, Buffer.StartIndex, bufferSize); #endif if (Server.VerifyCommandIdentity == 0) { IsVerifyMethod = true; if (isReceiveCommand()) return; } else { IsVerifyMethod = false; Socket.SendTimeout = Server.Attribute.ReceiveVerifyCommandSeconds * 1000; if (isReceiveVerifyCommand()) return; } close(); }
/// <summary> /// TCP 内部服务端套接字任务处理 /// </summary> internal void Start() { receiveBufferSize = Server.ReceiveBufferPool.Size; #if !MONO Socket.ReceiveBufferSize = receiveBufferSize; Socket.SendBufferSize = Server.SendBufferPool.Size; #endif #if !DOTNET2 receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); #endif Server.ReceiveBufferPool.Get(ref ReceiveBuffer); Sender = new ServerSocketSender(this); #if DOTNET2 onReceiveAsyncCallback = onReceive; #else receiveAsyncEventArgs.Completed += onReceive; receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); #endif if (Server.VerifyCommandIdentity == 0) { IsVerifyMethod = true; if (isReceiveCommand()) return; } else { IsVerifyMethod = false; if (isReceiveVerifyCommand()) return; } close(); }
/// <summary> /// TCP 内部服务端套接字任务处理 /// </summary> internal void Start() { #if !MONO Socket.ReceiveBufferSize = Server.ReceiveBufferPool.Size; Socket.SendBufferSize = Server.SendBufferPool.Size; #endif if ((maxInputSize = Server.Attribute.MaxInputSize) <= 0) maxInputSize = int.MaxValue; if ((maxMergeInputSize = maxInputSize + Server.ReceiveBufferPool.Size) < 0) maxMergeInputSize = int.MaxValue; #if !DOTNET2 receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); #endif Server.ReceiveBufferPool.Get(ref ReceiveBuffer); Sender = new ServerSocketSender(this); receiveBufferSize = ReceiveBuffer.PoolBuffer.Pool.Size; #if DOTNET2 onReceiveAsyncCallback = onReceive; #else receiveAsyncEventArgs.Completed += onReceive; receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); #endif if (Server.VerifyCommandIdentity == 0) { IsVerifyMethod = true; receiveCount = receiveIndex = 0; ReceiveType = TcpServer.ServerSocketReceiveType.Command; #if DOTNET2 Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) return; #else receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) return; #endif } else { IsVerifyMethod = false; ReceiveType = TcpServer.ServerSocketReceiveType.VerifyCommand; receiveTimeout = Date.NowTime.Now.AddSeconds(Server.Attribute.ReceiveVerifyCommandSeconds + 1); #if DOTNET2 IAsyncResult async = Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) { if (!async.CompletedSynchronously) Server.PushReceiveVerifyCommandTimeout(this, Socket); return; } #else receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) { Server.PushReceiveVerifyCommandTimeout(this, Socket); return; } #endif } #if !DOTNET2 ServerSocketTask.Task.Add(this); #endif }
/// <summary> /// HTTP 套接字数据接收器 /// </summary> /// <param name="socket">HTTP套接字</param> internal SocketBoundaryReceiver(Socket socket) : base(socket) { onReceiveAsyncCallback = onReceive; #if !DOTNET2 socketAsyncEventArgs = SocketAsyncEventArgsPool.Get(); socketAsyncEventArgs.Completed += onReceiveAsyncCallback; #endif }
///// <summary> ///// HTTP 头部是否接收完成 ///// </summary> //private byte isHeader; /// <summary> /// HTTP 头部 /// </summary> /// <param name="socket">HTTP 套接字</param> internal SocketHeader(Socket socket) : base() { httpSocket = socket; onReceiveAsyncCallback = onReceive; #if !DOTNET2 receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceiveAsyncCallback; receiveAsyncEventArgs.SetBuffer(Buffer.Buffer, Buffer.StartIndex, ReceiveBufferSize); #endif }
/// <summary> /// HTTP 套接字 /// </summary> internal Socket() { HttpHeader = Header = new SocketHeader(this); onReceiveAsyncCallback = onReceive; onSendAsyncCallback = onSend; #if !DOTNET2 receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); sendAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceiveAsyncCallback; sendAsyncEventArgs.Completed += onSendAsyncCallback; #endif }
private SendState send() { #if !DOTNET2 START: #endif if (IsSocket) { if (onSendAsyncCallback == null) onSendAsyncCallback = onSend; #if DOTNET2 SocketError socketError; Socket.BeginSend(sendData.Array, sendData.Start, sendData.Length, SocketFlags.None, out socketError, onSendAsyncCallback, Socket); if (socketError == SocketError.Success) return SendState.Asynchronous; #else if (sendAsyncEventArgs == null) { sendAsyncEventArgs = SocketAsyncEventArgsPool.Get(); sendAsyncEventArgs.Completed += onSendAsyncCallback; } #if !DotNetStandard while (Interlocked.CompareExchange(ref sendAsyncLock, 1, 0) != 0) Thread.Sleep(0); #endif sendAsyncEventArgs.SetBuffer(sendData.Array, sendData.Start, sendData.Length); if (Socket.SendAsync(sendAsyncEventArgs)) { #if !DotNetStandard Interlocked.Exchange(ref sendAsyncLock, 0); #endif return SendState.Asynchronous; } if (sendAsyncEventArgs.SocketError == SocketError.Success) { sendData.MoveStart(sendAsyncEventArgs.BytesTransferred); #if !DotNetStandard Interlocked.Exchange(ref sendAsyncLock, 0); #endif if (sendData.Length == 0) { freeCopyBuffer(); return SendState.Synchronize; } goto START; } #endif } return SendState.Error; }
/// <summary> /// 创建 TCP 服务客户端套接字 /// </summary> internal override void Create() { bool isErrorLog = false, isReceiveAsync = false; socketError = SocketError.Success; do { if (checkCreate() == 0) return; if (isSleep) { isSleep = false; Thread.Sleep(ClientCreator.CommandClient.TryCreateSleep); if (checkCreate() == 0) return; } try { Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if !MONO Socket.ReceiveBufferSize = ClientCreator.CommandClient.ReceiveBufferPool.Size; Socket.SendBufferSize = ClientCreator.CommandClient.SendBufferPool.Size; #endif Socket.Connect(ipAddress, port); if (checkCreate() == 0) return; #if DOTNET2 if (onReceiveAsyncCallback == null) onReceiveAsyncCallback = onReceive; #else if (receiveAsyncEventArgs == null) { receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceive; } #endif if (ReceiveBuffer.Buffer == null) ClientCreator.CommandClient.ReceiveBufferPool.Get(ref ReceiveBuffer); if (Sender == null) SetSender(new ClientSocketSender(this)); receiveBufferSize = ReceiveBuffer.PoolBuffer.Pool.Size; receiveCount = receiveIndex = 0; ReceiveType = TcpServer.ClientSocketReceiveType.CommandIdentity; #if DOTNET2 Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) #else #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 1); #endif receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) #endif { #if !DOTNET2 && !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock, 0); #endif isReceiveAsync = true; if (verifyMethod(ClientCreator.CommandClient)) { if (ClientCreator.OnSocketVerifyMethod(this)) { if (isErrorLog) { ClientCreator.CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接成功 " + ipAddress.ToString() + ":" + port.toString()); } return; } } if (Socket != null) { #if DotNetStandard AutoCSer.Net.TcpServer.CommandBase.CloseClientNotNull(Socket); #else Socket.Dispose(); #endif Socket = null; } return; } } catch (Exception error) { if (!isErrorLog) { isErrorLog = true; ClientCreator.CommandClient.Log.Add(AutoCSer.Log.LogType.Debug, error, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接失败 " + ipAddress.ToString() + ":" + port.toString()); } } if (isReceiveAsync) return; CreateSleep(); } while (true); }
/// <summary> /// 创建 TCP 服务客户端套接字 /// </summary> internal override void Create() { bool isErrorLog = false; socketError = SocketError.Success; do { if (checkCreate() == 0) return; if (isSleep) { isSleep = false; Thread.Sleep(ClientCreator.CommandClient.FristTryCreateSleep); } try { Socket = new Socket(IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); #if !MONO Socket.ReceiveBufferSize = ClientCreator.CommandClient.ReceiveBufferPool.Size; Socket.SendBufferSize = ClientCreator.CommandClient.SendBufferPool.Size; #endif Socket.Connect(IpAddress, Port); if (checkCreate() == 0) return; if (onReceiveAsyncCallback == null) onReceiveAsyncCallback = onReceive; #if !DOTNET2 if (receiveAsyncEventArgs == null) { receiveAsyncEventArgs = SocketAsyncEventArgsPool.Get(); receiveAsyncEventArgs.Completed += onReceiveAsyncCallback; } #endif if (ReceiveBuffer.Buffer == null) ClientCreator.CommandClient.ReceiveBufferPool.Get(ref ReceiveBuffer); if (Sender == null) SetSender(new ClientSocketSender(this)); receiveBufferSize = ReceiveBuffer.PoolBuffer.Pool.Size; receiveCount = receiveIndex = 0; ReceiveType = TcpServer.ClientSocketReceiveType.CommandIdentityAgain; #if DOTNET2 Socket.BeginReceive(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize, SocketFlags.None, out socketError, onReceiveAsyncCallback, Socket); if (socketError == SocketError.Success) #else #if !DotNetStandard Interlocked.Exchange(ref receiveAsyncLock.Lock, 1); #endif receiveAsyncEventArgs.SetBuffer(ReceiveBuffer.Buffer, ReceiveBuffer.StartIndex, receiveBufferSize); if (Socket.ReceiveAsync(receiveAsyncEventArgs)) #endif { #if !DOTNET2 && !DotNetStandard receiveAsyncLock.Exit(); #endif if (verifyMethod() && ClientCreator.OnSocketVerifyMethod(this)) { if (isErrorLog) { ClientCreator.CommandClient.Log.Debug(ClientCreator.Attribute.ServerName + " 客户端 TCP 连接成功 " + IpAddress.ToString() + ":" + Port.toString(), LogLevel.Debug | LogLevel.AutoCSer); } ClientCreator.VerifyCount = 0; return; } } ++ClientCreator.VerifyCount; } catch (Exception error) { if (!isErrorLog) { isErrorLog = true; ClientCreator.CommandClient.Log.Exception(error, ClientCreator.Attribute.ServerName + " 客户端 TCP 连接失败 " + IpAddress.ToString() + ":" + Port.toString(), LogLevel.Exception | LogLevel.AutoCSer); } } if (Sender != null) { VerifyMethodSleep(); return; } ClientCreator.VerifyCount = 0; CreateSleep(); } while (true); }