Esempio n. 1
0
        private static void SendAllKnownAddresses(Client client)
        {
            string addressesSql = "SELECT * from KnownAddresses WHERE NodeID = @nodeid;";

            try
            {
                SQLiteCommand command = new SQLiteCommand(addressesSql, Database.Connection);
                command.Parameters.AddWithValue("@nodeid", client.Node.ID);

                // TODO: This really should send a single message which contains all the addresses.
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string          address  = reader.GetString(reader.GetOrdinal("Address"));
                    string          hostname = reader.GetString(reader.GetOrdinal("Hostname"));
                    Common.NodeType type     = (Common.NodeType)reader.GetInt32(reader.GetOrdinal("Type"));

                    AsyncSocketListener.Send(client.GetSocket(), Messages.UpdateKnownAddressMessage(address, hostname, type));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public static void Shutdown()
        {
            if (!m_Initialised)
            {
                return;
            }

            AsyncSocketListener.StopListening();

            m_Database.Shutdown();

            Logger.Write(Logger.Level.Info, "Server shutdown.");
            m_Initialised = false;
        }
Esempio n. 3
0
        public static void Initialise()
        {
            Logger.Write(Logger.Level.Info, "Server initialising...");

            m_Database       = new Database();
            m_Clients        = new List <Client>();
            m_SocketToClient = new Dictionary <Socket, Client>();
            m_SocketListener = new AsyncSocketListener();
            AsyncSocketListener.OnConnectionAccepted = OnConnectionAccepted;
            AsyncSocketListener.OnConnectionLost     = OnConnectionLost;
            AsyncSocketListener.OnMessageReceived    = OnMessageReceived;
            AsyncSocketListener.StartListening();

            m_Initialised = true;
        }
Esempio n. 4
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);
        }