Ejemplo n.º 1
0
        public static bool OnMessageReceived(Socket handler, byte[] buffer)
        {
            // The messages always have the same structure:
            // A MessageBase which contains an union of all the valid messages.
            ByteBuffer bb = new ByteBuffer(buffer);

            MessageBase messageBase = MessageBase.GetRootAsMessageBase(bb);

            switch (messageBase.DataType)
            {
            case MessageContainer.CreateAccountMessage:
            {
                CreateAccountMessage?message = messageBase.Data <CreateAccountMessage>();
                if (message == null)
                {
                    return(false);
                }

                Messages.CreateAccountResult result = CreateAccount(message.Value.Username, message.Value.Email, message.Value.Password);
                AsyncSocketListener.Send(handler, Messages.CreateAccountResultMessage(result));

                break;
            }

            case MessageContainer.LoginMessage:
            {
                LoginMessage?message = messageBase.Data <LoginMessage>();
                if (message == null)
                {
                    return(false);
                }

                Messages.LoginResult result = TryLogin(message.Value.Username, message.Value.Password);
                AsyncSocketListener.Send(handler, Messages.LoginResultMessage(result));

                if (result == Messages.LoginResult.Success)
                {
                    // TODO: a second find shouldn't be needed, as we're doing one in the login
                    Account account = Account.Find(message.Value.Username);

                    Client client = m_SocketToClient[handler];
                    client.AssociateAccount(account);
                    OnClientLogin(client);

                    AsyncSocketListener.Send(handler, Messages.UpdateAddressMessage(client.Node.NodeAddress.Value, account.Username));
                    AsyncSocketListener.Send(handler, Messages.UpdateCreditsMessage(account.Credits));
                    AsyncSocketListener.Send(handler, Messages.UpdateReputationMessage(account.Reputation));

                    SendAllKnownAddresses(client);
                }

                break;
            }

            case MessageContainer.NodeConnectMessage:
            {
                NodeConnectMessage?message = messageBase.Data <NodeConnectMessage>();
                if (message == null || message.Value.RouteLength == 0)
                {
                    return(false);
                }

                List <string> route = new List <string>();
                for (int i = 0; i < message.Value.RouteLength; ++i)
                {
                    route.Add(message.Value.Route(i));
                }

                int nodeErrorIndex;
                Messages.NodeConnectResult result = NodeConnect(route, out nodeErrorIndex);
                AsyncSocketListener.Send(handler, Messages.NodeConnectResultMessage(result, nodeErrorIndex));

                break;
            }

            case MessageContainer.PurchaseSoftwareMessage:
            {
                PurchaseSoftwareMessage?message = messageBase.Data <PurchaseSoftwareMessage>();
                if (message == null)
                {
                    return(false);
                }

                Client    client        = m_SocketToClient[handler];
                File.Type softwareToBuy = (File.Type)message.Value.Software;
                Messages.PurchaseSoftwareResult result = TryPurchaseSoftware(client, softwareToBuy);
                AsyncSocketListener.Send(handler, Messages.PurchaseSoftwareResultMessage(result));
                break;
            }

            default:
                return(false);
            }

            return(true);
        }