示例#1
0
        private void OnNewConnection(IConnection connection)
        {
            ulong   sessionId = (ulong)Interlocked.Increment(ref this.totalSessionCount);
            string  client    = connection.RemoteEndPoint.ToString();
            Session session   = new Session(this, sessionId, this.OnInitSession, connection, this.protocol, this.instrumentation);

            Interlocked.Increment(ref this.activeSessionCount);
            RingMasterServerEventSource.Log.SessionCreated(sessionId, connection.Id, client);
            this.instrumentation?.OnSessionCreated(sessionId, client);

            PacketReceiveDelegate packetReciever = this.protocol.PacketReciever;

            if (packetReciever != null)
            {
                connection.DoPacketReceive = packetReciever;
            }

            connection.OnPacketReceived = packet => session.OnPacketReceived(packet);

            connection.OnConnectionLost = () =>
            {
                session.Close();
                Interlocked.Decrement(ref this.activeSessionCount);
                RingMasterServerEventSource.Log.SessionClosed(sessionId, connection.Id, client);
                this.instrumentation?.OnSessionClosed(sessionId, client);
            };
        }
示例#2
0
        private void OnNewConnection(IConnection connection)
        {
            ulong            sessionId = (ulong)Interlocked.Increment(ref this.totalSessionCount);
            string           client    = connection.RemoteEndPoint.ToString();
            ZooKeeperSession session   = new ZooKeeperSession(this, sessionId, this.OnInitSession, connection, this.protocol, this.instrumentation);

            Interlocked.Increment(ref this.activeSessionCount);
            ZooKeeperServerEventSource.Log.SessionCreated(sessionId, connection.Id, client);
            this.instrumentation?.OnSessionCreated(sessionId, client);

            PacketReceiveDelegate packetReciever = this.protocol.PacketReceiver;

            if (packetReciever != null)
            {
                connection.DoPacketReceive = packetReciever;
            }

            connection.OnPacketReceived = packet =>
            {
                Task.Run(
                    async() =>
                {
                    try
                    {
                        await session.OnPacketReceived(packet);
                    }
                    catch (Exception ex)
                    {
                        ZooKeeperServerEventSource.Log.OnPacketReceived_Failed(session.Id, ex.ToString());
                    }
                },
                    this.cancellationToken);
            };

            connection.OnConnectionLost = () =>
            {
                session.Close();
                Interlocked.Decrement(ref this.activeSessionCount);
                ZooKeeperServerEventSource.Log.SessionClosed(sessionId, connection.Id, client);
                this.instrumentation?.OnSessionClosed(sessionId, client);
            };
        }