/// <summary> /// Notifies all users that a user pressed the skip button /// </summary> public void UpdateSkipAudio() { Dictionary <string, object> data = new Dictionary <string, object>(); foreach (User user in userList) { try { user.socket.Send(PacketEncoding.EncodeResponsePacket(new packets.ResponsePacket(data, PacketType.skipAudio))); } catch (Exception error) { userList.Remove(user); Console.WriteLine(Server.GetLineAndFile() + error.Message); } } }
/// <summary> /// Sends an updated usernames list to all connected users /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void UpdateNicknames(object sender, ListChangedEventArgs e) { Dictionary <string, object> data = new Dictionary <string, object>() { { "nicknames", userList.Select(u => u.Nickname).ToList() } }; foreach (User user in userList) { try { user.socket.Send(PacketEncoding.EncodeResponsePacket(new packets.ResponsePacket(data, PacketType.updateNickname))); } catch (Exception error) { userList.Remove(user); Console.WriteLine(Server.GetLineAndFile() + error.Message); } } }
/// <summary> /// Sends an updated playlist to all connected users /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void UpdatePlaylist(object sender, ListChangedEventArgs e) { Dictionary <string, object> data = new Dictionary <string, object>() { { "playlist", videoQueue } }; // Send new video details to all clients foreach (User user in userList) { try { user.socket.Send(PacketEncoding.EncodeResponsePacket(new packets.ResponsePacket(data, PacketType.addToPlaylist))); } catch (Exception error) { userList.Remove(user); Console.WriteLine(Server.GetLineAndFile() + error.Message); } } }
/// <summary> /// Runs on a seperate thread and handles client communication. /// </summary> /// <param name="user">The user this thread handles.</param> public static void ClientHandler(User user) { // Contains the ip, port and other info about the client socket IPEndPoint remoteIpEndPoint; // Data packet byte[] packetBuffer; // Packet header byte array byte[] packetHeaderBuffer = new byte[5]; // Length of the next packet byte[] packetLengthBuffer = new byte[4]; // Length of the next packet (int) int packetLen; // Type of packet int packetType; // Packet stored in correct object Packet packet; ResponsePacket responsePacket; remoteIpEndPoint = user.socket.RemoteEndPoint as IPEndPoint; Console.WriteLine($"Connection from {remoteIpEndPoint.Address.MapToIPv4()}:{remoteIpEndPoint.Port}"); while (user.socket.Connected) { try { // If the byte count of the length packet is not 5, ignore this packet if (user.socket.Receive(packetHeaderBuffer) != 5) { continue; } } catch (SocketException e) { user.socket.Close(); channel.userList.Remove(user); Console.WriteLine(GetLineAndFile() + ": " + e.Message); continue; } // If the system architecture is little-endian ( little end first in array ) // reverse the byte array. if (BitConverter.IsLittleEndian) { Array.Reverse(packetHeaderBuffer, 0, 4); } // Get packet len from first 4 bytes of packet header buffer packetLen = BitConverter.ToInt32(packetHeaderBuffer, 0); // Get packet type from the last byte of the array packetType = packetHeaderBuffer[4]; // Initiate packet byte array with correct size packetBuffer = new byte[packetLen]; // Receive data packet user.socket.Receive(packetBuffer); packet = PacketEncoding.DecodePacket(packetBuffer, packetType); responsePacket = packet.Execute(user); if (responsePacket != null) { try { user.socket.Send(PacketEncoding.EncodeResponsePacket(responsePacket)); } catch (SocketException e) { user.socket.Close(); channel.userList.Remove(user); Console.WriteLine(GetLineAndFile() + ": " + e.Message); } } } }