<commit_before>﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Procon.Net {

    public class TcpClient<P> : Client<P> where P : Packet {

        /// <summary>
        /// The open client connection.
        /// </summary>
        protected System.Net.Sockets.TcpClient Client;

        /// <summary>
        /// The stream to read and write data to.
        /// </summary>
        protected NetworkStream NetworkStream;

        /// <summary>
        /// Why is this here?
        /// </summary>
        protected readonly Object AcquireSequenceNumberLock = new Object();

        /// <summary>
        /// Buffer for the data currently being read from the stream. This is appended to the received buffer.
        /// </summary>
        protected IPacketStream PacketStream;

        /// <summary>
        /// Lock when reading in new packets.
        /// </summary>
        protected readonly Object ReadPacketLock = new Object();

        /// <summary>
        /// How much data should be read when peeking for the full packet size.
        /// </summary>
        protected virtual long ReadPacketPeekShiftSize {
            get { return this.PacketSerializer != null ? this.PacketSerializer.PacketHeaderSize : 0; }
        }

        /// <summary>
        /// Why is this here?
        /// </summary>
        protected uint SequenceNumber;
        public uint AcquireSequenceNumber {
            get {
                lock (this.AcquireSequenceNumberLock) {
                    return ++this.SequenceNumber;
                }
            }
        }

        protected TcpClient(string hostname, UInt16 port) : base(hostname, port) {
            this.ReceivedBuffer = new byte[this.BufferSize];
            this.PacketStream = new PacketStream();

            this.SequenceNumber = 0;
        }

        /// <summary>
        /// Return for when the packet has been written (or an error occurs during write) to the
        /// server.
        /// </summary>
        /// <param name="ar"></param>
        protected void SendAsynchronousCallback(IAsyncResult ar) {

            P packet = (P)ar.AsyncState;

            if (this.NetworkStream != null) {
                try {
                    this.NetworkStream.EndWrite(ar);

                    this.OnPacketSent(packet);
                }
                catch (SocketException se) {
                    this.Shutdown(se);
                }
                catch (Exception e) {
                    this.Shutdown(e);
                }
            }
        }

        /// <summary>
        /// Sends a packet to the server asynchronously
        /// </summary>
        /// <param name="packet"></param>
        public override void Send(P packet) {

            if (packet != null) {
                if (this.BeforePacketSend(packet) == false && this.NetworkStream != null) {

                    byte[] bytePacket = this.PacketSerializer.Serialize(packet);

                    if (bytePacket != null && bytePacket.Length > 0) {
                        this.NetworkStream.BeginWrite(bytePacket, 0, bytePacket.Length, this.SendAsynchronousCallback, packet);
                    }
                }
            }
        }

        /// <summary>
        /// Attempts to read a single packet from the PacketStream
        /// </summary>
        /// <returns>A completed packet, or null if no packet could be read.</returns>
        protected virtual P ReadPacket() {
            P packet = null;

            lock (this.ReadPacketLock) {
                byte[] header = this.PacketStream.PeekShift((uint)this.ReadPacketPeekShiftSize);

                if (header != null) {
                    long packetSize = this.PacketSerializer.ReadPacketSize(header);

                    byte[] packetData = this.PacketStream.Shift((uint)packetSize);

                    if (packetData != null) {
                        packet = this.PacketSerializer.Deserialize(packetData);
                    }
                }
            }<commit_msg>Fix introduced for the daemon always using the packet header size.<commit_after>﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Procon.Net {

    public class TcpClient<P> : Client<P> where P : Packet {

        /// <summary>
        /// The open client connection.
        /// </summary>
        protected System.Net.Sockets.TcpClient Client;

        /// <summary>
        /// The stream to read and write data to.
        /// </summary>
        protected NetworkStream NetworkStream;

        /// <summary>
        /// Why is this here?
        /// </summary>
        protected readonly Object AcquireSequenceNumberLock = new Object();

        /// <summary>
        /// Buffer for the data currently being read from the stream. This is appended to the received buffer.
        /// </summary>
        protected IPacketStream PacketStream;

        /// <summary>
        /// Lock when reading in new packets.
        /// </summary>
        protected readonly Object ReadPacketLock = new Object();

        /// <summary>
        /// How much data should be read when peeking for the full packet size.
        /// </summary>
        protected virtual long ReadPacketPeekShiftSize {
            get { return this.PacketSerializer != null ? this.PacketSerializer.PacketHeaderSize : 0; }
        }

        /// <summary>
        /// Why is this here?
        /// </summary>
        protected uint SequenceNumber;
        public uint AcquireSequenceNumber {
            get {
                lock (this.AcquireSequenceNumberLock) {
                    return ++this.SequenceNumber;
                }
            }
        }

        protected TcpClient(string hostname, UInt16 port) : base(hostname, port) {
            this.ReceivedBuffer = new byte[this.BufferSize];
            this.PacketStream = new PacketStream();

            this.SequenceNumber = 0;
        }

        /// <summary>
        /// Return for when the packet has been written (or an error occurs during write) to the
        /// server.
        /// </summary>
        /// <param name="ar"></param>
        protected void SendAsynchronousCallback(IAsyncResult ar) {

            P packet = (P)ar.AsyncState;

            if (this.NetworkStream != null) {
                try {
                    this.NetworkStream.EndWrite(ar);

                    this.OnPacketSent(packet);
                }
                catch (SocketException se) {
                    this.Shutdown(se);
                }
                catch (Exception e) {
                    this.Shutdown(e);
                }
            }
        }

        /// <summary>
        /// Sends a packet to the server asynchronously
        /// </summary>
        /// <param name="packet"></param>
        public override void Send(P packet) {

            if (packet != null) {
                if (this.BeforePacketSend(packet) == false && this.NetworkStream != null) {

                    byte[] bytePacket = this.PacketSerializer.Serialize(packet);

                    if (bytePacket != null && bytePacket.Length > 0) {
                        this.NetworkStream.BeginWrite(bytePacket, 0, bytePacket.Length, this.SendAsynchronousCallback, packet);
                    }
                }
            }
        }

        /// <summary>
        /// Attempts to read a single packet from the PacketStream
        /// </summary>
        /// <returns>A completed packet, or null if no packet could be read.</returns>
        protected virtual P ReadPacket() {
            P packet = null;

            lock (this.ReadPacketLock) {
                byte[] header = this.PacketStream.PeekShift((uint)this.ReadPacketPeekShiftSize);

                if (header != null) {
                    long packetSize = this.PacketSerializer.ReadPacketSize(header);

                    byte[] packetData = this.PacketStream.PeekShift((uint)packetSize);

                    if (packetData != null && packetData.Length > 0) {
                        packet = this.PacketSerializer.Deserialize(packetData);

                        this.PacketStream.Shift((uint)packetSize);
                    }
                }
            }
{'fraction_non_alphanumeric': 0.1039628180039139, 'fraction_numerical': 0.0015900195694716242, 'mean_word_length': 1.4681557500754603, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945674', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1903, 'n_words': 551}