protected void AddIncomingSocket(Stream stream) { BcpDelegate.ProcessReadHead processReadHead = delegate(Bcp.ConnectionHead connectionHead) { var sessionId = connectionHead.SessionId; string sessionKey = Convert.ToBase64String(sessionId); var connectionId = connectionHead.ConnectionId; Debug.WriteLine("BcpServer add incomming socket, sessionId: " + sessionId + ", connectionId: " + connectionId); lock (serverLock) { BcpServer.Session session; if (sessions.TryGetValue(sessionKey, out session)) { } else { session = NewSession(sessionId); sessions.Add(sessionKey, session); session.RaiseAccepted(); } if (connectionHead.IsRenew) { session.RenewSession(); } session.AddStream(connectionId, stream); Debug.WriteLine("Server added stream!"); } }; BcpDelegate.ExceptionHandler exceptionHandler = delegate(Exception e) { Debug.WriteLine("BcpServer add incomming stream exception: " + e.Message); }; TimerCallback readTimeoutCallback = delegate(Object source) { stream.Dispose(); exceptionHandler(new Exception()); }; Bcp.ReadState readState = new Bcp.ReadState(); readState.readTimeoutTimer = new Timer(readTimeoutCallback, null, Bcp.ReadingTimeoutMilliseconds, Bcp.ReadingTimeoutMilliseconds); BcpIO.ReadHead(stream, readState, processReadHead, exceptionHandler); }
public static void ReadHead(Stream stream, Bcp.ReadState readState, BcpDelegate.ProcessReadHead processReadHead, BcpDelegate.ExceptionHandler exceptionHandler) { var sessionId = new byte[Bcp.NumBytesSessionId]; ProcessReadAll processReadAll = delegate() { ProcessReadVarint processReadIsRenew = delegate(uint isRenew) { ProcessReadVarint processReadConnectionId = delegate(uint connectionId) { readState.Cancel(); processReadHead(new Bcp.ConnectionHead(sessionId, Convert.ToBoolean(isRenew), connectionId)); }; ReadUnsignedVarint(stream, readState, processReadConnectionId, exceptionHandler); }; ReadUnsignedVarint(stream, readState, processReadIsRenew, exceptionHandler); }; ReadAll(stream, readState, sessionId, 0, Bcp.NumBytesSessionId, processReadAll, exceptionHandler); }