public void Broadcast(dynamic data, string[] clients, ResponseType type, long timestamp) { ClientMessage cb = new ClientMessage(); cb.clients = clients; cb.Data = data; cb.Type = type; cb.TimeStamp = timestamp; Send(Newtonsoft.Json.JsonConvert.SerializeObject(cb)); }
/// <summary> /// Informs a user of all the existing clients. /// </summary> private static void SendAllUsers(RCATContext RContext, string client) { try { ClientMessage r = new ClientMessage(); r = new ClientMessage(); r.Type = ResponseType.AllUsers; r.clients = new string[1]; r.clients[0] = client; // Using database User[] arr = dataConnector.GetAllUsers(); r.Data = new { Users = arr }; RContext.Send(JsonConvert.SerializeObject(r)); } catch (Exception e) { Log.Error("Exception in SendAllUsers:",e); } }
public void SendToClient(ClientMessage message) { string name = message.clients[0]; UserContext user = Proxy.onlineUsers[name]; try { Message m = new Message(); m.Type = message.Type; m.Data = message.Data; string json = JsonConvert.SerializeObject(m); user.Send(json); } catch { Log.Debug("[PROXY->CLIENT]: User " + user + " not found."); } }
/// <summary> ///send the same data to multiple clients (broadcast contains the data to send and the array of clients to send to) /// </summary> /// <param name="broadcast"></param> public void BroadcastToClients(ClientMessage broadcast) { try { string name = (string)broadcast.Data.SelectToken("n"); UserContext user = null; if (Proxy.onlineUsers.ContainsKey(name)) user = Proxy.onlineUsers[name]; else { Log.Debug("User " + name + " not present in this Proxy"); return; } long lastupdate = user.LastUpdate; if (broadcast.Type == ResponseType.Disconnect) lastupdate = 0; // Just to be sure it will enter next if user.SendingSemaphore.Wait(); if (broadcast.TimeStamp >= lastupdate) { user.LastUpdate = broadcast.TimeStamp; Message m = new Message(); m.Type = broadcast.Type; m.Data = broadcast.Data; string json = JsonConvert.SerializeObject(m); foreach (string client in broadcast.clients) { try { UserContext cl = Proxy.onlineUsers[client]; cl.Send(json); } catch { Log.Debug("[PROXY->CLIENT]: User " + client + " not found."); } } } else { user.LatePackets++; user.SendingSemaphore.Release(); return; } if (broadcast.Type == ResponseType.Disconnect) { if (Proxy.onlineUsers.ContainsKey(name)) Proxy.onlineUsers.Remove(name); } else { user.SentCounter--; if (user.SentCounter <= 0 && lastupdate > 0) { user.SentCounter = UserContext.DefaultSentCounter; long timetoprocess = user.TimeToProcess; LoggingObject logobj = new LoggingObject(broadcast.TimeStamp, user, timetoprocess); ThreadPool.QueueUserWorkItem(new WaitCallback(LogRoundTrip), logobj); user.TimeToProcess = DateTime.Now.Ticks; } } user.SendingSemaphore.Release(); } catch (Exception e) { Log.Error(e); } }