public ActorTransportSession(TcpSocketSession session) { if (session == null) { throw new ArgumentNullException("session"); } _session = session; }
private void AddTcpSocketSession(Socket client) { if (client == null) { return; } lock (ChannelManager.SyncLock) { ISocketAsyncEventArgsProxy socketProxy = this._SocketAsyncPool.Pop(); if (socketProxy == null) { AsyncUtil.AsyncRun(client.SafeClose); Logger.Info(false, "已经到达最大连接数"); return; } ISocketSession socketSession = new TcpSocketSession(client, (IPEndPoint)client.RemoteEndPoint, socketProxy); socketSession.Setup(this); socketSession.Initialize(); if (ChannelManager.AddChannel(socketSession.SessionID, socketSession)) { socketSession.CloseSocket += socketChannel_CloseSocket; if (ServerConfig.ControlMode == ControlMode.Self || ServerConfig.ControlMode == ControlMode.Parallel || ServerConfig.ControlMode == ControlMode.Singleton) { socketSession.SocketReceiveData += socketChannel_SocketReceiveData; AsyncUtil.AsyncRun(socketSession.TryReceive); } OnSocketConnected(socketSession.RemoteIP, socketSession.RemotePort); OnChannelChanged(socketSession.RemoteIP, CommunicateType.NET, ChannelState.Open); Logger.Info(false, String.Format("增加远程连接>>{0}:{1} 成功", socketSession.RemoteIP, socketSession.RemotePort)); } else { ISocketAsyncEventArgsProxy proxy = socketSession.SocketAsyncProxy; proxy.Reset(); if (proxy.SocketReceiveEventArgsEx.InitOffset != proxy.SocketReceiveEventArgsEx.Offset) { proxy.SocketReceiveEventArgsEx.SetBuffer(proxy.SocketReceiveEventArgsEx.InitOffset, ServerConfig.NetReceiveBufferSize); } _SocketAsyncPool.Push(proxy); socketSession.Close(); socketSession = null; Logger.Info(true, String.Format("增加远程连接>>{0}:{1} 失败", socketSession.RemoteIP, socketSession.RemotePort)); } } }
private void SendRpcException(TcpSocketSession session, Guid guid, Exception exception) { RpcExceptionPayload payload = RpcExceptionPayload.Create(guid, exception); Message msg = Message.Create("rpc-error", payload.ToArray()); server.SendTo(session, msg.ToArray()); if (Settings.Default.DebugMode) { Log($"RpcAgent send data, length:{msg.Size}"); } }
private void SendRpcResponse(TcpSocketSession session, Guid guid, JObject result) { RpcResponsePayload payload = new RpcResponsePayload { Guid = guid, Result = result }; Message msg = Message.Create("rpc-response", payload.ToArray()); server.SendTo(session, msg.ToArray()); if (Settings.Default.DebugMode) { Log($"RpcAgent send data, length:{msg.Size}"); } }
public void SendToAsync(string sessionKey, byte[] data, int offset, int count) { if (!IsListening) { throw new InvalidOperationException("The server has stopped to listen."); } TcpSocketSession session = null; if (_sessions.TryGetValue(sessionKey, out session)) { _server.BeginSendTo(session, data, offset, count); } else { _log.WarnFormat("SendToAsync, cannot find target client [{0}].", sessionKey); } }
public static void Send <T>(TcpSocketSession client, T t) { client.Send(ProtoBufHelper.ObjectToBytes <T>(t)); }
public static void SendJson(TcpSocketSession client, string t) { byte[] buffer = Encoding.UTF8.GetBytes(t); client.Send(buffer); }