<commit_before>﻿//  Copyright 2011 <NAME>, <NAME>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SerializerBase;

namespace NetworkCommsDotNet
{
    /// <summary>
    /// Wrapper for the entire comms packet. Packet consists of header and data in serialised byte[] form.
    /// </summary>
    class Packet
    {
        PacketHeader packetHeader;
        byte[] packetData;

        public Packet(string packetTypeStr, bool recieveConfirmationRequired, object packetObject, ISerialize serializer, ICompress compressor)
        {
            if (packetTypeStr == null)
                throw new ArgumentNullException("packetTypeStr should never be null.");

            //We can gain performance if we are just sending a byte array directly
            bool pureBytesInPayload = false;
            if (packetObject is byte[])
            {
                this.packetData = packetObject as byte[];
                pureBytesInPayload = true;
            }
            else
            {
                IThreadSafeSerialise packetObjectInt = packetObject as IThreadSafeSerialise;
                if (packetObjectInt != null)
                    this.packetData = packetObjectInt.ThreadSafeSerialise();
                else
                    this.packetData = serializer.SerialiseDataObject(packetObject, compressor);
            }

            //We only calculate the md5 if we are going to use it
            long dataHashValue = 0;
            if (NetworkComms.EnablePacketCheckSumValidation)
                dataHashValue = Adler32.GenerateCheckSum(packetData);

            this.packetHeader = new PacketHeader(packetTypeStr, recieveConfirmationRequired, dataHashValue, packetData.Length, pureBytesInPayload);
#if logging
                logger.Debug("... creating comms packet. Total packet size is " + FBPSerialiser.SerialiseDataObject(packetHeader).Length + packetData.Length + " bytes. PacketObject data size is " + packetData.Length + " bytes");
#endif
        }
<commit_msg>Missed a bug when sending byte array. Now fixed.<commit_after>﻿//  Copyright 2011 <NAME>, <NAME>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SerializerBase;

namespace NetworkCommsDotNet
{
    /// <summary>
    /// Wrapper for the entire comms packet. Packet consists of header and data in serialised byte[] form.
    /// </summary>
    class Packet
    {
        PacketHeader packetHeader;
        byte[] packetData;

        public Packet(string packetTypeStr, bool recieveConfirmationRequired, object packetObject, ISerialize serializer, ICompress compressor)
        {
            if (packetTypeStr == null)
                throw new ArgumentNullException("packetTypeStr should never be null.");

            //We can gain performance if we are just sending a byte array directly
            bool pureBytesInPayload = false;
            //if (packetObject is byte[] && compressor is NullCompressor)
            //{
            //    this.packetData = packetObject as byte[];
            //    pureBytesInPayload = true;
            //}
            //else
            //{
                IThreadSafeSerialise packetObjectInt = packetObject as IThreadSafeSerialise;
                if (packetObjectInt != null)
                    this.packetData = packetObjectInt.ThreadSafeSerialise();
                else
                    this.packetData = serializer.SerialiseDataObject(packetObject, compressor);
            //}

            //We only calculate the md5 if we are going to use it
            long dataHashValue = 0;
            if (NetworkComms.EnablePacketCheckSumValidation)
                dataHashValue = Adler32.GenerateCheckSum(packetData);

            this.packetHeader = new PacketHeader(packetTypeStr, recieveConfirmationRequired, dataHashValue, packetData.Length, pureBytesInPayload);
#if logging
                logger.Debug("... creating comms packet. Total packet size is " + FBPSerialiser.SerialiseDataObject(packetHeader).Length + packetData.Length + " bytes. PacketObject data size is " + packetData.Length + " bytes");
#endif
        }

{'fraction_non_alphanumeric': 0.07654275773667826, 'fraction_numerical': 0.003296099615455045, 'mean_word_length': 2.454775458570525, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1977490', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1307, 'n_words': 507}