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); }
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; } } }
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); } }
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); } } }
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); } } }
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); }
void onPeerEventCallback(FunapiRpcPeer peer, PeerEventType type) { lock (peer_event_lock_) { if (peer_event_handler_ != null) { peer_event_handler_(peer.peer_id, type); } } }
// 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(); }
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))); } }
public uint Add(FunapiRpcPeer peer) { if (peer == null) { throw new ArgumentNullException("peer"); } lock (lock_) { pending_.Add(peer); } return(peer.uid); }
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); }
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); } }
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)); } }
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); } } }
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); } } }
// 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(); }