Exemplo n.º 1
0
        public void StartBroadCastPing (string _name) {
            if (string.IsNullOrEmpty (mBroadcastManager.GetLocalIP ())) {
                isServerOn = false;
            } else {
                myIP = mBroadcastManager.GetLocalIP ();
                mBroadcastManager.StartUDPServer (_name, Server_OnJoinReqReceived);

                smp = new RoomBroadcastPacket ();
                smp.serverName = _name;
                smp.ip = (mBroadcastManager.GetServer ().LocalEndPoint as IPEndPoint).Address.ToString ();
                smp.port = (mBroadcastManager.GetServer ().LocalEndPoint as IPEndPoint).Port;
                pingData = NetworkUtil.GetBytes (JsonUtility.ToJson (smp));

                isServerOn = true;
            }
        }
Exemplo n.º 2
0
        // private void StartChat () {
        //     //Debug.Log("starting chat..");
        //     m_ChatBox.gameObject.SetActive (true);
        //     m_LobbyBox.gameObject.SetActive (false);
        // }
        #endregion

        #region  BEFORE_CONNECTION i.e. network discovery stuff
        private void AddNewHost()
        {
            RoomBroadcastPacket serverInfo = mCacheRoomPacket;
            GameObject          go         = Instantiate(m_JoinBtnPrefab, m_HostListHolder);

            Button          goBtn       = go.GetComponent <Button> ();
            TextMeshProUGUI goText      = go.GetComponentInChildren <TextMeshProUGUI> ();
            JoinReqPacket   joinReqPack = new JoinReqPacket();

            //Debug.Log("Adding new host");
            // if i am the host, i dont need to add myself, just showing button to explicitly say that we are online searching for other players/users.
            if (string.Equals(HOST_IP, serverInfo.ip))
            {
                goBtn.interactable = false;
                goText.text        = "(YOU) " + HOST_NAME;

                joinReqPack.clientIP   = HOST_IP;
                joinReqPack.clientName = HOST_NAME;
                OnRoomJoinReq(joinReqPack);        //to update the ui
                StartAsPlayer(HOST_IP, GAME_PORT); // If i am the server, i can also start as player as well.
            }
            else
            {
                goBtn.onClick.AddListener(() => {
                    Debug.Log("sending join request to ::" + serverInfo.ip + " : " + serverInfo.port);
                    try {
                        //TODO check if we need local end point here
                        Socket sock            = m_NetworkDiscovery.GetNetworkSocket();
                        joinReqPack.clientIP   = (sock.LocalEndPoint as IPEndPoint).Address.ToString();
                        joinReqPack.clientName = HOST_NAME;

                        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(serverInfo.ip), serverInfo.port);
                        sock.SendTo(NetworkUtil.GetBytes(JsonUtility.ToJson(joinReqPack)), (EndPoint)remoteEndPoint);
                        //TODO, improvement :: ACKNOWLEDGEMENT
                        StartAsPlayer(serverInfo.ip, GAME_PORT);
                        goBtn.interactable = false;
                        goText.text        = "Joining...";
                    } catch (Exception _exc) {
                        Debug.Log("EXC: " + _exc.Message);
                        goBtn.interactable = true;
                    }
                });
                goText.text = serverInfo.serverName + "'s Room";
            }
            m_HostListHolder.gameObject.SetActive(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// These functions are running on a different thread, not in the main thread,
        /// so if any error happens, i wont get anything on console unless used with try/catch Debug.Log function
        /// </summary>
        private void OnMsgReceivedByClient(string _msg)
        {
            mIsGameStarted = true;
            byte[] receivedBytes = NetworkUtil.GetBytes(_msg);
            Debug.Log(_msg);
            Debug.Log("received by mono " + Encoding.ASCII.GetString(receivedBytes));

            try {
                MemoryStream    memStream = new MemoryStream();
                BinaryFormatter binForm   = new BinaryFormatter();
                memStream.Write(receivedBytes, 0, receivedBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                MethodPacket obj = (MethodPacket)binForm.Deserialize(memStream);
                mMethodStack.Enqueue(obj);
                Debug.Log("Method successfully enqued...");
            } catch (Exception _exc) {
                Debug.LogError("ERROR in processing method packet: " + _exc.Message);
            }

            //====Right below, it was the initial prototype without using C# reflection, it was becoming difficult to maintain with packets,
            //====so genuinly felt the need to use a more robust way to implement rpc calls

            // IMessagePacket mp = JsonUtility.FromJson<IMessagePacket>(_msg);
            // Debug.Log("Client received :: " + mp);
            // switch ((MessageType)mp.msgType)
            // {
            //     case MessageType.START_GAME:
            //         isGameStarted = true;
            //         packetStack.Enqueue(mp);
            //         break;
            //     case MessageType.SIMPLE_MSG:
            //         SimpleMessagePacket smp = JsonUtility.FromJson<SimpleMessagePacket>(_msg);
            //         packetStack.Enqueue(smp);
            //         break;
            //     default:
            //         break;
            // }
        }