Exemplo n.º 1
0
        /// <summary>
        /// Writes the packets to the stream.
        /// </summary>
        private void WriteSubWork()
        {
            lock (sendPackets)
            {
                while (sendPackets.Count > 0)
                {
                    Packet packet = null;
                    if (!sendPackets.TryDequeue(out packet))
                    {
                        continue;
                    }

                    //Prepare some data in the packet.
                    packet.BeforeSend();

                    /*              Packet structure:
                     *              1. [16bits] packet type
                     *              2. [32bits] packet length
                     *              3. [xxbits] packet data                 */

                    byte[] packetData   = packet.GetBytes();
                    byte[] packetLength = BitConverter.GetBytes(packetData.Length);
                    byte[] packetByte   = new byte[2 + packetLength.Length + packetData.Length];

                    packetByte[0] = (byte)(typeByte[packet.GetType()]);
                    packetByte[1] = (byte)(typeByte[packet.GetType()] >> 8);
                    Array.Copy(packetLength, 0, packetByte, 2, packetLength.Length);
                    Array.Copy(packetData, 0, packetByte, 2 + packetLength.Length, packetData.Length);
                    WriteBytes(packetByte);
                }

                if (KeepAlive && nextPingStopWatch.ElapsedMilliseconds >= PING_INTERVALL)
                {
                    nextPingStopWatch.Reset();
                    currentPingStopWatch.Restart();
                    Send(new PingRequest());
                }
                else if (currentPingStopWatch.ElapsedMilliseconds >= Timeout)
                {
                    ConfigPing(KeepAlive);
                    currentPingStopWatch.Reset();
                    CloseHandler(CloseReason.Timeout);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes all queued <see cref="Packet"/> objects in the <see cref="sendPackets"/> queue to the network.
        /// </summary>
        private void WriteSubWork()
        {
            while (sendPackets.Count > 0)
            {
                Tuple <Packet, object> packetWithObject = null;
                if (!sendPackets.TryDequeue(out packetWithObject))
                {
                    continue;
                }

                Packet packet = packetWithObject.Item1;

                //Insert the ID into the packet if it is an request packet.
                if (packet.GetType().IsSubclassOf(typeof(RequestPacket)) && packetWithObject.Item2 != null)
                {
                    packet.ID = packetHandlerMap[requestResponseMap[packet.GetType()], packetWithObject.Item2];
                }

                //Prepare some data in the packet.
                packet.BeforeSend();

                /*              Packet structure:
                 *              1. [16bits] packet type
                 *              2. [32bits] packet length
                 *              3. [xxbits] packet data                 */

                byte[] packetData   = packetConverter.GetBytes(packet);
                byte[] packetLength = BitConverter.GetBytes(packetData.Length);
                byte[] packetByte   = new byte[2 + packetLength.Length + packetData.Length];

                packetByte[0] = (byte)(typeByte[packet.GetType()]);
                packetByte[1] = (byte)(typeByte[packet.GetType()] >> 8);
                Array.Copy(packetLength, 0, packetByte, 2, packetLength.Length);
                Array.Copy(packetData, 0, packetByte, 2 + packetLength.Length, packetData.Length);
                WriteBytes(packetByte);

                Logger.LogOutgoingPacket(packetData, packet);
            }
        }