Beispiel #1
0
        private void HandleConnectedClient(NetworkClient client)
        {
            while (client.IsConnected)
            {
                var message = client.ReadMessage();
                if (message == null)
                {
                    break;
                }

                Action<NetworkClient, Message> handler = null;
                messageHandlers.TryGetValue(message.Type, out handler);
                if (handler == null)
                {
                    Console.WriteLine("Error, unable to find a message handler for message type: {0}", message.Type);
                    continue;
                }

                handler(client, message);
            }

            // If the handler no longer running, do some clean up here
            BroadcastAll(client, Message.Create(MessageType.UserDisconnected, client.UserSession));
            client.Disconnect();
            connectedClients.Remove(client);

            // refactor this
            RemoveUserFromChannel(client.UserSession);
            Console.WriteLine("User Disconnected: {0}, now you have {1} users connected",
            (object)(client.UserSession == null ? "<unknown user>" : client.UserSession.UserInfo.Username),
                connectedClients.Count);
        }
Beispiel #2
0
        public void Connect(string host, int port, string username, string password, bool createNewUser)
        {
            if (networkClient != null)
            {
                Disconnect();
            }

            networkClient = NetworkClient.Connect(host, port);

            if (createNewUser)
            {
                networkClient.WriteMessage(Message.Create(MessageType.CreateUserRequest,
                    new UserInfo()
                    {
                        Username = username,
                        Password = password
                    }));
                var createUserResponse = networkClient.ReadMessage().GetData<SimpleResponse>();
                if (!createUserResponse.Success)
                {
                    OnConnected(false, createUserResponse.Message);
                    return;
                }
            }

            networkClient.WriteMessage(Message.Create(MessageType.CredentialRequest, new UserInfo() { Username = username, Password = password }));
            var response = networkClient.ReadMessage().GetData<UserCredentialResponse>();
            if (response.IsAuthenticated)
            {
                networkClient.UserSession = response.UserSession;

                Task.Factory.StartNew(() =>
                {
                    StartReading();

                    // handle disconnecting client if client is in bad state
                    Disconnect();
                }, TaskCreationOptions.LongRunning);

                soundEngine.Record();
                OnConnected(true, response.Message);
                networkClient.WriteMessage(Message.Create(MessageType.GetAllChannelsRequest));
            }
            else
            {
                OnConnected(false, response.Message);
                networkClient.Disconnect();
            }
        }