示例#1
0
        void onPeerMessage(FunapiRpcPeer peer, FunDedicatedServerRpcMessage msg)
        {
            string type = msg.type;

            if (system_handlers_.ContainsKey(type))
            {
                system_handlers_[type](peer, msg);
                return;
            }

            if (!msg.is_request)
            {
                return;
            }

            lock (handler_lock_)
            {
                if (handlers_.ContainsKey(type))
                {
                    FunDedicatedServerRpcMessage response = handlers_[type](type, msg);
                    if (response != null)
                    {
                        response.type       = type;
                        response.xid        = msg.xid;
                        response.is_request = false;

                        peer.Send(response);
                    }
                    return;
                }
            }

            FunDebug.Log("[RPC] handler not found '{0}'", type);
        }
示例#2
0
        void onPeerEvent(FunapiRpcPeer peer, PeerEventType type)
        {
            onPeerEventCallback(peer, type);

            if (!active_)
            {
                return;
            }

            if (type == PeerEventType.kDisconnected ||
                type == PeerEventType.kConnectionFailed ||
                type == PeerEventType.kConnectionTimedOut)
            {
                if (!peer.abort && peer_list_.Exists(peer.uid))
                {
                    peer.Reconnect();
                }

                if (peer == master_peer_)
                {
                    onMasterDisconnected(peer);
                    return;
                }
            }
        }
示例#3
0
        void onMasterDisconnected(FunapiRpcPeer peer)
        {
            if (peer != master_peer_)
            {
                return;
            }

            FunapiRpcPeer new_master = peer_list_.GetAnyAvailable(peer);

            if (new_master != null)
            {
                setMaster(new_master);
            }
            else
            {
                master_peer_ = null;

                // If there's no valid connection, remove the last peer.
                if (!peer.abort)
                {
                    peer_list_.Remove(peer.uid);
                }

                onConnect(0);
            }
        }
示例#4
0
        void onPeerEventBeforeConnect(FunapiRpcPeer peer, PeerEventType type)
        {
            onPeerEventCallback(peer, type);

            if (type == PeerEventType.kConnected)
            {
                peer.SetEventHandler(onPeerEvent);
            }
            else
            {
                int index = 0;
                if ((cur_index_ + 1) < option_.Addrs.Count)
                {
                    index = cur_index_ + 1;
                }

                if (index == cur_index_)
                {
                    if (!peer.abort)
                    {
                        peer.Reconnect();
                    }
                }
                else
                {
                    peer_list_.Remove(peer);

                    event_.Add(delegate {
                        onConnect(index);
                    }, 0.5f);
                }
            }
        }
示例#5
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);
                }
            }
        }
示例#6
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);
        }
示例#7
0
 void onPeerEventCallback(FunapiRpcPeer peer, PeerEventType type)
 {
     lock (peer_event_lock_)
     {
         if (peer_event_handler_ != null)
         {
             peer_event_handler_(peer.peer_id, type);
         }
     }
 }
示例#8
0
        // Connection due to 'add_server' message
        void onConnect(string hostname_or_ip, ushort port)
        {
            FunapiRpcPeer peer = new FunapiRpcPeer(getNextUid(), option_.DisableNagle);

            peer.SetAddr(hostname_or_ip, port);
            peer.SetEventHandler(onPeerEvent);
            peer.SetMessageHandler(onPeerMessage);

            peer_list_.Add(peer);

            peer.Connect();
        }
示例#9
0
        public FunapiRpcPeer Get(string peer_id)
        {
            lock (lock_)
            {
                FunapiRpcPeer peer = list_.Find(predicate(peer_id));
                if (peer != null)
                {
                    return(peer);
                }

                return(pending_.Find(predicate(peer_id)));
            }
        }
示例#10
0
        public uint Add(FunapiRpcPeer peer)
        {
            if (peer == null)
            {
                throw new ArgumentNullException("peer");
            }

            lock (lock_)
            {
                pending_.Add(peer);
            }

            return(peer.uid);
        }
示例#11
0
        public FunapiRpcPeer GetAnyAvailable(FunapiRpcPeer exclude = null)
        {
            lock (lock_)
            {
                foreach (FunapiRpcPeer peer in list_)
                {
                    if (peer != exclude && !peer.abort && peer.state == FunapiRpcPeer.State.kEstablished)
                    {
                        return(peer);
                    }
                }
            }

            return(null);
        }
示例#12
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);
            }
        }
示例#13
0
        public bool Remove(FunapiRpcPeer peer)
        {
            if (peer == null)
            {
                throw new ArgumentNullException("peer");
            }

            lock (lock_)
            {
                peer.abort = true;

                if (list_.Contains(peer))
                {
                    return(true);
                }

                return(pending_.Remove(peer));
            }
        }
示例#14
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);
                }
            }
        }
示例#15
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);
                }
            }
        }
示例#16
0
        // Connection from the address pool
        void onConnect(int index)
        {
            if (index >= option_.Addrs.Count)
            {
                FunDebug.Log("[RPC] Invalid connect index. index:{0} list size:{1}",
                             index, option_.Addrs.Count);
                return;
            }

            cur_index_ = index;

            FunapiRpcPeer peer = new FunapiRpcPeer(getNextUid(), option_.DisableNagle);
            KeyValuePair <string, ushort> addr = option_.Addrs[index];

            peer.SetAddr(addr.Key, addr.Value);
            peer.SetEventHandler(onPeerEventBeforeConnect);
            peer.SetMessageHandler(onPeerMessage);

            peer_list_.Add(peer);

            peer.Connect();
        }