public bool AttemptToConnect(IPEndPoint iPEndPoint, string nickname) { if (ConnectToServer(iPEndPoint)) { Login(nickname); tcpNetworkProcessingThread.Start(); udpNetworkProcessingThread.Start(); clientForm.Title = "Chat Facility | Nickname: " + nickname; return(true); } else { clientForm.UpdateChatWindow("[Error] Failed to Connect to server!", Colors.Red); } return(false); }
private void ProcessServerResponce() { Console.Write(">>>"); Packet packet; while ((packet = Read()) != null) { switch (packet.m_packetType) { case (PacketType.CHATMESSAGE): { ChatMessagePacket chatMessage = (ChatMessagePacket)packet; clientForm.UpdateChatWindow(chatMessage.m_message); break; } case (PacketType.CHATLOG): { ChatLog chatLog = (ChatLog)packet; clientForm.UpdateChatWindow(chatLog.m_ChatLog); break; } case (PacketType.CLIENTLISTUPDATE): { ClientListUpdate clientList = (ClientListUpdate)packet; //clientForm.UpdateClientList(clientList.m_clientName); break; } case (PacketType.EXIT): { ExitPacket exitPacket = (ExitPacket)packet; Console.WriteLine(exitPacket.m_message); break; } default: { break; } } } }
private void ProcessServerResponse() { int byteNum; try { while (connected) { if ((byteNum = reader.ReadInt32()) != 0) { byte[] buffer = reader.ReadBytes(byteNum); MemoryStream memstream = new MemoryStream(buffer); Packet packet = formatter.Deserialize(memstream) as Packet; switch (packet.packetType) { case PacketType.ChatMessage: ChatMessagePacket chatPacket = (ChatMessagePacket)packet; clientForm.UpdateChatWindow(chatPacket.message); break; case PacketType.UserListPacket: UserListPacket listPacket = (UserListPacket)packet; clientForm.UserListBox_Edit(listPacket.userList); break; case PacketType.PrivateMessage: PrivateMessagePacket msgPacket = (PrivateMessagePacket)packet; clientForm.OpenPrivateMessage(msgPacket.sender, msgPacket.message); break; } } else { break; } } } catch (Exception ex) { Console.WriteLine(ex); } }
private void TcpProcessServerResponse() { try { int numberOfBytes; while ((numberOfBytes = m_Reader.ReadInt32()) != -1) { byte[] buffer = m_Reader.ReadBytes(numberOfBytes); MemoryStream memoryStream = new MemoryStream(buffer); Packet packet = m_Formatter.Deserialize(memoryStream) as Packet; switch (packet.packetType) { case PacketType.CHATMESSAGE: ChatMessagePacket chatMessage = (ChatMessagePacket)packet; m_clientForm.UpdateChatWindow(chatMessage.message); break; case PacketType.PRIVATEMESSAGE: break; case PacketType.CLIENTNAME: ClientNamePacket clientName = (ClientNamePacket)packet; m_clientForm.UpdatePeopleList(clientName.name); break; default: break; } } } catch (SocketException e) { Console.WriteLine("Client TCP Read Method exception: " + e.Message); } }
private void TcpProcessServerResponse() { try { int numberOfBytes = 0; while ((numberOfBytes = reader.ReadInt32()) != -1) { byte[] buffer = reader.ReadBytes(numberOfBytes); MemoryStream memoryStream = new MemoryStream(buffer); Packet packet = formatter.Deserialize(memoryStream) as Packet; switch (packet.packetType) { case PacketType.LOGIN: Console.WriteLine("Client [" + clientName + "] TCP 'Login' Packet Received"); LoginPacket loginPacket = (LoginPacket)packet; ServerKey = loginPacket.PublicKey; break; case PacketType.ENCRYPTED_ADMIN: Console.WriteLine("Client [" + clientName + "] TCP 'Admin' Packet Received"); EncryptedAdminPacket adminPacket = (EncryptedAdminPacket)packet; clientForm.adminConnected = BitConverter.ToBoolean(adminPacket.adminConnected, 0); break; case PacketType.ENCRYPTED_SERVER: Console.WriteLine("Client [" + clientName + "] TCP 'Server' Packet Received"); EncryptedServerPacket serverPacket = (EncryptedServerPacket)packet; clientForm.UpdateCommandWindow(DecryptString(serverPacket.message), Color.Black, Color.MediumPurple); break; case PacketType.ENCRYPTED_MESSAGE: Console.WriteLine("Client [" + clientName + "] TCP 'Message' Packet Received"); EncryptedMessagePacket encryptedPacket = (EncryptedMessagePacket)packet; clientForm.UpdateChatWindow(DecryptString(encryptedPacket.message), "left", Color.Black, Color.MediumPurple); break; case PacketType.ENCRYPTED_PRIVATE_MESSAGE: Console.WriteLine("Client [" + clientName + "] TCP 'Private Message' Packet Received"); EncryptedPrivateMessagePacket privatePacket = (EncryptedPrivateMessagePacket)packet; clientForm.UpdateChatWindow(DecryptString(privatePacket.message), "left", Color.Black, Color.LightPink); break; case PacketType.ENCRYPTED_NICKNAME: Console.WriteLine("Client [" + clientName + "] TCP 'Nickname' Packet Received"); EncryptedNicknamePacket namePacket = (EncryptedNicknamePacket)packet; clientName = DecryptString(namePacket.name); break; case PacketType.ENCRYPTED_CLIENT_LIST: Console.WriteLine("Client [" + clientName + "] TCP 'Client List' Packet Received"); EncryptedClientListPacket clientListPacket = (EncryptedClientListPacket)packet; clientForm.UpdateClientList(DecryptString(clientListPacket.name), BitConverter.ToBoolean(clientListPacket.removeText, 0)); break; case PacketType.ENCRYPTED_GLOBAL_MUTE: Console.WriteLine("Client [" + clientName + "] TCP 'Global Mute' Packet Received"); EncryptedGlobalMutePacket mutePacket = (EncryptedGlobalMutePacket)packet; string mutedClient = DecryptString(mutePacket.clientToMute); if (clientForm.mutedClientsGlobal.Contains(mutedClient)) { clientForm.mutedClientsGlobal.Remove(mutedClient); if (mutedClient == clientName) { clientForm.UpdateCommandWindow("You have been unmuted by the Admin.", Color.Black, Color.SkyBlue); } } else { clientForm.mutedClientsGlobal.Add(mutedClient); if (mutedClient == clientName) { clientForm.UpdateCommandWindow("You have been muted globally by the Admin.", Color.Black, Color.IndianRed); } } break; } } } catch (Exception exception) { Console.WriteLine("Client TCP Read Method Exception: " + exception.Message); } }
private void TCPProcessServerResponse() { Packet receivedPacket; while ((receivedPacket = TCPRead()) != null) { try { switch (receivedPacket.EPacketType) { case PacketType.EMPTY: EmptyPacket emptyPacket = (EmptyPacket)receivedPacket; break; case PacketType.CHATMESSAGE: ChatMessagePacket chatMessagePacket = (ChatMessagePacket)receivedPacket; //Write message to client console Console.WriteLine(chatMessagePacket.Time + " " + UTF8.GetString(chatMessagePacket.OriginClient) + ": " + UTF8.GetString(chatMessagePacket.Message)); //Write message to client form _clientForm.UpdateChatWindow(chatMessagePacket.Time + " " + UTF8.GetString(chatMessagePacket.OriginClient) + ": " + UTF8.GetString(chatMessagePacket.Message)); break; case PacketType.PRIVATEMESSAGE: PrivateMessagePacket privateMessagePacket = (PrivateMessagePacket)receivedPacket; //Write pm to client console Console.WriteLine(privateMessagePacket.Time + " [PM] " + UTF8.GetString(privateMessagePacket.OriginClient) + " -> " + UTF8.GetString(privateMessagePacket.PrivateMessage)); //Write message to client form _clientForm.UpdateChatWindow(privateMessagePacket.Time + " [PM] " + UTF8.GetString(privateMessagePacket.OriginClient) + " -> " + UTF8.GetString(privateMessagePacket.PrivateMessage)); break; case PacketType.SERVERMESSAGE: ServerMessagePacket serverMessagePacket = (ServerMessagePacket)receivedPacket; //Write server response to client console Console.WriteLine(serverMessagePacket.Time + " [Server] -> " + UTF8.GetString(serverMessagePacket.Message)); //Write server response to client form _clientForm.UpdateChatWindow(serverMessagePacket.Time + " [Server] -> " + UTF8.GetString(serverMessagePacket.Message)); break; case PacketType.ANNOUNCEMESSAGE: AnnouncementMessagePacket announcementMessagePacket = (AnnouncementMessagePacket)receivedPacket; //Write announcement to client console Console.WriteLine(announcementMessagePacket.Time + " " + "[Announcement] -> " + UTF8.GetString(announcementMessagePacket.Message)); //Write announcement to client form _clientForm.UpdateChatWindow(announcementMessagePacket.Time + " " + "[Announcement] -> " + UTF8.GetString(announcementMessagePacket.Message)); break; case PacketType.SERVERKEY: ServerKeyPacket serverKeyPacket = (ServerKeyPacket)receivedPacket; _serverKey = serverKeyPacket.ServerKey; successfulLogin = serverKeyPacket.Successful; Console.WriteLine("Server Key Packet Received!"); break; case PacketType.CLIENTLIST: ClientListPacket clientListPacket = (ClientListPacket)receivedPacket; _clientForm.RefreshClientList(clientListPacket.ClientList, clientListPacket.ClientIPS); break; } } catch (SocketException e) { Console.WriteLine("[Error] " + e.Message + e.StackTrace); break; } } }