예제 #1
0
        /// <summary>
        /// Creates a new player in the game, sends it out to all the clients,
        /// and then sends that active player to the clientSocket that is
        /// specified
        /// </summary>
        /// <param name="clientSocket">
        /// The socket that needs an active player
        /// </param>
        private void ProcessNewPlayer(Socket clientSocket)
        {
            PlayerServer player = GameServer.instance.CreateNewPlayer();

            //Associate player's id with with the socket.
            playerDictionary.Add(clientSocket, player.Id);

            BasePacket createPlayPack = ServerPacketFactory.NewCreatePacket(player);

            // Create createObjectPacket, send to client
            byte[] data = PacketUtil.Serialize(createPlayPack);
            Send(clientSocket, data);
            MatchStartPacket informStart =
                new MatchStartPacket(MatchHandler.instance.GetMatch().GetTimeElapsed().Milliseconds);

            Send(clientSocket, PacketUtil.Serialize(informStart));
        }
예제 #2
0
        /// <summary>
        /// Connect to a requesting socket.
        /// </summary>
        /// <param name="ar">Stores socket and buffer data</param>
        public void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();

            // Get the socket that handles the client request.
            Socket listener     = (Socket)ar.AsyncState;
            Socket clientSocket = listener.EndAccept(ar);

            if (MatchHandler.instance.GetMatch().Started())
            {
                //Sending match time first so it's processed first.
                MatchStartPacket informStart =
                    new MatchStartPacket((float)(Constants.MATCH_TIME -
                                                 MatchHandler.instance.GetMatch().GetTimeElapsed().TotalSeconds));
                Send(clientSocket, PacketUtil.Serialize(informStart));
            }

            // Create a new player and send them the world
            SendWorldToClient(clientSocket);
            if (GameServer.instance.playerServerList.Count < Constants.NUM_PLAYERS)
            {
                ProcessNewPlayer(clientSocket);
            }
            else
            {
                SendSpectator(clientSocket);
            }

            // Add the new socket to the list of sockets recieving updates
            clientSockets.Add(clientSocket);

            GameServer.instance.ConnectCallback();
            // Create the state object.
            StateObject state = new StateObject();

            state.workSocket = clientSocket;
            clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                      new AsyncCallback(ReceiveCallback), state);

            //Start listening for the next connection
            listener.BeginAccept(
                new AsyncCallback(AcceptCallback),
                listener);
        }