Exemplo n.º 1
0
        void StartServer()
        {
            if (!NetworkServer.active)
            {
                NetworkServer.RegisterHandler((short)PTEvent.Connect, Handler_OnConnected);
                NetworkServer.RegisterHandler(MsgType.Disconnect, Handler_OnDisconnect);
                NetworkServer.RegisterHandler((short)PTEvent.Data, Handler_OnData);
                NetworkServer.RegisterHandler((short)PTEvent.Ready, Handler_OnReadyReceived);
                NetworkServer.RegisterHandler((short)PTEvent.ConfirmReadyReceived, Handler_OnConfirmReady);
                NetworkServer.Listen(PTNetwork.DEFAULT_SERVER_PORT);
            }

            PTSession newSession = new PTSession();

            newSession.senderName = playerName;
            newSession.gameName   = gameName;
            newSession.port       = NetworkServer.listenPort;
            newSession.data       = Encoding.Unicode.GetBytes(PTNetwork.DEFAULT_BROADCAST_DATA);

            if (nd.running)
            {
                nd.StopBroadcast();
            }
            nd.broadcastData = newSession.ToString();
            nd.Initialize();
            nd.StartAsServer();
        }
Exemplo n.º 2
0
        void Handler_OnBroadcastReceieved(PTSession session)
        {
            PTDebug("OnBroadcastReceieved: " + session.ToString(true));

            try
            {
                nearbySessions.Add(session);
                tempNearbySessionSet.Add(session);
            }
            catch
            {
            }
        }
Exemplo n.º 3
0
        public override void OnReceivedBroadcast(string fromAddress, string data)
        {
            //Corner case
            if (PTManager.singleton == null)
            {
                return;
            }

            //Get the new session by received broadcast data
            string serverAddress = fromAddress.Contains("::ffff:") ?
                                   fromAddress.Substring(7) : fromAddress;

            try
            {
                //New the PTBroadcastMessage
                //print(data);
                PTSession session = PTSession.FromJson(data);
                //print(session);

                session.ip = serverAddress;


                //invoke DiscoveredSession on PTHandHeld
                if (PTManager.OnBroadcastReceieved != null)
                {
                    PTManager.OnBroadcastReceieved(session);
                }
            }
            catch (ArgumentException e)
            {
                Debug.LogError(e);
            }
            catch (Exception)
            {
                //Debug.LogError("Unknown Error: OnReceivedBroadcast: " + data);
            }
        }
Exemplo n.º 4
0
        IEnumerator <CustomYieldInstruction> ConnectCoroutine(PTSession session)
        {
            if (!myClient.isConnected && session != null)
            {
                //
                ResetClient();

                //Check the player's name is valid (no duplica)
                //bool isClientNameValid = true;
                if (isClientNameValid())
                {
                    //Actuall connect happens here
                    myClient.Connect(session.ip, session.port);

                    //wait until the connection is established
                    yield return(new WaitUntil(() => myClient.isConnected));

                    //Send the connected event msg to server
                    PTMessage msg = new PTMessage();
                    msg.senderName = playerName;
                    myClient.Send((short)PTEvent.Connect, msg);
                }
            }
        }
Exemplo n.º 5
0
        /*API*/
        public static void Connect(string sender, string game)
        {
            PTSession session = PTSession.Find(sender, game);

            singleton.StartCoroutine(singleton.ConnectCoroutine(session));
        }