public static bool Handshake(string IPAddr, int port, bool anon, string username, string password, ConnectionScreen connSc = null) { if (NetworkManager.receiving == true) { NetworkManager.Disconnect(); return(true); } if (NetworkManager.Connect(IPAddr, port, (bool)anon)) { try { NetworkManager.SendMessage("serverInfoRequest", "", enc: false); Dictionary <string, string> message = NetworkManager.ReceiveMessage(); // Receive serverInfo KeyGenerator.SecretKey = KeyGenerator.GetUniqueKey(16); NetworkManager.SendMessage("clientSecret", EncryptionManager.RSAEncrypt(KeyGenerator.SecretKey, message["data"].ToString()), enc: false); message = NetworkManager.ReceiveMessage(true); // Receive gotSecret string version = ConfigManager.GetSetting("version"); List <string> connRequest = new List <string> { username, password, version }; string jsonConnReq = JsonConvert.SerializeObject(connRequest); NetworkManager.SendMessage("connectionRequest", jsonConnReq); message = NetworkManager.ReceiveMessage(true); if (message["messagetype"] == "CRAccepted") { NetworkManager.ReceiveMessages(); VisualManager.ClearUsers(); VisualManager.ClearChan(); VisualManager.SystemMessage("Handshake complete"); if (connSc != null) { connSc.Close(); } handshakeInfo["ip"] = IPAddr; handshakeInfo["port"] = port; handshakeInfo["username"] = username; handshakeInfo["password"] = password; handshakeInfo["anon"] = anon; return(true); } else if (message["messagetype"] == "CRDenied") { VisualManager.SystemMessage("Connection denied - " + message["data"]); if (connSc != null) { connSc.Close(); } return(false); } return(false); } catch (System.Net.Sockets.SocketException) { VisualManager.SystemMessage("Connection was lost during handshake"); if (connSc != null) { connSc.Close(); } return(false); } } else { VisualManager.SystemMessage("Connection failed"); return(false); } }
public static void RecvLoop() { ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = new byte[20480]; try { Queue netQueue = new Queue(); while (receiving == true) { int bytesRec = conn.Receive(bytes); string jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec); netQueue.Clear(); if (jsonData.Length > 0) { if (jsonData[0].Equals('[') && jsonData[(jsonData.Length - 1)].Equals(']')) { netQueue.Enqueue(jsonData); } else { string incompleteMessage = jsonData; bool messageComplete = false; while (messageComplete == false) { bytesRec = conn.Receive(bytes); jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec); if (jsonData[(jsonData.Length - 1)].Equals(']')) { messageComplete = true; incompleteMessage += jsonData; netQueue.Enqueue(incompleteMessage); } else { incompleteMessage += jsonData; } } } } foreach (string netMessage in netQueue) { List <string> encryptedData = JsonConvert.DeserializeObject <List <string> >(netMessage); jsonData = EncryptionManager.Decrypt(encryptedData[0], KeyGenerator.SecretKey, encryptedData[1]); Dictionary <string, string> message = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonData); switch (message["messagetype"]) { case "serverData": { Net.serverInfo.Handle(message); } break; case "outboundMessage": { Net.outboundMessage.Handle(message); } break; case "channelUpdate": { Net.channelUpdate.Handle(message); } break; case "userlistUpdate": { // Placeholder } break; case "channelHistory": { Net.channelHistory.Handle(message); } break; case "additionalHistory": { Net.additionalHistory.Handle(message); } break; case "commandData": { Net.commandData.Handle(message); } break; case "connectionTerminated": { Net.connectionTerminated.Handle(message); } break; case "errorOccured": { MessageBox.Show("Error - " + message["data"]); } break; default: { MessageBox.Show("Error - Unknown messagetype received"); } break; } } } } catch (System.Net.Sockets.SocketException) { if (receiving == true) { NetworkManager.serverInfo.Clear(); Disconnect(); VisualManager.SystemMessage("Error - Connection Lost"); VisualManager.SystemMessage("Trying to reconnect"); for (int reconnCounter = 0; reconnCounter < 3; reconnCounter++) { bool reconnSuccess = NetworkManager.Handshake((string)handshakeInfo["ip"], (int)handshakeInfo["port"], (bool)handshakeInfo["anon"], (string)handshakeInfo["username"], (string)handshakeInfo["password"]); if (reconnSuccess) { VisualManager.SystemMessage("Reconnect successful"); break; } } } } }