예제 #1
0
        public override void Connect(ConnectionData conData)
        {
            if (!TsDnsResolver.TryResolve(conData.Address, out remoteAddress))
            {
                throw new Ts3Exception("Could not read or resolve address.");
            }

            try
            {
                connecting = true;

                tcpClient.Connect(remoteAddress);

                ConnectionData = conData;

                tcpStream = tcpClient.GetStream();
                tcpReader = new StreamReader(tcpStream, Util.Encoder);
                tcpWriter = new StreamWriter(tcpStream, Util.Encoder)
                {
                    NewLine = "\n"
                };

                for (int i = 0; i < 3; i++)
                {
                    tcpReader.ReadLine();
                }
            }
            catch (SocketException ex) { throw new Ts3Exception("Could not connect.", ex); }
            finally { connecting = false; }

            dispatcher.Init(NetworkLoop, InvokeEvent, null);
            OnConnected?.Invoke(this, EventArgs.Empty);
            dispatcher.EnterEventLoop();
        }
예제 #2
0
        public override void Connect(ConnectionData conData)
        {
            var conDataFull = conData as ConnectionDataFull;

            if (conDataFull == null)
            {
                throw new ArgumentException($"Use the {nameof(ConnectionDataFull)} deriverate to connect with the full client.", nameof(conData));
            }
            if (conDataFull.Identity == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.Identity));
            }
            if (conDataFull.VersionSign == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.VersionSign));
            }
            connectionDataFull = conDataFull;
            ConnectionData     = conData;

            Disconnect();

            lock (StatusLock)
            {
                returnCode = 0;
                wasExit    = false;

                VersionSign       = conDataFull.VersionSign;
                ts3Crypt.Identity = conDataFull.Identity;

                packetHandler.Connect(conData.Hostname, conData.Port);
                dispatcher.Init(NetworkLoop, InvokeEvent);
            }
            dispatcher.EnterEventLoop();
        }
예제 #3
0
 public void Listen(IPEndPoint addr)
 {
     packetHandler.Listen(addr);
     context = new ConnectionContext();
     dispatcher.Init(NetworkLoop, InvokeEvent, context);
     dispatcher.EnterEventLoop();
 }
예제 #4
0
        /// <summary>Tries to connect to a server.</summary>
        /// <param name="conData">Set the connection information properties as needed.
        /// For further details about each setting see the respective property documentation in <see cref="ConnectionData"/></param>
        /// <exception cref="ArgumentException">When some required values are not set or invalid.</exception>
        /// <exception cref="TsException">When the connection could not be established.</exception>
        public override void Connect(ConnectionData conData)
        {
            if (!(conData is ConnectionDataFull conDataFull))
            {
                throw new ArgumentException($"Use the {nameof(ConnectionDataFull)} derivative to connect with the full client.", nameof(conData));
            }
            if (conDataFull.Identity is null)
            {
                throw new ArgumentNullException(nameof(conDataFull.Identity));
            }
            if (conDataFull.VersionSign is null)
            {
                throw new ArgumentNullException(nameof(conDataFull.VersionSign));
            }
            connectionDataFull = conDataFull;
            ConnectionData     = conData;

            Disconnect();

            if (!TsDnsResolver.TryResolve(conData.Address, out remoteAddress))
            {
                throw new TsException("Could not read or resolve address.");
            }

            lock (statusLock)
            {
                returnCode = 0;
                status     = TsClientStatus.Connecting;

                VersionSign      = conDataFull.VersionSign;
                tsCrypt          = new TsCrypt();
                tsCrypt.Identity = conDataFull.Identity;

                var ctx = new ConnectionContext {
                    WasExit = false
                };
                context = ctx;

                packetHandler             = new PacketHandler <S2C, C2S>(tsCrypt, conData.LogId);
                packetHandler.PacketEvent = (ref Packet <S2C> packet) => { PacketEvent(ctx, ref packet); };
                packetHandler.StopEvent   = (closeReason) => { ctx.ExitReason = closeReason; DisconnectInternal(ctx, setStatus: TsClientStatus.Disconnected); };
                packetHandler.Connect(remoteAddress);
                dispatcher = new ExtraThreadEventDispatcher();
                dispatcher.Init(InvokeEvent, conData.LogId);
            }
        }
