예제 #1
0
파일: Client.cs 프로젝트: avail/VRCTools
        private void ClientThread()
        {
            try
            {
                if (connectionListener != null)
                {
                    connectionListener.ConnectionStarted();
                }
                VRCModLogger.Log("[VRCMODNW] Connecting to server...");
                socket = new TcpClient();
                socket.ReceiveTimeout = 10000; //10s
                socket.Connect(address, port);
                sslStream = new SslStream(
                    socket.GetStream(),
                    false,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null
                    );
                sslStream.AuthenticateAsClient(address);
                string ln;

                inputStream = new StreamReader(sslStream);
                VRCModLogger.Log("[VRCMODNW] Waiting for connection...");
                if (connectionListener != null)
                {
                    connectionListener.WaitingForConnection();
                }
                while ((ln = inputStream.ReadLine()) != null && ln != "READY")
                {
                    ;
                }

                VRCModLogger.Log("[VRCMODNW] Connecting...");
                if (connectionListener != null)
                {
                    connectionListener.Connecting();
                }
                WriteLine("VRCMODNW_" + clientVersion);
                if ((ln = ReadLine()) == null || !ln.Equals("OK"))
                {
                    throw new Exception("Connection aborted");
                }

                socket.ReceiveTimeout = -1; //no receive timeout

                VRCModLogger.Log("[VRCMODNW] Connected.");
                if (connectionListener != null)
                {
                    connectionListener.Connected();
                }
            }
            catch (Exception e)
            {
                VRCModLogger.LogError("[VRCMODNW] Unable to connect: " + e);
                if (connectionListener != null)
                {
                    connectionListener.ConnectionFailed(e.ToString());
                }
                return;
            }
            try
            {
                socket.ReceiveTimeout = 7000;
                listen = true;
                if (!keepaliveThread.IsAlive)
                {
                    keepaliveThread.Start();
                }
                Listen();
                if (connectionListener != null)
                {
                    connectionListener.Disconnected("Connection closed");
                }
            }
            catch (Exception e)
            {
                VRCModLogger.LogError("[VRCMODNW] Disconnected from server: " + e);
                if (connectionListener != null)
                {
                    connectionListener.Disconnected(e.ToString());
                }
            }
        }