Ejemplo n.º 1
0
 protected override void OnDisconnected()
 {
     lock (stateLocker)
     {
         currState = ClientNetworkState.Disconnected;
     }
 }
Ejemplo n.º 2
0
 protected override void OnError(System.Net.Sockets.SocketError error)
 {
     lock (stateLocker)
     {
         currState = ClientNetworkState.Error;
     }
 }
Ejemplo n.º 3
0
        public void DoUpdate(float deltaTime)
        {
            lock (stateLocker)
            {
                if (prevState != currState)
                {
                    if (currState == ClientNetworkState.Connected)
                    {
                        OnNetConnected?.Invoke();
                    }
                    else if (currState == ClientNetworkState.Disconnected)
                    {
                        OnNetDisconnected?.Invoke();
                    }
                    else if (currState == ClientNetworkState.Error)
                    {
                        OnNetError?.Invoke();
                    }
                    prevState = currState;
                }
            }

            lock (bufferLocker)
            {
                if (dataBuffer.Length > 0 && IsConnected)
                {
                    int    index     = 1;
                    byte[] dataBytes = dataBuffer.ReadMessage();
                    while (dataBytes != null && (ReadCountAtOnce < 0 || index < ReadCountAtOnce))
                    {
                        OnDataReceived?.Invoke(dataBytes);
                        dataBytes = dataBuffer.ReadMessage();
                        index++;
                    }
                }
            }
        }
Ejemplo n.º 4
0
    public void ChangeState(ClientNetworkState state)
    {
        switch (state)
        {
        case ClientNetworkState.EndGame:
            Cursor.visible = true;
            if (listnerThread != null)
            {
                listnerThread.Abort();
            }
            if (udpClient != null)
            {
                udpClient.Close();
            }
            SceneManager.LoadScene("EndGame");
            break;

        case ClientNetworkState.Destroy:
            OnApplicationQuit();
            SceneManager.LoadScene("Server");
            Destroy(this);
            break;

        case ClientNetworkState.InGame:
        {
            networkSendList.Clear();
            networkReceivedList.Clear();
            cnm          = new ClientNetworkMessage();
            cnm.first_id = 0;
            cnm.first    = cnm.second = cnm.third = null;

            if (SceneManager.GetActiveScene().name != "InGame")
            {
                SceneManager.LoadScene("InGame");
            }
            if (listnerThread != null)
            {
                listnerThread.Abort();
            }
            listnerThread = new Thread(new ThreadStart(InGameListner));
            listnerThread.Start();
        }
        break;

        case ClientNetworkState.Matching:
        {
            if (currentState == ClientNetworkState.Matching && udpClient != null)
            {
                try
                {
                    byte[] buf2 = new byte[1];
                    buf2[0] = (byte)CTSType.Disconnect;
                    udpClient.SendTo(buf2, targetEndPoint);
                }
                catch
                {
                    udpClient = null;
                    ChangeState(ClientNetworkState.Matching);
                    return;
                }
            }
            Cursor.visible = true;
            targetIp       = "";
            // 서버 아이피를 받음
            if (udpClient != null)
            {
                udpClient.Close();
            }
            if (listnerThread != null)
            {
                listnerThread.Abort();
            }
            if (SceneManager.GetActiveScene().name != "Matching")
            {
                SceneManager.LoadScene("Matching");
            }
            if (SceneManager.GetSceneByName("Matching").GetRootGameObjects().Length > 0)
            {
                var       objects       = SceneManager.GetSceneByName("Matching").GetRootGameObjects();
                Transform root_Matching = objects[0].transform;
                foreach (var myObject in objects)
                {
                    if (myObject.transform.name == "Canvas")
                    {
                        root_Matching = myObject.transform;
                    }
                }
                var mtext = root_Matching.transform.Find("MatchingText").GetComponent <UnityEngine.UI.Text>();
                if (mtext != null)
                {
                    mtext.text = "매칭 중!";
                }
                var mtext2 = root_Matching.transform.Find("Button").Find("MatchingButtonText").GetComponent <UnityEngine.UI.Text>();
                if (mtext2 != null)
                {
                    mtext2.text = "매칭 취소";
                }
            }

            StreamReader reader   = new StreamReader("server.txt");
            var          serverIp = reader.ReadLine();
            reader.Close();
            print(serverIp + "에 요청");

            // 서버에 매칭 요청을 함.
            IPEndPoint serverEndpoint_IP = new IPEndPoint(IPAddress.Parse(serverIp), CS_PORT);
            targetEndPoint = (EndPoint)serverEndpoint_IP;
            IPEndPoint Remote_IP      = new IPEndPoint(IPAddress.Any, 0);
            EndPoint   remoteEndpoint = (EndPoint)Remote_IP;
            udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpClient.ReceiveTimeout = 5000;
            udpClient.SendTimeout    = 5000;
            byte[] buf = new byte[1];
            buf[0] = (byte)CTSType.Matching;
            udpClient.SendTo(buf, targetEndPoint);
            byte[] data = new byte[1024];
            try
            {
                udpClient.ReceiveFrom(data, ref remoteEndpoint);
                if ((STCType)data[0] != STCType.Connected)
                {
                    SceneManager.LoadScene("Aleart");
                }
                else
                {
                    // 모든게 성공했다면 쓰레드에서 대기한다
                    listnerThread = new Thread(new ThreadStart(MatchingListner));
                    listnerThread.Start();
                }
            }
            catch (Exception e)
            {
                print(e);
                SceneManager.LoadScene("Aleart");
            }
        }
        break;

        case ClientNetworkState.Ready:
        {
            Cursor.visible = true;
            if (currentState == ClientNetworkState.Matching && udpClient != null)
            {
                byte[] buf = new byte[1];
                buf[0] = (byte)CTSType.Disconnect;
                udpClient.SendTo(buf, targetEndPoint);
            }
            if (listnerThread != null)
            {
                listnerThread.Abort();
            }
            if (udpClient != null)
            {
                udpClient.Close();
            }
            if (SceneManager.GetActiveScene().name != "Matching")
            {
                SceneManager.LoadScene("Matching");
            }

            if (SceneManager.GetSceneByName("Matching").GetRootGameObjects().Length > 0)
            {
                var       objects       = SceneManager.GetSceneByName("Matching").GetRootGameObjects();
                Transform root_Matching = objects[0].transform;
                foreach (var myObject in objects)
                {
                    if (myObject.transform.name == "Canvas")
                    {
                        root_Matching = myObject.transform;
                    }
                }
                var mtext = root_Matching.transform.Find("MatchingText").GetComponent <UnityEngine.UI.Text>();
                if (mtext != null)
                {
                    mtext.text = "매칭 시작을 눌러주세요.";
                }
                var mtext2 = root_Matching.transform.Find("Button").Find("MatchingButtonText").GetComponent <UnityEngine.UI.Text>();
                if (mtext2 != null)
                {
                    mtext2.text = "매칭 시작";
                }
            }
            break;
        }

        default:
            print("Unexpected State");
            break;
        }
        currentState = state;
        print("상태 전환 : " + currentState);
    }