예제 #1
0
        internal void Connect()
        {
            Stealth.AddTraceMessage(string.Format("Connect Stealth client. Host: {0}, Port: {1}", _host, _port),
                                    "Stealth.Network");
            _client = new TcpClient(_host, _port);
            _reader = new BinaryReader(_client.GetStream());
            _writer = new BinaryWriter(_client.GetStream());

            Stealth.AddTraceMessage("Start reciever", "Stealth.Network");
            var factory = new TaskFactory(_cts.Token, TaskCreationOptions.LongRunning,
                                          TaskContinuationOptions.LongRunning, TaskScheduler.Default);

            factory.StartNew(Receiver, _cts.Token);
        }
예제 #2
0
        private void SendPacket(Packet packet)
        {
            while (_writer == null || !_writer.BaseStream.CanWrite)
            {
                Thread.Sleep(10);
            }

            if (Stealth.EnableTracing)
            {
                Stealth.AddTraceMessage(string.Format("Send packet. Type: {0}, Param: {1}",
                                                      packet.Method,
                                                      string.Join(",", packet.Data.Select(b => b.ToString("X2")))),
                                        "Stealth.Network");
            }
            var pb = packet.GetBytes();
            var lb = BitConverter.GetBytes(pb.Length).Reverse().ToArray();

            _writer.Write(lb);
            _writer.Write(pb);
            _writer.Flush();
        }
예제 #3
0
        private void Receiver(object state)
        {
            var token = (CancellationToken)state;

            while (!token.IsCancellationRequested)
            {
                if (_reader != null && _reader.BaseStream.CanRead && ((NetworkStream)_reader.BaseStream).DataAvailable)
                {
                    while (((NetworkStream)_reader.BaseStream).DataAvailable)
                    {
                        var packetLen = BitConverter.ToUInt32(_reader.ReadBytes(4), 0);

                        var packet = new Packet();
                        packet.Method     = (PacketType)_reader.ReadUInt16();
                        packet.DataLength = _reader.ReadInt16();
                        packet.Data       = _reader.ReadBytes(packet.DataLength);

                        if (Stealth.EnableTracing)
                        {
                            Stealth.AddTraceMessage(string.Format("Read packet. Type: {0}, Param: {1}",
                                                                  packet.Method,
                                                                  string.Join(",", packet.Data.Select(b => b.ToString("X2")))),
                                                    "Stealth.Network");
                        }

                        ProcessPacket(packet);
                    }
                }
                else
                {
                    Thread.Sleep(10);
                }
            }

            token.ThrowIfCancellationRequested();
        }