public RpcTcpDuplexConnection(RpcTcpClientChannel channel, TcpUri serverUri) : base(RpcConnectionMode.Duplex, RpcConnectionDirection.Client) { _serverUri = serverUri; _socket = new RpcTcpSocketConnection(RpcConnectionDirection.Client); _socket.Disconnected += new Action <RpcTcpSocketConnection>( (socket) => { OnDisconnected(); } ); _socket.RequestReceived += new Action <RpcTcpSocketConnection, int, RpcRequest>( (socket, seq, request) => { var tx = new RpcTcpServerTransaction(_channel, this, socket, request, seq); OnTransactionCreated(tx); } ); _socket.ResponseReceived += new Action <RpcTcpSocketConnection, int, RpcResponse>( (socket, seq, response) => { RpcTcpTransactionManager.EndTransaction(seq, response); } ); _channel = new RpcDuplexCallbackChannel("tcp", serverUri.ToString(), this, channel); }
public RpcTcpServerConnection(RpcTcpServerChannel channel, RpcTcpSocketConnection sock) : base(RpcConnectionMode.Unknown, RpcConnectionDirection.Server) { _channel = channel; _sock = sock; _sock.RequestReceived += new Action <RpcTcpSocketConnection, int, RpcRequest>( (socket, seq, request) => { RpcTcpServerTransaction tx = new RpcTcpServerTransaction(_channel, this, socket, request, seq); OnTransactionCreated(tx); } ); _sock.ResponseReceived += new Action <RpcTcpSocketConnection, int, RpcResponse>( (socket, seq, response) => { RpcTcpTransactionManager.EndTransaction(seq, response); } ); _sock.Disconnected += new Action <RpcTcpSocketConnection>( (socket) => { OnDisconnected(); } ); }
private static void RecycleProc() { while (true) { try { Thread.Sleep(1000); List <RpcTcpSimplexConnectionWrapper> keys = new List <RpcTcpSimplexConnectionWrapper>(); lock (_syncRoot) { foreach (var k in _connections) { keys.Add(k.Key); } } // // 尝试回收一个连接 foreach (var k in keys) { k.TryRecycle(); } // // 关闭延时Queue中的Socket while (_closeQueue.Count > 0) { ComboClass <DateTime, RpcTcpSocketConnection> a; lock (_syncRoot) { a = _closeQueue.Peek(); if (DateTime.Now > a.V1) { a = _closeQueue.Dequeue(); } else { break; } } try { RpcTcpSocketConnection sock = a.V2; _tracing.InfoFmt("delay close connection: {0}", sock.RemoteUri); sock.Disconnect(); _tracing.InfoFmt("delay close connection ok {0}", sock.RemoteUri); } catch (Exception ex) { _tracing.Error(ex, "delay close connection failed"); } } } catch (ThreadAbortException) { Thread.ResetAbort(); } catch (Exception ex) { _tracing.Error(ex, "RecycleProc Failed"); } } }
/// <summary> /// 延时关闭Socket, 保证所有的事务都执行完毕 /// </summary> /// <param name="sock"></param> public static void DelayClose(RpcTcpSocketConnection sock) { DateTime t = DateTime.Now.AddSeconds(_delayCloseSeconds); _tracing.InfoFmt("enqueue delay close connection: {0} in {1}", sock.RemoteUri, t); _counter.ConnectionsRecycled.Increment(); sock.Recycling = true; lock (_syncRoot) { _closeQueue.Enqueue(new ComboClass <DateTime, RpcTcpSocketConnection>(t, sock)); } }
private void CreateConnection() { var sock = new RpcTcpSocketConnection(RpcConnectionDirection.Client); sock.Disconnected += new Action <RpcTcpSocketConnection>( (s) => { _parent.OnSubDisconnected(); } ); sock.RequestReceived += new Action <RpcTcpSocketConnection, int, RpcRequest>( (s, seq, request) => { // TODO // Not support this response } ); sock.ResponseReceived += new Action <RpcTcpSocketConnection, int, RpcResponse>( (s, seq, response) => { RpcTcpTransactionManager.EndTransaction(seq, response); } ); // // Auto Connect sock.BeginConnect( (TcpUri)_parent.RemoteUri, delegate(Exception ex) { if (ex != null) { _retryBegin = DateTime.Now.AddSeconds(1); } else { _parent.OnSubConnected(); } }, false ); _sock = sock; }
void ProcessAccept(SocketAsyncEventArgs e) { _tracing.InfoFmt("Accept Connection"); try { RpcTcpSocketConnection s = new RpcTcpSocketConnection(RpcConnectionDirection.Server); RpcTcpServerConnection conn = new RpcTcpServerConnection(this, s); conn.TransactionCreated += new Action <RpcConnection, RpcServerTransaction>( (cnn, tx) => { OnTransactionCreated(tx); } ); conn.Disconnected += new Action <RpcConnection>( (cnn) => { RemoveConnection((RpcTcpServerConnection)cnn); } ); // // 把事件都挂全了的,再beginrecieve s.Accept(e); // 按原来的逻辑此时conn.RemoteUri = null; lock (_syncRoot) { _connections.Add(conn.RemoteUri, conn); } _tracing.InfoFmt("Connection Created from {0}", conn.RemoteUri); OnConnectionCreated(conn); } catch (Exception ex) { try { e.AcceptSocket.Close(); } catch (Exception ex2) { _tracing.Error(ex2, "AcceptSocket.Close() Failed"); } _tracing.Error(ex, "ProcessConnect Failed"); } finally { e.AcceptSocket = null; BeginAccept(); } }
internal RpcTcpClientTransaction(ServerUri uri, RpcTcpSocketConnection sock, RpcRequest request) : base(uri, request) { _sock = sock; }
public RpcTcpServerTransaction(RpcServerChannel channel, RpcConnection conn, RpcTcpSocketConnection socket, RpcRequest request, int sequence) : base(channel, conn, request) { _sock = socket; Sequence = sequence; }
public static RpcTcpClientTransaction CreateClientTransaction(ServerUri serverUri, RpcTcpSocketConnection sock, RpcRequest request) { var tx = new RpcTcpClientTransaction(serverUri, sock, request); return(tx); }