void SendConnectionAccepted(UdpClientData udpClientData) { ConnectionAcceptedPacket connectionAcceptedPacket = new ConnectionAcceptedPacket(); ConnectionAcceptedData connectionAcceptedData; connectionAcceptedData.clientID = udpClientData.id; connectionAcceptedData.clientsInSession = (uint)udpClientsIDs.Count; connectionAcceptedPacket.Payload = connectionAcceptedData; PacketsManager.Instance.SendPacket(connectionAcceptedPacket, udpClientData.ipEndPoint); }
private void WebsocketPacketHandler_SocketMessageReceived(List <Packet> packets) { // Server may send more than one packet. // ------------------------------------- foreach (var packet in packets) { if (packet.Header == ConnectionAcceptedPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } SendPacket(new ClientInfoPacket(false, UserAgent, packet.Data["hash"].ToString(), 0, false)); SendPacket(new OpenAcknowledgedPacket()); var eventArgs = new ConnectionAcceptedEventArgs(packet.Data["conn_id"].ToString(), packet.Data["hash"].ToString()); ConnectionAccepted?.Invoke(this, eventArgs); continue; } if (packet.Header == ConversationEndedPacket.ToString()) { // Unusual behavior, server sends "convended" without any data // if "flag stranger" packet is sent and no conversation have // been started before. // // Hence, we have to handle it like this. // ----------------------------------------------------------- IsStrangerConnected = false; if (packet.Data != null) { var di = new DisconnectInfo(true, int.Parse(packet.Data.ToString())); var eventArgs = new ConversationEndedEventArgs(di); ConversationEnded?.Invoke(this, eventArgs); } else { var di = new DisconnectInfo(true, -1); var eventArgs = new ConversationEndedEventArgs(di); ConversationEnded?.Invoke(this, eventArgs); } continue; } if (packet.Header == StrangerDisconnectedPacket.ToString()) { if (CurrentCID != packet.Data.ToString() && EncounteredClientIDs.Contains(packet.Data.ToString())) { EncounteredClientIDs.Remove(packet.Data.ToString()); continue; } IsStrangerConnected = false; if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } var di = new DisconnectInfo(false, int.Parse(packet.Data.ToString())); var eventArgs = new ConversationEndedEventArgs(di); ConversationEnded?.Invoke(this, eventArgs); continue; } if (packet.Header == MessageReceivedPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } int postId = -1; if (packet.AdditionalFields.ContainsKey("post_id")) { postId = int.Parse(packet.AdditionalFields["post_id"].ToString()); } var message = new Message( packet.Data["msg"].ToString(), int.Parse(packet.Data["cid"].ToString()), postId, MessageType.Chat ); var eventArgs = new MessageEventArgs(message); MessageReceived?.Invoke(this, eventArgs); continue; } if (packet.Header == OnlinePeopleCountPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } int number; if (!int.TryParse(packet.Data.ToString(), out number)) { number = -1; } var eventArgs = new OnlineCountEventArgs(number); OnlinePeopleCountChanged?.Invoke(this, eventArgs); continue; } if (packet.Header == PingPacket.ToString()) { if (KeepAlive) { PongResponse(); } var eventArgs = new PingEventArgs(DateTime.Now); PingReceived?.Invoke(this, eventArgs); continue; } if (packet.Header == RandomTopicReceivedPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } var message = new Message( packet.Data["topic"].ToString(), int.Parse(packet.Data["cid"].ToString()), int.Parse(packet.AdditionalFields["post_id"].ToString()), MessageType.Topic ); var eventArgs = new MessageEventArgs(message); MessageReceived?.Invoke(this, eventArgs); continue; } if (packet.Header == ServiceMessageReceivedPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } var message = new Message(packet.Data.ToString(), -1, -1, MessageType.Service); var eventArgs = new MessageEventArgs(message); MessageReceived?.Invoke(this, eventArgs); continue; } if (packet.Header == StrangerChatstatePacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } bool writing; if (!bool.TryParse(packet.Data.ToString(), out writing)) { writing = false; } var chatState = writing ? ChatState.Writing : ChatState.Idle; var eventArgs = new ChatstateEventArgs(chatState); StrangerChatstateChanged?.Invoke(this, eventArgs); continue; } if (packet.Header == StrangerFoundPacket.ToString()) { if (packet.Data == null) { throw new Exception("Invalid packet received, packet data is null."); } CurrentContactUID = packet.Data["ckey"].ToString(); SendPacket(new ConversationStartAcknowledged(CurrentContactUID)); ActionID++; EncounteredClientIDs.Add(packet.Data["cid"].ToString()); IsSearchingForStranger = false; IsStrangerConnected = true; var si = new StrangerInfo( int.Parse(packet.Data["cid"].ToString()), packet.Data["ckey"].ToString(), bool.Parse(packet.Data["flaged"].ToString()), packet.Data["info"] ); var eventArgs = new StrangerFoundEventArgs(si); StrangerFound?.Invoke(this, eventArgs); } } }
void ReceiveConnectionData(ushort packetTypeIndex, IPEndPoint ipEndPoint, Stream stream) { switch ((PacketType)packetTypeIndex) { case PacketType.ChallengeRequest: if (!UdpNetworkManager.Instance.IsServer && clientConnectionState == ClientConnectionState.RequestingConnection) { ChallengeRequestPacket challengeRequestPacket = new ChallengeRequestPacket(); challengeRequestPacket.Deserialize(stream); challengeResultGeneratedByClient = saltGeneratedByClient ^ challengeRequestPacket.Payload.serverSalt; clientConnectionState = ClientConnectionState.SendingChallengeResponse; } break; case PacketType.ConnectionAccepted: if (!UdpNetworkManager.Instance.IsServer && clientConnectionState == ClientConnectionState.SendingChallengeResponse) { ConnectionAcceptedPacket connectionAcceptedPacket = new ConnectionAcceptedPacket(); connectionAcceptedPacket.Deserialize(stream); UdpNetworkManager.Instance.SetClientID(connectionAcceptedPacket.Payload.clientID); onClientConnectedCallback?.Invoke(connectionAcceptedPacket.Payload.clientsInSession); onClientConnectedCallback = null; clientConnectionState = ClientConnectionState.Connected; } break; case PacketType.ClientJoined: if (!UdpNetworkManager.Instance.IsServer && clientConnectionState == ClientConnectionState.Connected) { ClientJoinedPacket clientJoinedPacket = new ClientJoinedPacket(); clientJoinedPacket.Deserialize(stream); OnOtherClientJoined.Invoke(clientJoinedPacket.Payload.clientID); } break; case PacketType.ConnectionRequest: if (UdpNetworkManager.Instance.IsServer && !udpClientsIDs.ContainsKey(ipEndPoint)) { if (!udpPendingClientsData.ContainsKey(ipEndPoint)) { ConnectionRequestPacket connectionRequestPacket = new ConnectionRequestPacket(); connectionRequestPacket.Deserialize(stream); AddPendingClient(ipEndPoint, connectionRequestPacket.Payload.clientSalt); } UdpPendingClientData udpPendingClientData = udpPendingClientsData[ipEndPoint]; SendChallengeRequest(udpPendingClientData); } break; case PacketType.ChallengeResponse: if (UdpNetworkManager.Instance.IsServer) { if (udpPendingClientsData.ContainsKey(ipEndPoint)) { ChallengeResponsePacket challengeResponsePacket = new ChallengeResponsePacket(); UdpPendingClientData udpPendingClientData = udpPendingClientsData[ipEndPoint]; challengeResponsePacket.Deserialize(stream); long serverResult = udpPendingClientData.clientSalt ^ udpPendingClientData.serverSalt; if (challengeResponsePacket.Payload.result == serverResult) { ClientJoinedPacket clientJoinedPacket = new ClientJoinedPacket(); ClientJoinedData clientJoinedData; clientJoinedData.clientID = ClientID; clientJoinedPacket.Payload = clientJoinedData; PacketsManager.Instance.SendPacket(clientJoinedPacket, null, 0, 0, reliable: true); AddClient(ipEndPoint); RemovePendingClient(ipEndPoint); OnClientAddedByServer?.Invoke(udpClientsIDs[ipEndPoint]); } } if (udpClientsIDs.ContainsKey(ipEndPoint)) { SendConnectionAccepted(udpClientsData[udpClientsIDs[ipEndPoint]]); } } break; } }