Пример #1
0
        void onSystemAddServer(FunapiRpcPeer peer, FunDedicatedServerRpcMessage msg)
        {
            FunDedicatedServerRpcMessage response = new FunDedicatedServerRpcMessage();

            response.type       = msg.type;
            response.xid        = msg.xid;
            response.is_request = false;
            peer.Send(response);

            FunDedicatedServerRpcSystemMessage sysmsg = FunapiDSRpcMessage.GetMessage <FunDedicatedServerRpcSystemMessage>(msg, MessageType.ds_rpc_sys);

            if (string.IsNullOrEmpty(sysmsg.data))
            {
                return;
            }

            Dictionary <string, object> data = FunapiDSRpcMessage.ParseJson(sysmsg.data) as Dictionary <string, object>;

            if (data.ContainsKey("id") && data.ContainsKey("ip") && data.ContainsKey("port"))
            {
                string peer_id = data["id"] as string;
                string ip      = data["ip"] as string;
                ushort port    = Convert.ToUInt16(data["port"]);

                if (!peer_list_.Exists(peer_id))
                {
                    FunDebug.Log("[Peer:{0}:{1}] Added. ({2})", ip, port, peer_id);
                    onConnect(ip, port);
                }
            }
        }
Пример #2
0
        void setMaster(FunapiRpcPeer peer)
        {
            if (peer == null)
            {
                return;
            }

            master_peer_ = peer;

            FunDebug.Log("[Peer:{0}:{1}] Set Master: {2}", peer.addr.host, peer.addr.port, peer.peer_id);

            FunDedicatedServerRpcSystemMessage sysmsg = new FunDedicatedServerRpcSystemMessage();

            if (!string.IsNullOrEmpty(option_.Tag))
            {
                sysmsg.data = string.Format("{{ \"tag\" : \"{0}\" }}", option_.Tag);
            }

            FunDedicatedServerRpcMessage response = FunapiDSRpcMessage.CreateMessage(sysmsg, MessageType.ds_rpc_sys);
            string xid = Guid.NewGuid().ToString("N");

            response.type       = kRpcMasterMessageType;
            response.xid        = Encoding.UTF8.GetBytes(xid);
            response.is_request = true;

            peer.Send(response);
        }
Пример #3
0
        void onSystemMaster(FunapiRpcPeer peer, FunDedicatedServerRpcMessage msg)
        {
            // Get peer id list from peer list
            List <string> old_peers = new List <string>();

            peer_list_.ForEach(delegate(FunapiRpcPeer p)
            {
                old_peers.Add(p.peer_id);
            });
            old_peers.Remove(peer.peer_id);

            FunDedicatedServerRpcSystemMessage sysmsg = FunapiDSRpcMessage.GetMessage <FunDedicatedServerRpcSystemMessage>(msg, MessageType.ds_rpc_sys);
            List <object> server_list = FunapiDSRpcMessage.ParseJson(sysmsg.data) as List <object>;

            foreach (Dictionary <string, object> data in server_list)
            {
                if (data.ContainsKey("id") && data.ContainsKey("ip") && data.ContainsKey("port"))
                {
                    string peer_id = data["id"] as string;
                    string ip      = data["ip"] as string;
                    ushort port    = Convert.ToUInt16(data["port"]);

                    if (!peer_list_.Exists(peer_id))
                    {
                        FunDebug.Log("[Peer:{0}:{1}] Added. ({2})", ip, port, peer_id);
                        onConnect(ip, port);
                    }
                    else
                    {
                        old_peers.Remove(peer_id);
                    }
                }
            }

            // Remove invalid peers from peer list
            foreach (string peer_id in old_peers)
            {
                FunapiRpcPeer p = peer_list_.Get(peer_id);
                if (p.state != FunapiRpcPeer.State.kDisconnected)
                {
                    p.Close(true);
                }
                peer_list_.Remove(p);
            }
        }
Пример #4
0
        void onSystemRemoveServer(FunapiRpcPeer peer, FunDedicatedServerRpcMessage msg)
        {
            FunDedicatedServerRpcMessage response = new FunDedicatedServerRpcMessage();

            response.type       = msg.type;
            response.xid        = msg.xid;
            response.is_request = false;
            peer.Send(response);

            FunDedicatedServerRpcSystemMessage sysmsg = FunapiDSRpcMessage.GetMessage <FunDedicatedServerRpcSystemMessage>(msg, MessageType.ds_rpc_sys);

            if (string.IsNullOrEmpty(sysmsg.data))
            {
                return;
            }

            Dictionary <string, object> data = FunapiDSRpcMessage.ParseJson(sysmsg.data) as Dictionary <string, object>;
            string peer_id = "";

            if (data.ContainsKey("id"))
            {
                peer_id = data["id"] as string;
            }

            if (string.IsNullOrEmpty(peer_id))
            {
                return;
            }

            if (peer_list_.Exists(peer_id))
            {
                FunapiRpcPeer del_peer = peer_list_.Get(peer_id);
                FunDebug.Log("[Peer:{0}:{1}] Removed. ({2})", del_peer.addr.host, del_peer.addr.port, peer_id);

                peer_list_.Remove(del_peer.uid);

                if (del_peer == master_peer_)
                {
                    onMasterDisconnected(del_peer);
                }
            }
        }
Пример #5
0
        void onSystemInfo(FunapiRpcPeer peer, FunDedicatedServerRpcMessage msg)
        {
            FunDedicatedServerRpcSystemMessage sysmsg = new FunDedicatedServerRpcSystemMessage();

            if (!string.IsNullOrEmpty(option_.Tag))
            {
                sysmsg.data = string.Format("{{ \"tag\" : \"{0}\" }}", option_.Tag);
            }
            FunDedicatedServerRpcMessage response = FunapiDSRpcMessage.CreateMessage(sysmsg, MessageType.ds_rpc_sys);

            response.type       = msg.type;
            response.xid        = msg.xid;
            response.is_request = false;
            peer.Send(response);

            sysmsg = FunapiDSRpcMessage.GetMessage <FunDedicatedServerRpcSystemMessage>(msg, MessageType.ds_rpc_sys);
            if (string.IsNullOrEmpty(sysmsg.data))
            {
                return;
            }

            Dictionary <string, object> data = FunapiDSRpcMessage.ParseJson(sysmsg.data) as Dictionary <string, object>;
            string peer_id = "";

            if (data.ContainsKey("id"))
            {
                peer_id = data["id"] as string;
            }

            if (peer_id.Length > 0)
            {
                peer.SetPeerId(peer_id);

                if (master_peer_ == null)
                {
                    setMaster(peer);
                }
            }
        }
Пример #6
0
 public static void AppendExtension_ds_rpc_sys(this FunDedicatedServerRpcMessage message, FunDedicatedServerRpcSystemMessage value)
 {
     ProtoBuf.Extensible.AppendValue(message, (int)ExtendedMessageFieldNumber.FunDedicatedServerRpcMessage_ds_rpc_sys, value);
 }
Пример #7
0
 public static bool TryGetExtension_ds_rpc_sys(this FunDedicatedServerRpcMessage message, out FunDedicatedServerRpcSystemMessage value)
 {
     return(ProtoBuf.Extensible.TryGetValue(message, (int)ExtendedMessageFieldNumber.FunDedicatedServerRpcMessage_ds_rpc_sys, out value));
 }