public static async Task <int> SendDataToUserById(RPCType type, ObjectId id, PackedWebSocketMessage message) { //Find the group var group = Program.holder.FindGroup(new RPCGroupQueryUser { type = type, user_id = id }); if (group == null) { return(0); } //Send event to all await group.SendDistributedMessage(message, new List <LibDeltaSystem.WebFramework.WebSockets.Groups.GroupWebSocketService>()); return(group.clients.Count); }
public static async Task <int> SendDataToServerAdminsById(RPCType type, ObjectId id, PackedWebSocketMessage message, List <GroupWebSocketService> ignoredClients) { //Find the group var group = Program.holder.FindGroup(new RPCGroupQueryServerAdmin { type = type, server_id = id }); if (group == null) { return(0); } //Send event to all await group.SendDistributedMessage(message, ignoredClients); return(group.clients.Count); }
public static async Task <int> SendDataToServerTribeById(RPCType type, ObjectId id, int tribe_id, PackedWebSocketMessage message) { //Find the group var group = Program.holder.FindGroup(new RPCGroupQueryServerTribe { type = type, server_id = id, tribe_id = tribe_id }); if (group == null) { return(0); } //Send event to all await group.SendDistributedMessage(message, new List <LibDeltaSystem.WebFramework.WebSockets.Groups.GroupWebSocketService>()); //Also send this to server admins, as they could also be viewing this tribe int adminCount = await SendDataToServerAdminsById(type, id, message, group.clients); return(group.clients.Count + adminCount); }
/// <summary> /// Sends a message to all clients /// </summary> /// <returns></returns> public async Task SendDistributedMessage(PackedWebSocketMessage packed, List <GroupWebSocketService> ignoredClients) { await SendDistributedMessage(packed.data, packed.length, packed.type, ignoredClients); }
/// <summary> /// Handles an RPC message: opcode 1. This will fire events to clients by a filter /// </summary> /// <param name="data"></param> /// <param name="length"></param> /// <returns></returns> private async Task HandleRPCMessage(byte[] data, int length, ulong index) { //This follows the following format, starting at byte 10 // 4 bytes: int - Payload length "x" // x bytes: string - Payload // 2 bytes: ushort - Filter type // [Dependant on filter type] //Read the length of the payload and read the payload int payloadLength = BinaryTool.ReadInt32(data, 10); byte[] payload = BinaryTool.CopyFromArray(data, 14, payloadLength); Log("RPC-MESSAGE", "Payload length " + payloadLength); //Read the filter code ushort filterCode = BinaryTool.ReadUInt16(data, 14 + payloadLength); Log("RPC-MESSAGE", "Filter type " + filterCode); //Pack contents PackedWebSocketMessage packed = new PackedWebSocketMessage(payload, payloadLength, WebSocketMessageType.Text); //Read the filter and send messages Task pending; if (filterCode == 0) { //USER_ID - Read the user ID // 12 bytes: MongoDB ID - User ID ObjectId userId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); pending = RPCEventDispatcher.SendDataToUserById(RPCType.RPCSession, userId, packed); } else if (filterCode == 1) { //SERVER - Sends to all members of a server // 12 bytes: MongoDB ID - Server ID ObjectId serverId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); pending = RPCEventDispatcher.SendDataToServerById(RPCType.RPCSession, serverId, packed); } else if (filterCode == 2) { //SERVER_TRIBE - Sends to a tribe of a server // 12 bytes: MongoDB ID - Server ID // 4 bytes: Int - ARK Tribe ID ObjectId serverId = BinaryTool.ReadMongoID(data, 14 + payloadLength + 2); int tribeId = BinaryTool.ReadInt32(data, 14 + payloadLength + 2 + 12); pending = RPCEventDispatcher.SendDataToServerTribeById(RPCType.RPCSession, serverId, tribeId, packed); } else { await WriteResponse(index, 400); return; } //Wait for sending to complete Log("RPC-MESSAGE", "Sending message....pending..."); await pending; Log("RPC-MESSAGE", "Message sent OK!"); //Send OK await WriteResponse(index, 0); }