private void OnClientDisconnect(PcsClient client, Member member)
        {
            lock (@lock)
            {
                connectedClients.Remove(client);

                Console.WriteLine(Messages.Server.ClientDisconnect, member);
            }
        }
Ejemplo n.º 2
0
        public ClientConnectionHandler(PcsClient client,
                                       Action <Member> signIn,
                                       Action <BroadcastMessage> addMessage,
                                       Action <PcsClient, Member> disconnect)
        {
            bool connected = true;

            while (connected)
            {
                try
                {
                    Packet packet = client.ReceivePacket();

                    switch (packet)
                    {
                    case SignInPacket signInPacket:
                    {
                        m_signedInMember = signInPacket.Member;
                        signIn(m_signedInMember);
                        signedIn = true;
                    }
                    break;

                    case MessagePacket messagePacket when signedIn:
                    {
                        var message = new BroadcastMessage(messagePacket.Message, DateTime.Now, m_signedInMember);
                        addMessage(message);
                    }
                    break;

                    case DisconnectPacket _ when signedIn:
                    {
                        connected = false;
                    }
                    break;
                    }
                }
                catch (SocketException) // When An existing connection was forcibly closed by the remote host
                {
                    connected = false;
                }
            }

            client.Disconnect();
            disconnect(client, m_signedInMember);
        }