public override async Task <bool> TryHandleAsync(Packet packet) { using (var receiveStream = new MemoryStream(packet.Data)) using (var reader = new BinaryReader(receiveStream)) { var header = JsonConvert.DeserializeObject <SendHeader>(reader.ReadString()); if (header.Action != "send") { return(false); } var sendingPackets = header.To.Select(data => new Session(data)) .Select(session => session.GetIPEndPoint()) .Select(endPoint => new Packet { IPEndPoint = endPoint, Data = packet.Data }); foreach (var sendingPacket in sendingPackets) { await sender.SendAsync(packet); } return(true); } }
private async Task SendAllRoomsToAsync(IPEndPoint iPEndPoint) { using (var responseStream = new MemoryStream()) using (var writer = new BinaryWriter(responseStream)) { var allRooms = await rooms.GetAllAsync(); var jsonRooms = allRooms.Aggregate(new JObject(), (result, item) => { result.Add(item.Session, item.Description); return(result); }); var jsonResult = new JObject { ["Action"] = "rooms", ["Rooms"] = jsonRooms }; writer.Write(jsonResult.ToString()); await sender.SendAsync(new Packet { IPEndPoint = iPEndPoint, Data = responseStream.ToArray() }); } }
public override async Task <bool> TryHandleAsync(Packet packet) { using (var receiveStream = new MemoryStream(packet.Data)) using (var reader = new BinaryReader(receiveStream)) using (var responseStream = new MemoryStream()) using (var writer = new BinaryWriter(responseStream)) { var header = JsonConvert.DeserializeObject <SessionHeader>(reader.ReadString()); if (header.Action != "session") { return(false); } var response = new { Action = "session", Session = new Session(packet.IPEndPoint).ToString() }; var json = JsonConvert.SerializeObject(response); writer.Write(json); await sender.SendAsync(new Packet { IPEndPoint = packet.IPEndPoint, Data = responseStream.ToArray() }); return(true); } }
public override async Task <bool> TryHandleAsync(Packet packet) { using (var receiveStream = new MemoryStream(packet.Data)) using (var reader = new BinaryReader(receiveStream)) { var header = JsonConvert.DeserializeObject <PingHeader>(reader.ReadString()); if (header.Action != "ping") { return(false); } var toSession = new Session(header.To); var toEndPoint = toSession.GetIPEndPoint(); var toPacket = new Packet { IPEndPoint = toEndPoint, Data = packet.Data }; await sender.SendAsync(toPacket); return(true); } }