public void TCPSendMessage(string message) { if (_tcpClient.Connected) { //checks if message is a command if (!message.StartsWith("/")) { //create message packet ChatMessagePacket packet = new ChatMessagePacket(EncryptString("Default"), EncryptString(message)); //serialize and send packet to all clients TCPSerialize(packet); } else { //creates command message packet CommandMessagePacket packet = new CommandMessagePacket(EncryptString(message)); //send message to client console Console.WriteLine(message); //serialize and send packet to all clients TCPSerialize(packet); } } }
public Packet TCPRead() { lock (_readLock) { try { int numberOfBytes; if ((numberOfBytes = _reader.ReadInt32()) != -1) { byte[] buffer = _reader.ReadBytes(numberOfBytes); MemoryStream memoryStream = new MemoryStream(buffer); Packet packet = _binaryFormatter.Deserialize(memoryStream) as Packet; //decrypt relevant packets switch (packet.EPacketType) { case PacketType.CHATMESSAGE: ChatMessagePacket chatMessagePacket = (ChatMessagePacket)packet; return(new ChatMessagePacket(Decrypt(chatMessagePacket.OriginClient), Decrypt(chatMessagePacket.Message))); case PacketType.COMMANDMESSAGE: CommandMessagePacket commandMessagePacket = (CommandMessagePacket)packet; return(new CommandMessagePacket(Decrypt(commandMessagePacket.Message))); case PacketType.CLIENTNAME: ClientNamePacket clientNamePacket = (ClientNamePacket)packet; return(new ClientNamePacket(Decrypt(clientNamePacket.ClientName))); } return(packet); } else { return(null); } } catch (Exception e) { Console.WriteLine("[Error] " + e.Message + e.StackTrace); return(null); } } }
private void TCPClientMethod(int index) { Packet receivedPacket; //loop to allow continuous communication while((receivedPacket = _clients[index].TCPRead()) != null) { try { switch (receivedPacket.EPacketType) { case PacketType.EMPTY: EmptyPacket emptyPacket = (EmptyPacket)receivedPacket; break; case PacketType.CHATMESSAGE: ChatMessagePacket chatMessagePacket = (ChatMessagePacket)receivedPacket; try { //reencrypt data into new packet so that all the clients are able to decrypt it with their private key ChatMessagePacket sendPacket = new ChatMessagePacket(UTF8.GetBytes(_clients[index].clientData.clientNickname), chatMessagePacket.Message) { Time = chatMessagePacket.Time }; //send messages to all clients TCPSendPacketToAll(sendPacket); //print client messages to server console OutputLog(chatMessagePacket.Time + " " + _clients[index].clientData.clientNickname + ": " + UTF8.GetString(chatMessagePacket.Message)); } catch (Exception e) { OutputLog("[Error] " + e.Message + e.StackTrace); break; } break; case PacketType.COMMANDMESSAGE: CommandMessagePacket commandMessagePacket = (CommandMessagePacket)receivedPacket; //Process command ProcessCommand(_clients[index], UTF8.GetString(commandMessagePacket.Message)); break; case PacketType.CLIENTNAME: ClientNamePacket clientNamePacket = (ClientNamePacket)receivedPacket; bool uniqueName = true; for(int i = 0; i < _clients.Count; i++) { if(UTF8.GetString(clientNamePacket.ClientName) == _clients[i].clientData.clientNickname) { uniqueName = false; } } if(uniqueName) { OutputLog("[Nickname] " + _clients[index].clientData.clientNickname + " has changed their nickname to " + UTF8.GetString(clientNamePacket.ClientName) + "."); _clients[index].clientData.clientNickname = UTF8.GetString(clientNamePacket.ClientName); //Refresh client list ClientListPacket clientListPacket = new ClientListPacket(GetClientNames(), GetClientAddresses()); TCPSendPacketToAll(clientListPacket); //Send name change confirmation ServerMessagePacket serverMessagePacket = new ServerMessagePacket(UTF8.GetBytes("Your server nickname has been changed to " + UTF8.GetString(clientNamePacket.ClientName) + "!")); _clients[index].TCPSend(serverMessagePacket); } else { //Send name change failed ServerMessagePacket serverMessagePacket = new ServerMessagePacket(UTF8.GetBytes("Someone on this server already has the nickname '" + UTF8.GetString(clientNamePacket.ClientName) + "'. Please choose a different nickname!")); _clients[index].TCPSend(serverMessagePacket); } break; case PacketType.DISCONNECTREQUEST: DisconnectRequestPacket disconnectRequestPacket = (DisconnectRequestPacket)receivedPacket; //Announce client has joined to all clients ServerMessagePacket serverMessagePacket3 = new ServerMessagePacket(UTF8.GetBytes(_clients[index].clientData.clientNickname + " has left the server!")); TCPSendPacketToAll(serverMessagePacket3); _clients[index].Close(); break; case PacketType.LOGIN: LoginPacket loginPacket = (LoginPacket)receivedPacket; try { //set server client ip and client key _clients[index].clientData.ipEndPoint = loginPacket.EndPoint; _clients[index]._clientKey = loginPacket.PublicKey; //send server public key back ServerKeyPacket serverKeyPacket = new ServerKeyPacket(_clients[index]._publicKey, true); _clients[index].TCPSend(serverKeyPacket); _clients[index].successfulLogin = true; //Announce client has joined to all clients ServerMessagePacket serverMessagePacket2 = new ServerMessagePacket(UTF8.GetBytes(_clients[index].clientData.clientNickname + " has joined the server!")); TCPSendPacketToAll(serverMessagePacket2); //Refresh client list ClientListPacket clientListPacket2 = new ClientListPacket(GetClientNames(), GetClientAddresses()); TCPSendPacketToAll(clientListPacket2); break; } catch (Exception e) { OutputLog("[Error] " + e.Message + e.StackTrace); break; } } } catch (Exception e) { OutputLog("[Error] " + e.Message + e.StackTrace); break; } } try { _clients[index].Close(); //Take client out of client ConcurrentBag _clients.TryRemove(index, out Client c); } catch (Exception e) { OutputLog("[Error] " + e.Message + e.StackTrace); } }