Exemplo n.º 1
0
    public void TryLogin()
    {
        Debug.Log("[TryLogin]");

        bool isCreated = tcpClient.init("127.0.0.1", "3566", (isConnectTcp) => {
            if (isConnectTcp)
            {
                Debug.Log("tcp 服务器连接成功");

                GameMessagehandler.init();

                UserLoginRequest request = new UserLoginRequest();

                request.UserName = "******";

                tcpClient.SendPacket(request);
            }
            else
            {
                Debug.Log("tcp 服务器连接失败!!!");
            }
        });

        if (isCreated)
        {
            Debug.Log("创建网络成功");
        }
        else
        {
            Debug.Log("创建网络失败!!!");
        }
    }
Exemplo n.º 2
0
    public void OnRun()
    {
        if (m_socketState == SocketState.ConnectSuccess)
        {
            if (m_callbackOnConnect != null)
            {
                m_callbackOnConnect(true);
            }

            m_socketState = SocketState.ConnectEnd;
        }
        else if (m_socketState == SocketState.ConnectFail)
        {
            if (m_callbackOnConnect != null)
            {
                m_callbackOnConnect(false);
            }
            m_socketState = SocketState.ConnectEnd;
        }
        else if (m_socketState == SocketState.SocketError)
        {
            if (m_callbackOnDisConnect != null)
            {
                m_callbackOnDisConnect();
            }
            m_socketState = SocketState.ConnectEnd;
        }
        if (m_isConnect == false)
        {
            return;
        }
        if (m_needClose == true)
        {
            Close();

            return;
        }


        //发包队列
        if (m_ReceivePackList != null && m_ReceivePackList.Count != 0)
        {
            Queue <IMessage> sendQueue = new Queue <IMessage> ();

            lock (m_receiveObject) {
                while (m_ReceivePackList.Count > 0)
                {
                    sendQueue.Enqueue(m_ReceivePackList.First.Value);

                    m_ReceivePackList.RemoveFirst();
                }
            }
            while (sendQueue.Count > 0)
            {
                GameMessagehandler.MessageDispatch(sendQueue.Dequeue());
            }
        }
    }
Exemplo n.º 3
0
    //消息id

    //收包轮循
    private void ReceiveLoopThread()
    {
        Debug.Log("[ReceiveLoopThread]");

        while (m_socket != null && m_isConnect == true && m_needClose == false && m_socket.Connected)
        {
            try{
                if (m_socket.Available > 0)
                {
                    if (piece == null && msglen == 0 && m_socket.Available > PACKET_HEAD_SIZE)                       //接收包头

                    {
                        piece = new byte[PACKET_HEAD_SIZE];
                        //获取头 2 个字节
                        m_socket.Receive(piece, PACKET_HEAD_SIZE, SocketFlags.None);

                        Array.Reverse(piece);
                        msglen = BitConverter.ToInt16(piece, 0);
                    }
                    if (piece != null && msglen > 0)                        //接收包

                    {
                        byte[] bodyBytes = new byte[msglen];

                        int receiveBuffSize = 0;

                        while (receiveBuffSize < msglen && m_isConnect == true)
                        {
                            int tempSize = m_socket.Receive(bodyBytes, receiveBuffSize, msglen - receiveBuffSize, SocketFlags.None);

                            receiveBuffSize += tempSize;

                            Thread.Sleep(1);
                        }
                        if (receiveBuffSize == msglen)
                        {
                            var idBytes = new byte[2] {
                                bodyBytes[0], bodyBytes[1]
                            };
                            Array.Reverse(idBytes);
                            UInt16 msgId = (UInt16)BitConverter.ToInt16(idBytes, 0);

                            var dataBytes = new byte[bodyBytes.Length - 2];
                            System.Buffer.BlockCopy(bodyBytes, 2, dataBytes, 0, dataBytes.Length);
                            IMessage packet = GameMessagehandler.DeserializePacket(dataBytes, msgId);

                            lock (m_receiveObject) {
                                m_ReceivePackList.AddLast(packet);
                            }

                            piece = null;
                        }
                        else
                        {
                            Debug.LogError("ReceiveLoopThread error 接收包长度错误");

                            m_needClose = true;
                        }
                    }
                }
                else
                {
                    Thread.Sleep(10);
                }
            }
            catch (Exception e) {
                Debug.LogError("ReceiveLoopThread error " + e);

                m_needClose = true;
            }
        }
        Debug.Log("ReceiveLoopThread die");
    }
Exemplo n.º 4
0
 public static void Init()
 {
     GameMessagehandler.setReflect(typeof(Hello), (UInt16)MessageId.Hello);
     GameMessagehandler.setReflect(typeof(UserLoginRequest), (UInt16)MessageId.UserLoginRequest);
 }