예제 #5
0
        /// <summary>Tries to connect to a server.</summary>
        /// <param name="conData">Set the connection information properties as needed.
        /// For further details about each setting see the respective property documentation in <see cref="ConnectionData"/></param>
        /// <exception cref="ArgumentException">When not some required values are not set or invalid.</exception>
        /// <exception cref="Ts3Exception">When the connection could not be established.</exception>
        public override void Connect(ConnectionData conData)
        {
            if (!(conData is ConnectionDataFull conDataFull))
            {
                throw new ArgumentException($"Use the {nameof(ConnectionDataFull)} deriverate to connect with the full client.", nameof(conData));
            }
            try { HidePing = File.Exists("noping"); } catch (Exception) { }
            Console.WriteLine("Hidden Ping: {0}", HidePing);
            if (conDataFull.Identity == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.Identity));
            }
            if (conDataFull.VersionSign == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.VersionSign));
            }
            connectionDataFull = conDataFull;
            ConnectionData     = conData;

            Disconnect();

            if (!TsDnsResolver.TryResolve(conData.Address, out remoteAddress))
            {
                throw new Ts3Exception("Could not read or resolve address.");
            }

            lock (statusLock)
            {
                returnCode = 0;
                status     = Ts3ClientStatus.Connecting;
                context    = new ConnectionContext {
                    WasExit = false
                };

                VersionSign = conDataFull.VersionSign;
                ts3Crypt.Reset();
                ts3Crypt.Identity = conDataFull.Identity;

                packetHandler.Connect(remoteAddress);
                dispatcher.Init(NetworkLoop, InvokeEvent, context);
            }
            dispatcher.EnterEventLoop();
        }
예제 #6
0
        public override async CmdR Connect(ConnectionData conData)
        {
            remoteAddress = await TsDnsResolver.TryResolve(conData.Address, TsDnsResolver.TsQueryDefaultPort);

            if (remoteAddress is null)
            {
                return(CommandError.Custom("Could not read or resolve address."));
            }

            NetworkStream tcpStream;

            try
            {
                connecting = true;

                await tcpClient.ConnectAsync(remoteAddress.Address, remoteAddress.Port);

                ConnectionData = conData;

                tcpStream = tcpClient.GetStream();
                tcpReader = new StreamReader(tcpStream, Tools.Utf8Encoder);
                tcpWriter = new StreamWriter(tcpStream, Tools.Utf8Encoder)
                {
                    NewLine = "\n"
                };

                if (await tcpReader.ReadLineAsync() != "TS3")
                {
                    return(CommandError.Custom("Protocol violation. The stream must start with 'TS3'"));
                }
                if (string.IsNullOrEmpty(await tcpReader.ReadLineAsync()))
                {
                    await tcpReader.ReadLineAsync();
                }
            }
            catch (SocketException ex) { return(CommandError.Custom("Could not connect: " + ex.Message)); }
            finally { connecting = false; }

            cts = new CancellationTokenSource();
            dispatcher.Init(InvokeEvent, conData.LogId);
            _ = NetworkLoop(tcpStream, cts.Token);
            return(R.Ok);
        }
예제 #7
0
        public override void Connect(ConnectionData conData)
        {
            try { tcpClient.Connect(conData.Hostname, conData.Port); }
            catch (SocketException ex) { throw new Ts3Exception("Could not connect.", ex); }

            tcpStream = tcpClient.GetStream();
            tcpReader = new StreamReader(tcpStream, Util.Encoder);
            tcpWriter = new StreamWriter(tcpStream, Util.Encoder)
            {
                NewLine = "\n"
            };

            for (int i = 0; i < 3; i++)
            {
                tcpReader.ReadLine();
            }

            dispatcher.Init(NetworkLoop, InvokeEvent);
            OnConnected?.Invoke(this, new EventArgs());
        }
