void Session_Data(RudpSession session, byte[] data) { G2Header root = new G2Header(data); if (G2Protocol.ReadPacket(root)) { switch (root.Name) { case ChatPacket.Data: ReceiveMessage(ChatText.Decode(root), session); break; case ChatPacket.Status: ReceiveStatus(ChatStatus.Decode(root), session); break; case ChatPacket.Invite: ReceiveInvite(ChatInvite.Decode(root), session); break; case ChatPacket.Who: ReceiveWho(ChatWho.Decode(root), session); break; } } }
void SendWhoResponse(ChatRoom room, RudpSession session) { Debug.Assert(!IsCommandRoom(room.Kind)); List <ChatWho> whoPackets = new List <ChatWho>(); ChatWho who = new ChatWho(); who.RoomID = room.RoomID; whoPackets.Add(who); room.Members.LockReading(delegate() { foreach (ulong id in room.Members) { if (Network.RudpControl.GetActiveSessions(id).Count > 0) // only send members who are connected { who.Members.Add(id); if (who.Members.Count > 40) // 40 * 8 = 320 bytes { who = new ChatWho(); who.RoomID = room.RoomID; whoPackets.Add(who); } } } }); // send who to already connected locations foreach (ChatWho packet in whoPackets) { session.SendData(ServiceID, 0, packet); } }
void SendWhoRequest(ChatRoom room, RudpSession session) { ChatWho whoReq = new ChatWho(); whoReq.Request = true; whoReq.RoomID = room.RoomID; session.SendData(ServiceID, 0, whoReq); }
void ReceiveWho(ChatWho who, RudpSession session) { // if in room ChatRoom room; // if not in room, send status if (!RoomMap.TryGetValue(who.RoomID, out room)) { SendStatus(session); return; } // if room not public, and not from verified private room member or host, igonre if (IsCommandRoom(room.Kind) || (room.Kind == RoomKind.Secret && !room.Verified.ContainsKey(session.UserID))) { return; } if (!room.Active) { return; } // if requset if (who.Request) { SendWhoResponse(room, session); } // if reply else { // add members to our own list foreach (ulong id in who.Members) { if (!room.Members.SafeContains(id)) { room.AddMember(id); if (Trust != null && Trust.GetTrust(id) == null) { Trust.Research(id, 0, false); } Core.Locations.Research(id); } } // connect to new members ConnectRoom(room); } }
public static ChatWho Decode(G2Header root) { ChatWho who = new ChatWho(); G2Protocol.ResetPacket(root); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Request: who.Request = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_RoomID: who.RoomID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Members: if (child.PayloadSize % 8 == 0) { int offset = 0; while (offset < child.PayloadSize) { UInt64 id = BitConverter.ToUInt64(child.Data, child.PayloadPos + offset); who.Members.Add(id); offset += 8; } } break; } } return(who); }
public static ChatWho Decode(G2Header root) { ChatWho who = new ChatWho(); G2Protocol.ResetPacket(root); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_Request: who.Request = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_RoomID: who.RoomID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Members: if (child.PayloadSize % 8 == 0) { int offset = 0; while (offset < child.PayloadSize) { UInt64 id = BitConverter.ToUInt64(child.Data, child.PayloadPos + offset); who.Members.Add(id); offset += 8; } } break; } } return who; }