public static void RepeatToAllClientsInChannel <T>(List <T> list, ClientInstance instance) { // Get the index of the client initially sending the message. // This is to get the channel ID from the user, and to only transmit to others with that channel ID. int index = ServerMessage.FindClientKeysIndex(instance.client); string json = Serialization.Serialize(list, false); byte[] data = Serialization.AddEndCharToMessage(json); for (int i = 0; i < ServerHandler.activeClients.Count; i++) { // Same channel check. if (ServerHandler.activeClients[i].ChannelID == ServerHandler.activeClients[index].ChannelID) { byte[] encrypted = EncryptMessage(data, ServerHandler.activeClients[i].RSAModulus, ServerHandler.activeClients[i].RSAExponent); // Try to send to [i] client, if the client does not exist anymore, remove from activeClients. try { StreamHandler.WriteToStream(ServerHandler.activeClients[i].TCPClient.GetStream(), encrypted); } catch (ObjectDisposedException) { ServerHandler.activeClients.RemoveAt(i); } catch (InvalidOperationException) { ServerHandler.activeClients.RemoveAt(i); } } } }
public static void ClientPrivateMessage(TcpClient client, ConsoleColor color, string message, int userID, string username) { List <PrivateMessageFormat> privateMessage = new(); privateMessage.Add(new PrivateMessageFormat { MessageType = MessageTypes.PRIVATEMESSAGE, Message = message, Color = color, RSAExponent = Encryption.RSAExponent, RSAModulus = Encryption.RSAModulus, SenderUsername = username, SenderID = userID }); string json = Serialization.Serialize(privateMessage, false); byte[] data = Serialization.AddEndCharToMessage(json); int index = FindClientKeysIndex(client); byte[] encrypted = MessageHandler.EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent); StreamHandler.WriteToStream(client.GetStream(), encrypted); }
public static void Serialize <T>(List <T> message, ClientInstance instance, bool encrypt) { string json = Serialization.Serialize(message, false); byte[] data = Serialization.AddEndCharToMessage(json); if (encrypt) { int index = ServerMessage.FindClientKeysIndex(instance.client); data = EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent); } StreamHandler.WriteToStream(instance.stream, data); }
public static void ReplyToDataRequest(ClientInstance instance, string message, CommandDataTypes dataType) { List <DataReplyFormat> serverMessage = new(); serverMessage.Add(new DataReplyFormat { MessageType = MessageTypes.DATAREPLY, DataType = dataType, Data = message, }); string json = Serialization.Serialize(serverMessage, false); byte[] data = Serialization.AddEndCharToMessage(json); int index = ServerMessage.FindClientKeysIndex(instance.client); byte[] encrypted = MessageHandler.EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent); StreamHandler.WriteToStream(instance.stream, encrypted); }
/// Serialize the messageFormat with json to transmit. public static void PrepareMessage <T>(List <T> message, TcpClient client, NetworkStream stream, bool Encrypt, bool loopReadInput) { string json = Serialization.Serialize(message, false); byte[] data = Serialization.AddEndCharToMessage(json); // Encrypts message and sends. if (Encrypt) { EncryptSendMessage(data, stream); } // Does not encrypt, just sends. else { StreamHandler.WriteToStream(stream, data); } // When message has been sent, return to reading console input. if (loopReadInput) { InputMessage(client, stream); } }
public static void ServerClientMessage(TcpClient client, ConsoleColor color, string message) { List <ServerMessageFormat> serverMessage = new(); serverMessage.Add(new ServerMessageFormat { MessageType = MessageTypes.SERVER, Message = message, Color = color, RSAExponent = Encryption.RSAExponent, RSAModulus = Encryption.RSAModulus, }); string json = Serialization.Serialize(serverMessage, false); byte[] data = Serialization.AddEndCharToMessage(json); int index = FindClientKeysIndex(client); byte[] encrypted = MessageHandler.EncryptMessage(data, ServerHandler.activeClients[index].RSAModulus, ServerHandler.activeClients[index].RSAExponent); StreamHandler.WriteToStream(client.GetStream(), encrypted); }
public static void RepeatToAllClients <T>(List <T> list) { string json = Serialization.Serialize(list, false); byte[] data = Serialization.AddEndCharToMessage(json); for (int i = 0; i < ServerHandler.activeClients.Count; i++) { byte[] encrypted = EncryptMessage(data, ServerHandler.activeClients[i].RSAModulus, ServerHandler.activeClients[i].RSAExponent); // Try to send to [i] client, if the client does not exist anymore, remove from activeClients. try { StreamHandler.WriteToStream(ServerHandler.activeClients[i].TCPClient.GetStream(), encrypted); } catch (ObjectDisposedException) { ServerHandler.activeClients.RemoveAt(i); } catch (InvalidOperationException) { ServerHandler.activeClients.RemoveAt(i); } } }