Esempio n. 1
0
        public void OnIDRequest(object sender, IDRequestEventArgs e)
        {
            ClientHost clientHost = (ClientHost)sender;
            GameData   gameData   = _model.Update();
            int        id         = e.ID;
            string     password   = e.Password;

            //
            //	Permission is checked at client level, here we just check to see
            //   if the player ID has already been assigned to another client
            //

            //Cycle through Client Hosts, if any have requested ID,
            //do nothing - no update, probably should send a rejection notice at
            // some point, we'll see if its necessary
            foreach (ClientHost tempClient in _clientHostArray)
            {
                if (tempClient != null && tempClient.PlayerID == id)
                {
                    return;
                }
            }
            this._playerIDArray[id]          = clientHost;
            gameData.PlayerList[id].Name     = clientHost.UserName;
            gameData.PlayerList[id].Password = password;
            clientHost.PlayerID = id;
            Console.WriteLine(clientHost.UserName + " is assigned slot " + id);
            foreach (ClientHost tempClient in _clientHostArray)
            {
                if (tempClient != null)
                {
                    tempClient.SendID(id, clientHost.UserName);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Event Handler for Disconnteced
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnDisconnected(object sender, EventArgs e)
        {
            _numClients--;
            ClientHost temp = (ClientHost)sender;

            _clientHostArray[temp.ID] = null;
            Console.WriteLine(temp.UserName + " disconnected at slot " + temp.ID);
            foreach (ClientHost client in _clientHostArray)
            {
                if (client != null)
                {
                    client.SendText(temp.UserName + " disconnected from slot " + temp.ID);
                }
            }
        }
Esempio n. 3
0
        public void StartListen()
        {
            try
            {
                //
                //	Consider a dialog displaying IP and allowing custom
                //	port selection.
                //

                //	Start the tcpListener
                _listener = new TcpListener(5220);
                _listener.Start();
                do
                {
                    //
                    //	Here we'll need to figure out how to handle new clients
                    //  taking into consideration options we'll offer to new
                    //  connections -- i.e. joining game in progress, re-starting
                    //  saved game, choosing slot... passwords?. Lets consider
                    //	keeping the Guid/Hashtable stategy for now as a way of
                    //  positively identifying players/clients, perhaps assigning
                    //  Clients to Player objects later in the process
                    //
                    ClientHost newClient = new ClientHost(_listener.AcceptTcpClient());
                    if (newClient != null)
                    {
                        Console.WriteLine("Client Connected");
                    }
                    //Attach the Delegates
                    newClient.eDisconnected    += new DisconnectDelegate(OnDisconnected);
                    newClient.eConnected       += new ConnectDelegate(this.OnConnected);
                    newClient.eMessageReceived += new MessageDelegate(OnMessageReceived);
                    newClient.eReady           += new ReadyDelegate(OnReady);
                    newClient.eIDRequest       += new IDRequestDelegate(OnIDRequest);
                    newClient.eRenameSystem    += new RenameSystemDelegate(OnRenameSystem);
                    newClient.eTradeMessage    += new TradeMessageDelegate(OnTradeMessage);
                    //Connect to the clients
                    newClient.Connect();
                }while(true);
            }
            catch
            {
                _listener.Stop();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Event Handler for MessageReceived
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnMessageReceived(object sender, MessageEventArgs e)
        {
            //Message sender client
            ClientHost temp = (ClientHost)sender;

            Console.WriteLine(temp.UserName + ": " + e.Message.Text);
            //for each valid client
            for (int i = 0; i < 8; i++)
            {
                if (_clientHostArray[i] != null)
                {
                    int ID = _clientHostArray[i].PlayerID;
                    //if they are in the bit array of targets
                    if (ID == -1 || e.Message.PlayerTargets[ID] == true)
                    {
                        _clientHostArray[i].SendText(temp.UserName + ":\r\n    " + e.Message.Text);
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Event Handler for Connected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnConnected(object sender, EventArgs e)
        {
            _numClients++;
            bool assigned = false;
            //Get the client that raised the event
            ClientHost temp = (ClientHost)sender;

            //Add the client to the first empty slot
            for (int i = 0; i < 8; i++)
            {
                if (_clientHostArray[i] == null)
                {
                    temp.ID             = i;
                    _clientHostArray[i] = temp;
                    assigned            = true;
                    break;
                }
            }
            if (!assigned)
            {
                temp.SendText("Sorry, server is full");
                temp.Disconnect();
                return;
            }

            Console.WriteLine(temp.UserName + " connected at slot " + temp.ID);
            //loop through each client and announce the
            //client connected
            foreach (ClientHost client in _clientHostArray)
            {
                if (client != null)
                {
                    client.SendText(temp.UserName + " connected at slot " + temp.ID);
                    Console.WriteLine("Server SendText called");
                }
            }
        }