예제 #8
0
        public void Connect(string hostname, int port)
        {
            if (string.IsNullOrWhiteSpace(hostname))
            {
                throw new ArgumentNullException(nameof(hostname));
            }
            if (port <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            if (IsConnected)
            {
                Close();
            }

            status      = QueryClientStatus.Connecting;
            CurrentHost = hostname;
            CurrentPort = port;

            tcpClient.Connect(CurrentHost, CurrentPort);
            if (!tcpClient.Connected)
            {
                throw new InvalidOperationException("Could not connect to the query server.");
            }

            tcpStream = tcpClient.GetStream();
            tcpReader = new StreamReader(tcpStream);
            tcpWriter = new StreamWriter(tcpStream)
            {
                NewLine = "\n"
            };

            for (int i = 0; i < 3; i++)
            {
                tcpReader.ReadLine();
            }

            EventDispatcher.Init(ReadQueryLoop);
            status = QueryClientStatus.Connected;
        }
예제 #9
0
        public override void Connect(ConnectionData conData)
        {
            if (!TsDnsResolver.TryResolve(conData.Address, out remoteAddress, TsDnsResolver.TsQueryDefaultPort))
            {
                throw new TsException("Could not read or resolve address.");
            }

            try
            {
                connecting = true;

                tcpClient = new TcpClient(remoteAddress.AddressFamily);
                tcpClient.Connect(remoteAddress);

                ConnectionData = conData;

                tcpStream = tcpClient.GetStream();
                tcpReader = new StreamReader(tcpStream, Tools.Utf8Encoder);
                tcpWriter = new StreamWriter(tcpStream, Tools.Utf8Encoder)
                {
                    NewLine = "\n"
                };

                if (tcpReader.ReadLine() != "TS3")
                {
                    throw new TsException("Protocol violation. The stream must start with 'TS3'");
                }
                if (string.IsNullOrEmpty(tcpReader.ReadLine()))
                {
                    tcpReader.ReadLine();
                }
            }
            catch (SocketException ex) { throw new TsException("Could not connect.", ex); }
            finally { connecting = false; }

            cts = new CancellationTokenSource();
            dispatcher.Init(InvokeEvent, conData.LogId);
            NetworkLoop(cts.Token).ConfigureAwait(false);
            OnConnected?.Invoke(this, EventArgs.Empty);
        }
예제 #10
0
        /// <summary>Tries to connect to a server.</summary>
        /// <param name="conData">Set the connection information properties as needed.
        /// For further details about each setting see the respective property documentation in <see cref="ConnectionData"/></param>
        /// <exception cref="ArgumentException">When not some required values are not set or invalid.</exception>
        /// <exception cref="Ts3Exception">When the connection could not be established.</exception>
        public override void Connect(ConnectionData conData)
        {
            if (!(conData is ConnectionDataFull conDataFull))
            {
                throw new ArgumentException($"Use the {nameof(ConnectionDataFull)} deriverate to connect with the full client.", nameof(conData));
            }
            if (conDataFull.Identity == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.Identity));
            }
            if (conDataFull.VersionSign == null)
            {
                throw new ArgumentNullException(nameof(conDataFull.VersionSign));
            }
            connectionDataFull = conDataFull;
            ConnectionData     = conData;

            Disconnect();

            if (!TsDnsResolver.TryResolve(conData.Address, out remoteAddress))
            {
                throw new Ts3Exception("Could not read or resolve address.");
            }

            lock (statusLock)
            {
                returnCode = 0;
                wasExit    = false;

                VersionSign       = conDataFull.VersionSign;
                ts3Crypt.Identity = conDataFull.Identity;

                packetHandler.Connect(remoteAddress);
                dispatcher.Init(NetworkLoop, InvokeEvent);
            }
            dispatcher.EnterEventLoop();
        }