Beispiel #1
0
    private void handleNetworkMessages(RakNet.RakPeerInterface rakPeerInterface)
    {
        RakNet.Packet packet;
        packet = null;

        while (true)
        {
            for (packet = rakPeerInterface.Receive(); packet != null;
                 rakPeerInterface.DeallocatePacket(packet),
                 packet = rakPeerInterface.Receive())
            {
                switch (packet.data[0])
                {
                case (int)RakNet.DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:
                    Debug.Log("A connection is incoming.");
                    SystemAddress sa = packet.systemAddress;
                    sendNewClientID(rakPeerInterface, ref sa);
                    packet.systemAddress = sa;
                    break;

                case (int)RakNet.DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                    Debug.Log("A client has disconnected.");
                    break;

                case (int)RakNet.DefaultMessageIDTypes.ID_CONNECTION_LOST:
                    Debug.Log("A client has lost the connection.");
                    break;

                case (int)GameMessages.ENUMGAMEMESSAGES.ID_CLIENT_CLIENT_DATA:
                {
                    RakNet.BitStream bitStream;
                    bitStream = new RakNet.BitStream(packet.data, packet.length, false);
                    rakPeerInterface.Send(bitStream, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED,
                                          (char)0, packet.systemAddress, true);
                    break;
                }

                default:
                    Debug.Log("Recieved a message with unknown id:");
                    Debug.Log(packet.data[0]);
                    break;
                }
            }
        }
    }
