Esempio n. 1
0
        private void Constructor(string sendingPacketTypeStr, string requestReturnPacketTypeStr, object packetObject, SendReceiveOptions options)
        {
            if (sendingPacketTypeStr == null || sendingPacketTypeStr == "")
            {
                throw new ArgumentNullException("sendingPacketTypeStr", "The provided string can not be null or zero length.");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options", "The provided SendReceiveOptions cannot be null.");
            }

            if (packetObject == null)
            {
                this.packetData = new StreamSendWrapper(new ThreadSafeStream(new MemoryStream(new byte[0], 0, 0, false, true), true));
            }
            else
            {
                if (options.DataSerializer == null)
                {
                    throw new ArgumentNullException("options", "The provided SendReceiveOptions.DataSerializer cannot be null.");
                }
                this.packetData = options.DataSerializer.SerialiseDataObject(packetObject, options.DataProcessors, options.Options);
            }

            //We only calculate the checkSum if we are going to use it
            string hashStr = null;

            if (NetworkComms.EnablePacketCheckSumValidation)
            {
                hashStr = NetworkComms.MD5Bytes(packetData.ThreadSafeStream.ToArray(packetData.Start, packetData.Length));
            }

            this.packetHeader = new PacketHeader(sendingPacketTypeStr, packetData.Length, requestReturnPacketTypeStr,
                                                 options.Options.ContainsKey("ReceiveConfirmationRequired"),
                                                 hashStr,
                                                 options.Options.ContainsKey("IncludePacketConstructionTime"));

            //Add an identifier specifying the serializers and processors we have used
            this.packetHeader.SetOption(PacketHeaderLongItems.SerializerProcessors, DPSManager.CreateSerializerDataProcessorIdentifier(options.DataSerializer, options.DataProcessors));

            if (NetworkComms.LoggingEnabled)
            {
                NetworkComms.Logger.Trace(" ... creating comms packet. PacketObject data size is " + packetData.Length.ToString() + " bytes");
            }
        }