コード例 #1
0
        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);
            }
        }