Beispiel #2
0
        void _RunRead()
        {
            while (!_RequestReadThreadStop)
            {
                _packet = _peer.Receive();
                while (_packet != null)
                {
                    ProcessMessage(_packet);
                    _peer.DeallocatePacket(_packet);
                    _packet = _peer.Receive();
                }
                Thread.Sleep(10);
            }
            Log.Debug("Thread Stop!");

            if (_peer != null && ClientState == RakNetClientState.ConnectOK)
            {
                _peer.Shutdown(300);
            }
        }
        void handleNetworkMessages(RakNet.RakPeerInterface rakPeerInterface)
        {
            RakNet.Packet packet;
            packet = null;

            while (true)
            {
                for (packet = rakPeerInterface.Receive(); packet != null; rakPeerInterface.DeallocatePacket(packet), packet = rakPeerInterface.Receive())
                {
                    switch (packet.data[0])
                    {
                    case (int)RakNet.DefaultMessageIDTypes.ID_NEW_INCOMING_CONNECTION:

                        break;
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 处理接受消息
        /// </summary>
        void ReceiveMessage()
        {
            receiveCount = 0;
            // 单帧处理消息数量不大于指定的消息数量
            while (receiveCount < MaxReceiveCount || !ProcUserMessage)
            {
                if (mClient == null)
                {
                    Debug.LogErrorFormat("NetworkClient is null!");
                    break;
                }
                Packet packet = mClient.Receive();
                if (packet == null) // 没有消息内容了,不用解析,退出循环
                {
                    break;
                }
                receiveCount++;
                // DefaultMessageIDTypes messageIDType = (DefaultMessageIDTypes)packet.data[0];
                // LogMessage(messageIDType.ToString());
                switch (packet.data[0])
                {
                case (byte)DefaultMessageIDTypes.ID_CONNECTED_PING:             // 0
                case (byte)DefaultMessageIDTypes.ID_UNCONNECTED_PING:           // 1
                    LogMessage(string.Format("Ping from {0}", packet.systemAddress.ToString(true)));
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:        //16 连接成功
                {
                    IsConnecting      = false;
                    IsConnectedServer = true;
                    m_serverAddress   = packet.systemAddress;
                    m_guid            = packet.guid;
                    mClient.SetTimeoutTime(9000, m_serverAddress);
                    NotifyConnectCallBack(true);
                }
                break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:          // 17 连接失败(多种原因)
                    if (IsConnecting)
                    {
                        NotifyConnectCallBack(false);
                    }
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_ALREADY_CONNECTED:                  // 18 已经存在
                    //Debug.Log("收到18消息");
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:       // 20 服务器满员了
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    // Sorry, the server is full.  I don't do anything here but
                    // A real app should tell the user
                    break;

                case (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:         // 21 丢失通知
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST:                    // 22 连接关闭
                    // Couldn't deliver a reliable packet - i.e. the other system was abnormally
                    // terminated
                    //Debug.Log("收到22消息");
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_BANNED:                  // 23 服务器维护,强壮踢出所有人
                    if (IsConnecting)
                    {
                        NotifyConnectCallBack(false);
                    }
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_INVALID_PASSWORD:                       // 24 无效的密码
                    break;

                case (byte)DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:          // 25 无效协议
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_DISCONNECTION_NOTIFICATION:      // 31
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_CONNECTION_LOST:                 // 32
                    // Server telling the clients of another m_Client disconnecting forcefully.  You can manually broadcast this in a peer to peer enviroment if you want.
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_NEW_INCOMING_CONNECTION:         // 33
                    // Server telling the clients of another m_Client connecting.  You can manually broadcast this in a peer to peer enviroment if you want.
                    break;

                case (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM:                       // 134
                case (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM2:                      // 135
                    if (ProcUserMessage == true)
                    {
                        Dispatch(packet);
                    }
                    break;

                default:
                    // It's a m_Client, so just show the message
                    LogMessage("Message ID Type default: " + packet.data[0]);
                    break;
                }
                if (mClient != null)
                {
                    mClient.DeallocatePacket(packet);
                }
                else
                {
                    Debug.LogErrorFormat("DeallocatePacket NetworkClient:{0} is null!", ClientName);
                }
            }

            if (ProcUserMessage == false)
            {
                ProcUserMessage = true;
                // 抛出消息清除完毕的事件
                //Debug.Log("c#清空消息完毕");
                EventDispatcher.Instance.TriggerEvent("Application_ClearMessage_OK", null);
            }
        }
Beispiel #5
0
        /// <summary>
        /// 处理接受消息
        /// </summary>
        void ReceiveMessage()
        {
            receiveCount = 0;
            // 单帧处理消息数量不大于指定的消息数量
            while (receiveCount < MaxReceiveCount || !ProcUserMessage)
            {
                Packet packet = mClient.Receive();
                if (packet == null) // 没有消息内容了,不用解析,退出循环
                {
                    break;
                }
                receiveCount++;
                DefaultMessageIDTypes messageIDType = (DefaultMessageIDTypes)packet.data[0];
                LogMessage(messageIDType.ToString());
                switch (packet.data[0])
                {
                case (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                    //DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_ALREADY_CONNECTED:
                    //Debug.Log("收到18消息");
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_DISCONNECTION_NOTIFICATION:     // Server telling the clients of another m_Client disconnecting gracefully.  You can manually broadcast this in a peer to peer enviroment if you want.
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_CONNECTION_LOST:     // Server telling the clients of another m_Client disconnecting forcefully.  You can manually broadcast this in a peer to peer enviroment if you want.
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_REMOTE_NEW_INCOMING_CONNECTION:     // Server telling the clients of another m_Client connecting.  You can manually broadcast this in a peer to peer enviroment if you want.
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_BANNED:     // Banned from this server
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:
                    DisConnect();
                    //ShutDown();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:    // 服务器满员了
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    // Sorry, the server is full.  I don't do anything here but
                    // A real app should tell the user
                    break;

                case (byte)DefaultMessageIDTypes.ID_INVALID_PASSWORD:    // 无效的密码
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST:
                    // Couldn't deliver a reliable packet - i.e. the other system was abnormally
                    // terminated
                    DisConnect();
                    EventDispatcher.Instance.TriggerEvent("Application_ConnectionLost", packet.data[0]);
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:     // 连接成功
                {
                    IsConnectedServer = true;
                    //LogMessage(string.Format("ID_CONNECTION_REQUEST_ACCEPTED to {0} with GUID {1}", packet.systemAddress.ToString(true), packet.guid.ToString()));
                    //LogMessage(string.Format("My external address is {0}", mClient.GetExternalID(packet.systemAddress).ToString(true)));
                    m_serverAddress = packet.systemAddress;
                    m_guid          = packet.guid;
                    Debug.LogFormat("RakNetGUID:{0}", m_guid);
                    mClient.SetTimeoutTime(9000, m_serverAddress);
                    NotifyConnectCallBack(true);
                    break;
                }

                case (byte)DefaultMessageIDTypes.ID_CONNECTED_PING:
                case (byte)DefaultMessageIDTypes.ID_UNCONNECTED_PING:
                    LogMessage(string.Format("Ping from {0}", packet.systemAddress.ToString(true)));
                    break;

                case (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM:
                case (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM2:
                    if (ProcUserMessage == true)
                    {
                        Dispatch(packet);
                    }
                    break;

                default:
                    // It's a m_Client, so just show the message
                    LogMessage("Message ID Type default: " + packet.data[0]);
                    break;
                }
                mClient.DeallocatePacket(packet);
            }

            if (ProcUserMessage == false)
            {
                ProcUserMessage = true;
                // 抛出消息清除完毕的事件
                //Debug.Log("c#清空消息完毕");
                EventDispatcher.Instance.TriggerEvent("Application_ClearMessage_OK", null);
            }
        }