private void readPackage(List <byte> buffer)
        {
            BinaryWalker walker = new BinaryWalker();

            walker.Cursor = 0;
            walker.Buffer = buffer;
            if (walker.TryReadByte(out var type))
            {
                switch ((PackageType)type)
                {
                case PackageType.Ping:
                    lastReceivedPing = DateTime.Now;
                    break;

                case PackageType.ConfigPing:
                    walker.TryReadByte(out var enabled);
                    pingEnabled      = enabled != 0;
                    lastReceivedPing = DateTime.Now;
                    break;

                case PackageType.Message:
                    if (walker.TryReadString(out var str))
                    {
                        MessageHandler(str);
                    }
                    break;

                default:
                    // Unknown package.
                    break;
                }
            }
        }
Beispiel #2
0
 public TraceClient()
 {
     client = new NetClient();
     walker = new BinaryWalker();
     thread = new Thread(new ThreadStart(loop));
     close  = new Mutex();
 }
        public void Poll()
        {
            switch (state)
            {
            case State.Connecting:
                if (connectTask == null)
                {
                    TryNextAddress();
                }
                else if (connectTask.IsCompleted)
                {
                    if (connectTask.IsCanceled || connectTask.IsFaulted)
                    {
                        TryNextAddress();
                    }
                    else
                    {
                        state = (socket != null) && (destination != null) ? State.Connected : State.Idle;
                        if (state == State.Connected)
                        {
                            connectTask = null;
                            BinaryWalker walker = new BinaryWalker();
                            walker.Put((byte)PackageType.Identifier);
                            walker.Put(Identifier);
                            sendMutex.WaitOne();
                            Send(walker.Buffer.ToArray());
                            sendMutex.ReleaseMutex();
                            pingEnabled      = true;
                            lastReceivedPing = DateTime.Now;
                            lastSentPing     = DateTime.Now;
                            Console.WriteLine("Client: Connected");
                        }
                        else
                        {
                            Console.WriteLine("Client: Connection failed");
                        }
                    }
                }
                break;

            case State.Connected:
                if (pingEnabled)
                {
                    var time = DateTime.Now;
                    if (time - lastSentPing > PING_INTERVAL)
                    {
                        lastSentPing = time;
                        byte[] packet = new byte[5];
                        Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet, 0, 4);
                        packet[4] = (byte)PackageType.Ping;
                        Send(packet);
                    }
                }

                if (sendTask != null)
                {
                    if (sendTask.IsFaulted || sendTask.IsCanceled)
                    {
                        Console.WriteLine("Client: Error occurred while sending data.");
                        // We are going to retry this send.
                        sendTask = null;
                    }
                    else if (sendTask.IsCompleted)
                    {
                        sendTask = null;
                        outbound.RemoveAt(0);
                    }
                }
                else
                {
                    sendMutex.WaitOne();
                    sendPackets(outbound);
                    sendMutex.ReleaseMutex();
                }

                if (recieveTask == null)
                {
                    try {
                        recieveTask = socket.ReceiveAsync(new ArraySegment <byte>(recieveBuffer), SocketFlags.None);
                    }
                    catch (SocketException err) {
                        Console.WriteLine("Client: Error occurred while sending or receiving data.");
                        Console.WriteLine("   Error: {0}", err.Message);
                    }
                }
                else if (recieveTask.IsFaulted || recieveTask.IsCanceled)
                {
                    recieveTask = null;
                }
                else if (recieveTask.IsCompleted)
                {
                    var          count  = recieveTask.Result;
                    BinaryWalker walker = new BinaryWalker();
                    walker.Cursor = 0;
                    walker.Buffer = new ArraySegment <byte>(recieveBuffer, 0, count).ToList();
                    while (walker.TryReadInt(out var size))
                    {
                        if (walker.Cursor + size > walker.Size)
                        {
                            break;
                        }

                        readPackage(walker.Buffer.GetRange(walker.Cursor, size));
                        walker.Cursor += size;
                    }

                    recieveTask = null;
                }

                if (pingEnabled && (DateTime.Now - lastReceivedPing) > PING_WAIT_TIME)
                {
                    Close();
                }

                break;

            default:
                break;
            }
        }