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 parseMessages() { lock (message_lock_) { while (true) { if (next_decoding_offset_ >= received_size_ - 4) { // Not enough bytes. Wait for more bytes to come. break; } int length = 0; if (BitConverter.IsLittleEndian) { byte[] bytes = new byte [4]; Buffer.BlockCopy(receive_buffer_, next_decoding_offset_, bytes, 0, 4); Array.Reverse(bytes); // gets big endian length = (int)BitConverter.ToUInt32(bytes, 0); } else { length = (int)BitConverter.ToUInt32(receive_buffer_, next_decoding_offset_); } if (length > 0) { int offset = next_decoding_offset_ + 4; if (received_size_ - offset < length) { // Need more bytes for a message body. Waiting. break; } object obj = FunapiDSRpcMessage.Deserialize(new ArraySegment <byte>(receive_buffer_, offset, length)); if (obj != null) { FunDedicatedServerRpcMessage msg = obj as FunDedicatedServerRpcMessage; messages_.Enqueue(msg); #if ENABLE_DEBUG StringBuilder log = new StringBuilder(); log.AppendFormat("[Peer:{0}] [S->C] type={1}, length={2} ", peer_id_, msg.type, length); FunapiDSRpcMessage.DebugString(msg, log); FunDebug.LogDebug(log.ToString()); #endif } next_decoding_offset_ = offset + length; } else { next_decoding_offset_ += 4; } } } }
void onSend() { try { lock (send_lock_) { if (send_queue_.Count <= 0 || sending_.Count > 0) { return; } Queue <FunDedicatedServerRpcMessage> list = send_queue_; send_queue_ = new Queue <FunDedicatedServerRpcMessage>(); foreach (FunDedicatedServerRpcMessage msg in list) { byte[] buf = FunapiDSRpcMessage.Serialize(msg); if (buf == null) { continue; } #if ENABLE_DEBUG StringBuilder log = new StringBuilder(); log.AppendFormat("[Peer:{0}] [C->S] type={1}, length={2} ", peer_id_, msg.type, buf.Length); FunapiDSRpcMessage.DebugString(msg, log); FunDebug.LogDebug(log.ToString()); #endif sending_.Add(new ArraySegment <byte>(buf)); } if (sending_.Count <= 0) { return; } lock (sock_lock_) { if (sock_ == null) { return; } sock_.BeginSend(sending_, SocketFlags.None, new AsyncCallback(this.sendCb), this); } } } catch (Exception e) { logWarning("Sending failed. {0}", e.ToString()); onDisconnect(PeerEventType.kDisconnected); } }
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); } }
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); } } }