public static T GetMulticastMessage <T> (FunMulticastMessage msg, MulticastMessageType msg_type) { try { object _msg = null; Extensible.TryGetValue(serializer_, MessageTable.GetType(msg_type), msg, (int)msg_type, DataFormat.Default, true, out _msg); FunDebug.Assert(_msg != null, "TryGetValue() failed. Please check the message type."); return((T)_msg); } catch (Exception e) { Type type = MessageTable.GetType(msg_type); FunDebug.LogError("FunapiMessage.GetMulticastMessage - Failed to decode '{0}' ({1})\n{2}", type, msg_type, e.ToString()); if (ParsingErrorCallback != null) { ParsingErrorCallback(type); } } return(default(T)); }
void onReceived(string channel_id, string sender, object data) { if (encoding_ == FunEncoding.kJson) { string text = json_helper_.GetStringField(data, kMessage); lock (chat_channel_lock_) { if (chat_channels_.ContainsKey(channel_id)) { chat_channels_[channel_id](channel_id, sender, text); } } } else { FunDebug.Assert(data is FunMulticastMessage); FunMulticastMessage mcast_msg = data as FunMulticastMessage; object obj = FunapiMessage.GetMessage(mcast_msg, MulticastMessageType.chat); FunChatMessage chat_msg = obj as FunChatMessage; lock (chat_channel_lock_) { if (chat_channels_.ContainsKey(channel_id)) { chat_channels_[channel_id](channel_id, sender, chat_msg.text); } } } }
void onMulticastChannelReceived(string channel_id, string sender, object body) { if (multicast_.encoding == FunEncoding.kJson) { string channel = FunapiMessage.JsonHelper.GetStringField(body, "_channel"); FunDebug.Assert(channel != null && channel == channel_id); string message = FunapiMessage.JsonHelper.GetStringField(body, "_message"); FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}", channel_id, message); } else { FunDebug.Assert(body is FunMulticastMessage); FunMulticastMessage mcast_msg = body as FunMulticastMessage; FunDebug.Assert(channel_id == mcast_msg.channel); object obj = FunapiMessage.GetMessage(mcast_msg, MulticastMessageType.pbuf_hello); PbufHelloMessage hello_msg = obj as PbufHelloMessage; if (hello_msg == null) { return; } FunDebug.Log("Received a multicast message from the '{0}' channel.\nMessage: {1}", channel_id, hello_msg.message); } }
private void OnMulticastingReceived(string chat_channel, object data) { KeyValuePair <string, OnChannelMessage> p; lock (channel_lock_) { if (!channel_info_.TryGetValue(chat_channel, out p)) { DebugUtils.Log("You are not in the chat channel: {0}", chat_channel); return; } } if (encoding_ == FunEncoding.kJson) { DebugUtils.Assert(data is Dictionary <string, object>); Dictionary <string, object> mcast_msg = data as Dictionary <string, object>; p.Value(chat_channel, mcast_msg["sender"] as string, mcast_msg["text"] as string); } else { DebugUtils.Assert(data is FunMulticastMessage); FunMulticastMessage mcast_msg = data as FunMulticastMessage; FunChatMessage chat_msg = Extensible.GetValue <FunChatMessage> (mcast_msg, (int)MulticastMessageType.chat); p.Value(chat_channel, chat_msg.sender, chat_msg.text); } }
// For Multicast messages public static FunMulticastMessage CreateMulticastMessage(object msg, MulticastMessageType msg_type) { if (msg is Enum) { msg = (Int32)msg; } try { FunMulticastMessage _msg = new FunMulticastMessage(); Extensible.AppendValue(serializer_, _msg, (int)msg_type, DataFormat.Default, msg); return(_msg); } catch (Exception e) { Type type = MessageTable.GetType(msg_type); FunDebug.LogError("FunapiMessage.CreateMulticastMessage - Failed to create '{0}' ({1})\n{2}", type, msg_type, e.ToString()); if (ParsingErrorCallback != null) { ParsingErrorCallback(type); } } return(null); }
public bool SendText(string channel_id, string text) { if (!Connected) { return(false); } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kBounce] = true; mcast_msg[kMessage] = text; SendToChannel(mcast_msg); } else { FunChatMessage chat_msg = new FunChatMessage(); chat_msg.text = text; FunMulticastMessage mcast_msg = FunapiMessage.CreateFunMessage(chat_msg, MulticastMessageType.chat); mcast_msg.channel = channel_id; mcast_msg.bounce = true; SendToChannel(mcast_msg); } return(true); }
/// <summary> /// The sender must fill in the mcast_msg. /// The "channel_id" field is mandatory. /// And mcas_msg must have join and leave flags set. /// </summary> public bool SendToChannel(FunMulticastMessage mcast_msg) { DebugUtils.Assert(encoding_ == FunEncoding.kProtobuf); DebugUtils.Assert(mcast_msg != null); DebugUtils.Assert(!mcast_msg.join); DebugUtils.Assert(!mcast_msg.leave); string channel_id = mcast_msg.channel; DebugUtils.Assert(channel_id != ""); lock (channel_lock_) { if (!Connected) { DebugUtils.Log("Not connected. If you are trying to leave a channel in which you were, " + "connect first while preserving the session id you used for join."); return(false); } if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return(false); } } FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage(kMulticastMsgType, fun_msg); return(true); }
public bool JoinChannel(string channel_id, string token, ChannelMessage handler) { if (!Connected) { FunDebug.LogWarning("Multicast.JoinChannel - Multicast is not connected.\n" + "Please connect first before join a multicast channel."); return(false); } lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { FunDebug.LogWarning("Multicast.JoinChannel - Already joined the '{0} channel.", channel_id); return(false); } channels_.Add(channel_id, handler); } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kSender] = sender_; mcast_msg[kJoin] = true; if (token != null && token.Length > 0) { mcast_msg[kToken] = token; } session_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = channel_id; mcast_msg.sender = sender_; mcast_msg.join = true; if (token != null && token.Length > 0) { mcast_msg.token = token; } FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); } FunDebug.Log("Multicast - Request to join '{0}' channel", channel_id); return(true); }
/// The 'channel_id' field is mandatory. /// The 'sender' must fill in the message. /// The message shouldn't include join and leave flags. /// public bool SendToChannel(FunMulticastMessage mcast) { if (mcast == null) { return(false); } FunDebug.Assert(!mcast.join); FunDebug.Assert(!mcast.leave); return(sendToChannel(mcast.channel, mcast)); }
// For Multicast messages public static FunMulticastMessage CreateFunMessage(object msg, MulticastMessageType msg_type) { if (msg is Enum) { msg = (Int32)msg; } FunMulticastMessage _msg = new FunMulticastMessage(); Extensible.AppendValue(serializer_, _msg, (int)msg_type, DataFormat.Default, msg); return(_msg); }
public static object GetMessage(FunMulticastMessage msg, MulticastMessageType msg_type) { object _msg = null; bool success = Extensible.TryGetValue(serializer_, MessageTable.GetType(msg_type), msg, (int)msg_type, DataFormat.Default, true, out _msg); if (!success) { FunDebug.Log("Failed to decode {0} {1}", MessageTable.GetType(msg_type), (int)msg_type); return(null); } return(_msg); }
void onReceivedMessage(FunMulticastMessage mcast) { string channel_id = ""; string sender = ""; if (mcast.channelSpecified) { channel_id = mcast.channel; } if (mcast.channels.Count > 0 || string.IsNullOrEmpty(channel_id)) { if (ChannelListCallback != null) { ChannelListCallback(mcast.channels); } return; } // If the channel id is not in the channel list, ignores it. if (!InChannel(channel_id)) { return; } if (mcast.error_codeSpecified) { int error_code = (int)mcast.error_code; onError(channel_id, (FunMulticastMessage.ErrorCode)error_code); return; } if (mcast.senderSpecified) { sender = mcast.sender; } if (mcast.joinSpecified && mcast.join) { onUserJoined(channel_id, sender); return; } else if (mcast.leaveSpecified && mcast.leave) { onUserLeft(channel_id, sender); return; } onMessageCallback(channel_id, sender, mcast); }
private void OnReceived(string msg_type, object body) { DebugUtils.Assert(msg_type == kMulticastMsgType); string channel_id = ""; if (encoding_ == FunEncoding.kJson) { DebugUtils.Assert(body is Dictionary <string, object>); Dictionary <string, object> msg = body as Dictionary <string, object>; channel_id = msg[kChannelId] as string; lock (channel_lock_) { if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return; } ChannelReceiveHandler h = channels_[channel_id]; h(channel_id, body); } } else { DebugUtils.Assert(body is FunMessage); FunMessage msg = body as FunMessage; object obj = network_.GetMessage(msg, MessageType.multicast); DebugUtils.Assert(obj != null); FunMulticastMessage mcast_msg = obj as FunMulticastMessage; channel_id = mcast_msg.channel; lock (channel_lock_) { if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return; } ChannelReceiveHandler h = channels_[channel_id]; h(channel_id, mcast_msg); } } }
void onReceivedMessage(string msg_type, object body) { if (encoding_ == FunEncoding.kJson) { onReceivedMessage(body); } else { FunMessage msg = body as FunMessage; FunMulticastMessage mcast = FunapiMessage.GetMessage <FunMulticastMessage>(msg, MessageType.multicast); if (mcast != null) { onReceivedMessage(mcast); } } }
public bool SendText(string chat_channel, string text) { if (!Connected) { return(false); } KeyValuePair <string, OnChannelMessage> p; lock (channel_lock_) { if (!channel_info_.TryGetValue(chat_channel, out p)) { DebugUtils.Log("You are not in the chat channel: {0}", chat_channel); return(false); } } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg["_channel"] = chat_channel; mcast_msg["_bounce"] = true; mcast_msg["sender"] = p.Key; mcast_msg["text"] = text; multicasting_.SendToChannel(mcast_msg); } else { FunChatMessage chat_msg = new FunChatMessage(); chat_msg.sender = p.Key; chat_msg.text = text; FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = chat_channel; mcast_msg.bounce = true; Extensible.AppendValue(mcast_msg, (int)MulticastMessageType.chat, chat_msg); multicasting_.SendToChannel(mcast_msg); } return(true); }
public bool JoinChannel(string channel_id, ChannelMessage handler) { if (!Connected) { FunDebug.Log("Not connected. First connect before join a multicast channel."); return(false); } lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { FunDebug.Log("Already joined the channel: {0}", channel_id); return(false); } channels_.Add(channel_id, handler); } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kSender] = sender_; mcast_msg[kJoin] = true; session_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = channel_id; mcast_msg.sender = sender_; mcast_msg.join = true; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); } FunDebug.Log("Request to join '{0}' channel", channel_id); return(true); }
void requestToJoin(string channel_id, string token) { if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast = new Dictionary <string, object>(); mcast[kChannelId] = channel_id; mcast[kSender] = sender_; mcast[kJoin] = true; if (!string.IsNullOrEmpty(token)) { mcast[kToken] = token; } session_.SendMessage(kMulticastMsgType, mcast, protocol_); } else { FunMulticastMessage mcast = new FunMulticastMessage(); mcast.channel = channel_id; mcast.sender = sender_; mcast.join = true; if (!string.IsNullOrEmpty(token)) { mcast.token = token; } FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg, protocol_); } if (!string.IsNullOrEmpty(token)) { lock (token_lock_) { tokens_[channel_id] = token; } } FunDebug.Log("[Multicast] requested to join '{0}' channel.", channel_id); }
bool sendToChannel(string channel_id, object message) { if (message == null) { return(false); } if (string.IsNullOrEmpty(channel_id)) { FunDebug.LogWarning("[Multicast] can't send a message. invalid channel id."); return(false); } if (!Connected) { FunDebug.LogWarning("[Multicast] can't send a message. session is not connected."); return(false); } if (!InChannel(channel_id)) { FunDebug.LogWarning("[Multicast] can't send a message. you aren't in '{0}' channel.", channel_id); return(false); } if (encoding_ == FunEncoding.kJson) { json_helper_.SetStringField(message, kSender, sender_); session_.SendMessage(kMulticastMsgType, message, protocol_); } else { FunMulticastMessage mcast = message as FunMulticastMessage; mcast.sender = sender_; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg, protocol_); } return(true); }
protected override void onMessageCallback(string channel_id, string user_id, object message) { if (encoding_ == FunEncoding.kJson) { string text = json_helper_.GetStringField(message, kMessage); base.onMessageCallback(channel_id, user_id, text); } else { FunMulticastMessage mcast = message as FunMulticastMessage; FunChatMessage chat = FunapiMessage.GetMulticastMessage <FunChatMessage>(mcast, MulticastMessageType.chat); if (chat == null) { return; } base.onMessageCallback(channel_id, user_id, chat.text); } }
/// The 'channel_id' field is mandatory. /// The 'sender' must fill in the message. /// The message shouldn't include join and leave flags. public bool SendToChannel(FunMulticastMessage mcast_msg) { if (mcast_msg == null) { return(false); } FunDebug.Assert(!mcast_msg.join); FunDebug.Assert(!mcast_msg.leave); string channel_id = mcast_msg.channel; if (channel_id == "") { FunDebug.LogWarning("Multicast.SendToChannel - You should set a vaild channel id."); return(false); } lock (channel_lock_) { if (!Connected) { FunDebug.LogWarning("Multicast.SendToChannel - Multicast is not connected.\n" + "If you are trying to send a message in which you were, " + "connect first while preserving the session id you used for join."); return(false); } if (!channels_.ContainsKey(channel_id)) { FunDebug.LogWarning("Multicast.SendToChannel - You are not in the '{0} channel.", channel_id); return(false); } } mcast_msg.sender = sender_; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); return(true); }
public bool JoinChannel (string channel_id, ChannelMessage handler) { if (!Connected) { DebugUtils.Log("Not connected. First connect before join a multicast channel."); return false; } lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { DebugUtils.Log("Already joined the channel: {0}", channel_id); return false; } channels_.Add (channel_id, handler); } if (encoding_ == FunEncoding.kJson) { Dictionary<string, object> mcast_msg = new Dictionary<string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kSender] = sender_; mcast_msg[kJoin] = true; network_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = channel_id; mcast_msg.sender = sender_; mcast_msg.join = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage (kMulticastMsgType, fun_msg); } return true; }
void sendLeaveMessage(string channel_id) { if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kSender] = sender_; mcast_msg[kLeave] = true; session_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = channel_id; mcast_msg.sender = sender_; mcast_msg.leave = true; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); } }
public bool LeaveChannel(string channel_id) { if (!Connected) { DebugUtils.Log("Not connected. If you are trying to leave a channel in which you were, " + "connect first while preserving the session id you used for join."); return(false); } lock (channel_lock_) { if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return(false); } channels_.Remove(channel_id); } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kLeave] = true; network_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = channel_id; mcast_msg.leave = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage(kMulticastMsgType, fun_msg); } return(true); }
public bool JoinChannel(string channel_id, ChannelReceiveHandler handler) { if (!Connected) { DebugUtils.Log("Not connected. First connect before join a multicast channel."); return(false); } lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { DebugUtils.Log("Already joined the channel: {0}", channel_id); return(false); } channels_.Add(channel_id, handler); } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kJoin] = true; network_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = channel_id; mcast_msg.join = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage(kMulticastMsgType, fun_msg); } return(true); }
public void sendMulticastMessage() { if (multicast_.encoding == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg["_channel"] = kChannelName; mcast_msg["_bounce"] = true; mcast_msg["_message"] = "multicast test message"; multicast_.SendToChannel(mcast_msg); } else { PbufHelloMessage hello_msg = new PbufHelloMessage(); hello_msg.message = "multicast test message"; FunMulticastMessage mcast_msg = FunapiMessage.CreateFunMessage(hello_msg, MulticastMessageType.pbuf_hello); mcast_msg.channel = kChannelName; mcast_msg.bounce = true; multicast_.SendToChannel(mcast_msg); } }
public void RequestChannelList() { if (!Connected) { FunDebug.LogWarning("[Multicast] request a channel list but the session is not connected."); return; } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast = new Dictionary <string, object>(); mcast[kSender] = sender_; session_.SendMessage(kMulticastMsgType, mcast, protocol_); } else { FunMulticastMessage mcast = new FunMulticastMessage(); mcast.sender = sender_; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg, protocol_); } }
public void RequestChannelList() { if (!Connected) { FunDebug.Log("Not connected. First connect before join a multicast channel."); return; } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kSender] = sender_; session_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.sender = sender_; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); } }
public void RequestChannelList() { if (!Connected) { FunDebug.LogWarning("Multicast.RequestChannelList - Multicast is not connected.\n" + "Please connect first before request a channel list."); return; } if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast_msg = new Dictionary <string, object>(); mcast_msg[kSender] = sender_; session_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.sender = sender_; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast_msg, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg); } }
void requestToLeave(string channel_id) { if (encoding_ == FunEncoding.kJson) { Dictionary <string, object> mcast = new Dictionary <string, object>(); mcast[kChannelId] = channel_id; mcast[kSender] = sender_; mcast[kLeave] = true; session_.SendMessage(kMulticastMsgType, mcast, protocol_); } else { FunMulticastMessage mcast = new FunMulticastMessage(); mcast.channel = channel_id; mcast.sender = sender_; mcast.leave = true; FunMessage fun_msg = FunapiMessage.CreateFunMessage(mcast, MessageType.multicast); session_.SendMessage(kMulticastMsgType, fun_msg, protocol_); } FunDebug.Log("[Multicast] requested to leave '{0}' channel.", channel_id); }
public bool SendText(string chat_channel, string text) { if (multicasting_ == null || !multicasting_.Connected) { return false; } KeyValuePair<string, OnChannelMessage> p; lock (lock_) { if (!channel_info_.TryGetValue(chat_channel, out p)) { Debug.Log("You are not in the chat channel: " + chat_channel); return false; } } FunChatMessage chat_msg = new FunChatMessage (); chat_msg.sender = p.Key; chat_msg.text = text; FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = chat_channel; mcast_msg.bounce = true; Extensible.AppendValue (mcast_msg, (int)MulticastMessageType.chat, chat_msg); multicasting_.SendToChannel (mcast_msg); return true; }
/// <summary> /// The sender must fill in the mcast_msg. /// The "channel_id" field is mandatory. /// And mcas_msg must have join and leave flags set. /// </summary> public bool SendToChannel(FunMulticastMessage mcast_msg) { DebugUtils.Assert(encoding_ == FunEncoding.kProtobuf); DebugUtils.Assert(mcast_msg != null); DebugUtils.Assert(!mcast_msg.join); DebugUtils.Assert(!mcast_msg.leave); string channel_id = mcast_msg.channel; DebugUtils.Assert(channel_id != ""); lock (channel_lock_) { if (!Connected) { DebugUtils.Log("Not connected. If you are trying to leave a channel in which you were, " + "connect first while preserving the session id you used for join."); return false; } if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return false; } } FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage (kMulticastMsgType, fun_msg); return true; }
public bool LeaveChannel(string channel_id) { if (!Connected) { DebugUtils.Log("Not connected. If you are trying to leave a channel in which you were, " + "connect first while preserving the session id you used for join."); return false; } lock (channel_lock_) { if (!channels_.ContainsKey(channel_id)) { DebugUtils.Log("You are not in the channel: {0}", channel_id); return false; } channels_.Remove(channel_id); } if (encoding_ == FunEncoding.kJson) { Dictionary<string, object> mcast_msg = new Dictionary<string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kLeave] = true; network_.SendMessage(kMulticastMsgType, mcast_msg); } else { FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = channel_id; mcast_msg.leave = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage (kMulticastMsgType, fun_msg); } return true; }
public void OnGUI() { //---------------------------------------------------------------------------- // FunapiNetwork test //---------------------------------------------------------------------------- with_protobuf_ = GUI.Toggle(new Rect(30, 0, 300, 20), with_protobuf_, " google protocol buffer"); with_session_reliability_ = GUI.Toggle(new Rect(30, 20, 300, 20), with_session_reliability_, " session reliability"); GUI.Label(new Rect(30, 40, 300, 20), "server : " + kServerIp); GUI.enabled = (network_ == null || !network_.Started); if (GUI.Button(new Rect(30, 60, 240, 40), "Connect (TCP)")) { Connect(TransportProtocol.kTcp); } if (GUI.Button(new Rect(30, 110, 240, 40), "Connect (UDP)")) { Connect(TransportProtocol.kUdp); } if (GUI.Button(new Rect(30, 160, 240, 40), "Connect (HTTP)")) { Connect(TransportProtocol.kHttp); } GUI.enabled = (network_ != null && network_.Connected); if (GUI.Button(new Rect(30, 210, 240, 40), "Disconnect")) { Disconnect(); } if (GUI.Button(new Rect(30, 260, 240, 40), "Send 'Hello World'")) { SendEchoMessage(); } //---------------------------------------------------------------------------- // Announcements test //---------------------------------------------------------------------------- GUI.enabled = true; if (GUI.Button(new Rect(30, 340, 240, 40), "Update Announcements")) { if (announcement_ == null) { announcement_ = new FunapiAnnouncement(); announcement_.ResultCallback += new FunapiAnnouncement.EventHandler(OnAnnouncementResult); string url = ""; if (FunapiConfig.IsValid) url = FunapiConfig.AnnouncementUrl; if (url.Length <= 0) url = string.Format("http://{0}:{1}", kAnnouncementIp, kAnnouncementPort); if (url.Length <= 0) return; announcement_.Init(url); } announcement_.UpdateList(); } //---------------------------------------------------------------------------- // Resource download test //---------------------------------------------------------------------------- GUI.enabled = downloader_ == null; GUI.Label(new Rect(30, 390, 300, 20), String.Format("server : {0}:{1}", kDownloadServerIp, kDownloadServerPort)); if (GUI.Button(new Rect(30, 410, 240, 40), "File Download (HTTP)")) { if (FunapiConfig.IsValid) { downloader_ = FunapiConfig.CreateDownloader(FunapiUtils.GetLocalDataPath); if (downloader_ != null) { downloader_.UpdateCallback += new FunapiHttpDownloader.UpdateEventHandler(OnDownloadUpdate); downloader_.FinishedCallback += new FunapiHttpDownloader.FinishEventHandler(OnDownloadFinished); } } if (downloader_ == null) { downloader_ = new FunapiHttpDownloader(FunapiUtils.GetLocalDataPath, false, OnDownloadUpdate, OnDownloadFinished); downloader_.StartDownload(string.Format("http://{0}:{1}", kDownloadServerIp, kDownloadServerPort)); } } //---------------------------------------------------------------------------- // FunapiMulticasting test //---------------------------------------------------------------------------- GUI.enabled = (multicast_ == null || !multicast_.Connected); GUI.Label(new Rect(280, 40, 300, 20), "server : " + kMulticastServerIp); string multicast_title = "Multicast (Protobuf) connect"; if (GUI.Button(new Rect(280, 60, 240, 40), multicast_title)) { if (FunapiConfig.IsValid) { multicast_ = FunapiConfig.CreateMulticasting(FunEncoding.kProtobuf, with_session_reliability_); } if (multicast_ == null) { if (multicast_ == null) multicast_ = new FunapiMulticastClient(FunEncoding.kProtobuf); multicast_.Connect(kMulticastServerIp, kMulticastPbufPort, with_session_reliability_); } Debug.Log("Connecting to the multicast server.."); } GUI.enabled = (multicast_ != null && multicast_.Connected && !multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) join"; if (GUI.Button(new Rect(280, 110, 240, 40), multicast_title)) { multicast_.JoinChannel(kMulticastTestChannel, OnMulticastChannelSignalled); Debug.Log(String.Format("Joining the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) send"; if (GUI.Button(new Rect(280, 160, 240, 40), multicast_title)) { PbufHelloMessage hello_msg = new PbufHelloMessage(); hello_msg.message = "multicast test message"; FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = kMulticastTestChannel; mcast_msg.bounce = true; Extensible.AppendValue(mcast_msg, (int)MulticastMessageType.pbuf_hello, hello_msg); multicast_.SendToChannel(mcast_msg); Debug.Log(String.Format("Sending a message to the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Multicast (Protobuf) leave"; if (GUI.Button(new Rect(280, 210, 240, 40), multicast_title)) { multicast_.LeaveChannel(kMulticastTestChannel); Debug.Log(String.Format("Leaving the multicast channel '{0}'", kMulticastTestChannel)); } GUI.enabled = (chat_ == null || !chat_.Connected); string chat_title = "Chat (Protobuf) connect"; if (GUI.Button(new Rect(280, 260, 240, 40), chat_title)) { if (chat_ == null) chat_ = new FunapiChatClient(); chat_.Connect(kMulticastServerIp, kMulticastPbufPort, FunEncoding.kProtobuf, with_session_reliability_); Debug.Log("Connecting to the chat server.."); } GUI.enabled = (chat_ != null && chat_.Connected && !chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) join"; if (GUI.Button(new Rect(280, 310, 240, 40), chat_title)) { chat_.JoinChannel(kChatTestChannel, kChatUserName, OnChatChannelReceived); Debug.Log(String.Format("Joining the chat channel '{0}'", kChatTestChannel)); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) send"; if (GUI.Button(new Rect(280, 360, 240, 40), chat_title)) { chat_.SendText(kChatTestChannel, "hello world"); Debug.Log(String.Format("Sending a message to the chat channel '{0}'", kChatTestChannel)); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Chat (Protobuf) leave"; if (GUI.Button(new Rect(280, 410, 240, 40), chat_title)) { chat_.LeaveChannel(kChatTestChannel); Debug.Log(String.Format("Leaving the chat channel '{0}'", kChatTestChannel)); } }
public void OnGUI() { //---------------------------------------------------------------------------- // FunapiNetwork test //---------------------------------------------------------------------------- with_session_reliability_ = GUI.Toggle(new Rect(30, 5, 130, 20), with_session_reliability_, " session reliability"); with_protobuf_ = GUI.Toggle(new Rect(180, 5, 150, 20), with_protobuf_, " google protocol buffer"); GUI.Label(new Rect(30, 40, 300, 20), "[FunapiNetwork] - " + kServerIp); GUI.enabled = (network_ == null || !network_.Started); if (GUI.Button(new Rect(30, 60, 240, 40), "Connect (TCP)")) { Connect(TransportProtocol.kTcp); } if (GUI.Button(new Rect(30, 105, 240, 40), "Connect (UDP)")) { Connect(TransportProtocol.kUdp); } if (GUI.Button(new Rect(30, 150, 240, 40), "Connect (HTTP)")) { Connect(TransportProtocol.kHttp); } GUI.enabled = (network_ != null && network_.Connected); if (GUI.Button(new Rect(30, 195, 240, 40), "Disconnect")) { Disconnect(); } if (GUI.Button(new Rect(30, 240, 240, 40), "Send a message")) { SendEchoMessage(); } //---------------------------------------------------------------------------- // Announcements test //---------------------------------------------------------------------------- GUI.enabled = true; GUI.Label(new Rect(30, 300, 300, 20), string.Format("[Announcer] - {0}:{1}", kAnnouncementIp, kAnnouncementPort)); if (GUI.Button(new Rect(30, 320, 240, 40), "Update announcements")) { if (announcement_ == null) { announcement_ = new FunapiAnnouncement(); announcement_.ResultCallback += new FunapiAnnouncement.EventHandler(OnAnnouncementResult); string url = ""; if (FunapiConfig.IsValid) url = FunapiConfig.AnnouncementUrl; if (url.Length <= 0) url = string.Format("http://{0}:{1}", kAnnouncementIp, kAnnouncementPort); if (url.Length <= 0) return; announcement_.Init(url); } announcement_.UpdateList(5); } //---------------------------------------------------------------------------- // Resource download test //---------------------------------------------------------------------------- GUI.enabled = downloader_ == null; GUI.Label(new Rect(30, 380, 300, 20), string.Format("[Downloader] - {0}:{1}", kDownloadServerIp, kDownloadServerPort)); if (GUI.Button(new Rect(30, 400, 240, 40), "Resource downloader (HTTP)")) { string download_url = ""; if (FunapiConfig.IsValid) { FunapiConfig.GetDownloaderUrl(out download_url); } if (download_url == "") { download_url = string.Format("http://{0}:{1}", kDownloadServerIp, kDownloadServerPort); } downloader_ = new FunapiHttpDownloader(); downloader_.VerifyCallback += new FunapiHttpDownloader.VerifyEventHandler(OnDownloadVerify); downloader_.ReadyCallback += new FunapiHttpDownloader.ReadyEventHandler(OnDownloadReady); downloader_.UpdateCallback += new FunapiHttpDownloader.UpdateEventHandler(OnDownloadUpdate); downloader_.FinishedCallback += new FunapiHttpDownloader.FinishEventHandler(OnDownloadFinished); downloader_.GetDownloadList(download_url, FunapiUtils.GetLocalDataPath); } //---------------------------------------------------------------------------- // FunapiMulticasting test //---------------------------------------------------------------------------- GUI.enabled = (multicast_ == null); GUI.Label(new Rect(280, 40, 300, 20), "[Muticasting]"); string multicast_title = "Create 'multicast'"; if (GUI.Button(new Rect(280, 60, 240, 40), multicast_title)) { FunapiTransport transport = null; if (network_ == null || (transport = network_.GetTransport(TransportProtocol.kTcp)) == null) { DebugUtils.LogWarning("You should connect to tcp transport first."); } else { multicast_ = new FunapiMulticastClient(network_, transport.Encoding); multicast_.sender = "player" + UnityEngine.Random.Range(1, 100); multicast_encoding_ = transport.Encoding; multicast_.JoinedCallback += delegate(string channel_id, string sender) { DebugUtils.DebugLog("JoinedCallback called. player:{0}", sender); }; multicast_.LeftCallback += delegate(string channel_id, string sender) { DebugUtils.DebugLog("LeftCallback called. player:{0}", sender); }; multicast_.ErrorCallback += new FunapiMulticastClient.ErrorNotify(OnMulticastError); } } GUI.enabled = (multicast_ != null && multicast_.Connected && !multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Join a channel"; if (GUI.Button(new Rect(280, 105, 240, 40), multicast_title)) { multicast_.JoinChannel(kMulticastTestChannel, OnMulticastChannelSignalled); } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Send a message"; if (GUI.Button(new Rect(280, 150, 240, 40), multicast_title)) { if (multicast_encoding_ == FunEncoding.kJson) { Dictionary<string, object> mcast_msg = new Dictionary<string, object>(); mcast_msg["_channel"] = kMulticastTestChannel; mcast_msg["_bounce"] = true; mcast_msg["message"] = "multicast test message"; multicast_.SendToChannel(mcast_msg); } else { PbufHelloMessage hello_msg = new PbufHelloMessage(); hello_msg.message = "multicast test message"; FunMulticastMessage mcast_msg = new FunMulticastMessage(); mcast_msg.channel = kMulticastTestChannel; mcast_msg.bounce = true; Extensible.AppendValue(mcast_msg, (int)MulticastMessageType.pbuf_hello, hello_msg); multicast_.SendToChannel(mcast_msg); } } GUI.enabled = (multicast_ != null && multicast_.Connected && multicast_.InChannel(kMulticastTestChannel)); multicast_title = "Leave a channel"; if (GUI.Button(new Rect(280, 195, 240, 40), multicast_title)) { multicast_.LeaveChannel(kMulticastTestChannel); } GUI.Label(new Rect(280, 250, 300, 20), "[Multicast Chat]"); GUI.enabled = (chat_ == null); string chat_title = "Create 'chat'"; if (GUI.Button(new Rect(280, 270, 240, 40), chat_title)) { FunapiTransport transport = null; if (network_ == null || (transport = network_.GetTransport(TransportProtocol.kTcp)) == null) { DebugUtils.LogWarning("You should connect to tcp transport first."); } else { chat_ = new FunapiChatClient(network_, transport.Encoding); chat_.sender = "player" + UnityEngine.Random.Range(1, 100); chat_.JoinedCallback += delegate(string channel_id, string sender) { DebugUtils.DebugLog("JoinedCallback called. player:{0}", sender); }; chat_.LeftCallback += delegate(string channel_id, string sender) { DebugUtils.DebugLog("LeftCallback called. player:{0}", sender); }; } } GUI.enabled = (chat_ != null && chat_.Connected && !chat_.InChannel(kChatTestChannel)); chat_title = "Join a channel"; if (GUI.Button(new Rect(280, 315, 240, 40), chat_title)) { chat_.JoinChannel(kChatTestChannel, OnChatChannelReceived); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Send a message"; if (GUI.Button(new Rect(280, 360, 240, 40), chat_title)) { chat_.SendText(kChatTestChannel, "hello everyone."); } GUI.enabled = (chat_ != null && chat_.Connected && chat_.InChannel(kChatTestChannel)); chat_title = "Leave a channel"; if (GUI.Button(new Rect(280, 405, 240, 40), chat_title)) { chat_.LeaveChannel(kChatTestChannel); } }
private void OnMulticastError (FunMulticastMessage.ErrorCode code) { // do something.. }
public bool JoinChannel(string channel_id, ChannelReceiveHandler handler) { lock (lock_) { if (network_ == null || !network_.Started) { Debug.Log ("Not connected. First connect before join a multicast channel."); return false; } if (channels_.ContainsKey(channel_id)) { Debug.Log ("Already joined the channel: " + channel_id); return false; } channels_.Add (channel_id, handler); } FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = channel_id; mcast_msg.join = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage (kMulticastMsgType, fun_msg); return true; }
public bool LeaveChannel(string channel_id) { lock (lock_) { if (network_ == null || !network_.Started) { Debug.Log ("Not connected. If you are trying to leave a channel in which you were, connect first while preserving the session id you used for join."); return false; } if (!channels_.ContainsKey(channel_id)) { Debug.Log ("You are not in the channel: " + channel_id); return false; } channels_.Remove(channel_id); } FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = channel_id; mcast_msg.leave = true; FunMessage fun_msg = network_.CreateFunMessage(mcast_msg, MessageType.multicast); network_.SendMessage (kMulticastMsgType, fun_msg); return true; }
public bool SendText (string channel_id, string text) { if (!Connected) return false; if (encoding_ == FunEncoding.kJson) { Dictionary<string, object> mcast_msg = new Dictionary<string, object>(); mcast_msg[kChannelId] = channel_id; mcast_msg[kBounce] = true; mcast_msg[kMessage] = text; SendToChannel(mcast_msg); } else { FunChatMessage chat_msg = new FunChatMessage (); chat_msg.text = text; FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = channel_id; mcast_msg.bounce = true; Extensible.AppendValue (mcast_msg, (int)MulticastMessageType.chat, chat_msg); SendToChannel(mcast_msg); } return true; }
void onReceived(string msg_type, object body) { string channel_id = ""; string sender = ""; bool join = false; bool leave = false; int error_code = 0; if (encoding_ == FunEncoding.kJson) { if (json_helper_.HasField(body, kChannelId)) { channel_id = json_helper_.GetStringField(body, kChannelId); } if (json_helper_.HasField(body, kSender)) { sender = json_helper_.GetStringField(body, kSender); } if (json_helper_.HasField(body, kErrorCode)) { error_code = (int)json_helper_.GetIntegerField(body, kErrorCode); } if (json_helper_.HasField(body, kChannelList)) { if (ChannelListCallback != null) { object list = json_helper_.GetObject(body, kChannelList); ChannelListCallback(list); } return; } else if (json_helper_.HasField(body, kJoin)) { join = json_helper_.GetBooleanField(body, kJoin); } else if (json_helper_.HasField(body, kLeave)) { leave = json_helper_.GetBooleanField(body, kLeave); } } else { FunMessage msg = body as FunMessage; FunMulticastMessage mcast_msg = FunapiMessage.GetMessage <FunMulticastMessage>(msg, MessageType.multicast); if (mcast_msg == null) { return; } if (mcast_msg.channelSpecified) { channel_id = mcast_msg.channel; } if (mcast_msg.senderSpecified) { sender = mcast_msg.sender; } if (mcast_msg.error_codeSpecified) { error_code = (int)mcast_msg.error_code; } if (mcast_msg.channels.Count > 0 || (channel_id == "" && sender == "")) { if (ChannelListCallback != null) { ChannelListCallback(mcast_msg.channels); } return; } else if (mcast_msg.joinSpecified) { join = mcast_msg.join; } else if (mcast_msg.leaveSpecified) { leave = mcast_msg.leave; } body = mcast_msg; } if (error_code != 0) { FunMulticastMessage.ErrorCode code = (FunMulticastMessage.ErrorCode)error_code; FunDebug.LogWarning("Multicast.onReceived - channel: {0} error: {1}", channel_id, code); if (code != FunMulticastMessage.ErrorCode.EC_ALREADY_JOINED) { lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { channels_.Remove(channel_id); } } } if (ErrorCallback != null) { ErrorCallback(channel_id, code); } return; } lock (channel_lock_) { if (!channels_.ContainsKey(channel_id)) { FunDebug.LogWarning("Multicast.onReceived - You are not in the '{0} channel.", channel_id); return; } } if (join) { onUserJoined(channel_id, sender); } else if (leave) { onUserLeft(channel_id, sender); } else { lock (channel_lock_) { if (channels_.ContainsKey(channel_id)) { channels_[channel_id](channel_id, sender, body); } } } }
public bool SendText(string chat_channel, string text) { if (!Connected) { return false; } KeyValuePair<string, OnChannelMessage> p; lock (channel_lock_) { if (!channel_info_.TryGetValue(chat_channel, out p)) { DebugUtils.Log("You are not in the chat channel: {0}", chat_channel); return false; } } if (encoding_ == FunEncoding.kJson) { Dictionary<string, object> mcast_msg = new Dictionary<string, object>(); mcast_msg["_channel"] = chat_channel; mcast_msg["_bounce"] = true; mcast_msg["sender"] = p.Key; mcast_msg["text"] = text; multicasting_.SendToChannel(mcast_msg); } else { FunChatMessage chat_msg = new FunChatMessage (); chat_msg.sender = p.Key; chat_msg.text = text; FunMulticastMessage mcast_msg = new FunMulticastMessage (); mcast_msg.channel = chat_channel; mcast_msg.bounce = true; Extensible.AppendValue (mcast_msg, (int)MulticastMessageType.chat, chat_msg); multicasting_.SendToChannel(mcast_msg); } return true; }
public static object GetMulticastMessage(FunMulticastMessage msg, MulticastMessageType msg_type) { return(GetMulticastMessage <object>(msg, msg_type)); }