Beispiel #1
0
 private void Recv(IAsyncResult res)
 {
     try {
         if (udp.Client == null)
         {
             return;
         }
         IPEndPoint remote = new IPEndPoint(IPAddress.Any, port);
         byte[]     bytes  = udp.EndReceive(res, ref remote);
         var        msg    = new UdpMessage(remote, bytes, this);
         messagesIn.AddLast(msg);
         Debug.Log(remote.Address.ToString() + ": " + msg.Code.ToString());
         udp.BeginReceive(Recv, null);
     } catch (Exception ex) {
         Debug.LogError("recv() failed: " + ex.ToString());
         StopListening();
     }
 }
Beispiel #2
0
 private void ProcessServerMessage(UdpMessage msg)
 {
     switch (msg.Code)
     {
     case CommandCode.Connect:
         MasterController.Instance.MultiplayerGameStarted();
         Debug.Log("Incoming connection: " + msg.Remote.Address.ToString());
         destAddr = msg.Remote;
         msg.Respond(new UdpMessage(msg.Remote, new CommandConnectEstablished(udpHandler.SessionId)));
         GameController.Instance.paddleRed.InitializePaddle(0, PaddleColors.Red, true, true);
         udpHandler.SendMessage(new UdpMessage(destAddr, new CommandPaddleInitialized(0, PaddleColors.Red, true, false)));
         GameController.Instance.paddleBlue.InitializePaddle(1, PaddleColors.Blue, false, false);
         udpHandler.SendMessage(new UdpMessage(destAddr, new CommandPaddleInitialized(1, PaddleColors.Blue, false, true)));
         GameController.Ball.Spawn(GameController.Instance.PaddleRed);
         udpHandler.SendMessage(new UdpMessage(destAddr, new CommandBallSpawn(0)));
         break;
     }
 }
Beispiel #3
0
        private bool ProcessGeneralMessage(UdpMessage msg)
        {
            switch (msg.Code)
            {
            case CommandCode.PaddleMove:
                PaddleMoved(msg.cmd as CommandPaddleMove);
                return(true);

            case CommandCode.BallLaunch:
                BallLaunched(msg.cmd as CommandBallLaunch);
                return(true);

            case CommandCode.ScoreUpdate:
                ScoreUpdated(msg.cmd as CommandScoreUpdate);
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        private void ProcessClientMessage(UdpMessage msg)
        {
            switch (msg.Code)
            {
            case CommandCode.ConnectEstablished:
                ConnectionEstablished(msg.cmd as CommandConnectEstablished);
                break;

            case CommandCode.PaddleInitialized:
                PaddleInitialized(msg.cmd as CommandPaddleInitialized);
                break;

            case CommandCode.BallSpawn:
                BallSpawned(msg.cmd as CommandBallSpawn);
                break;

            case CommandCode.BallUpdate:
                BallUpdated(msg.cmd as CommandBallUpdate);
                break;
            }
        }