Exemple #1
0
        private int CreateHeader(out int contentLength)
        {
            contentLength = 0;
            if (_header is ContentHeader content)
            {
                if (_message is Stream)
                {
                    throw new NotSupportedException();
                }

                if (_message is byte[] buf)
                {
                    throw new NotSupportedException();
                }

                _bodyStream = _contentStream;
                var pos = _bodyStream.Position;
                if (_message is IDynamicPayload dp)
                {
                    content.PacketFlag |= PacketFlag.MultiContent;
                    var obj = dp.Deconstruct();
                    content.ContentLength = new int[obj.Length];
                    byte[] ct = null;
                    for (var i = 0; i < obj.Length; i++)
                    {
                        var cur = _bodyStream.Position;
                        Serializer.Serialize(obj[i], _bodyStream, out ct);
                        content.ContentLength[i] = checked ((int)(_bodyStream.Position - cur));
                    }

                    content.ContentType = ct;
                }
                else
                {
                    byte[] contentType = ContentHeader.EmptyContent;
                    if (!content.PacketFlag.HasFlag(PacketFlag.NoContent))
                    {
                        Serializer.Serialize(_message, _bodyStream, out contentType);
                    }
                    content.ContentType   = contentType;
                    content.ContentLength = new int[1] {
                        checked ((int)(_bodyStream.Position - pos))
                    };
                }

                _bodyStream.Position = pos;
                contentLength        = checked ((int)(_bodyStream.Length - _bodyStream.Position));

                if (contentLength == 0)
                {
                    content.PacketFlag |= PacketFlag.NoContent;
                }
                if (content.ContentLength.Length > 1)
                {
                    content.PacketFlag |= PacketFlag.MultiContent;
                }
            }

            else if (_header is RawDataHeader raw)
            {
                _bodyStream = (Stream)_message;
                if (raw.ContentLength <= 0)
                {
                    raw.ContentLength = checked ((int)(_bodyStream.Length - _bodyStream.Position));
                }
                contentLength      = raw.ContentLength;
                _disposeBodyStream = raw.DisposeStreamAfterSend;
            }

            _headerStream.TryExtend(512);
            _headerStream.Position = sizeof(short);
            _headerStream.Write(Version);
            _header.Serialize(_headerStream);
            var len = (short)_headerStream.Position;

            if (_headerStream.Position > ushort.MaxValue)
            {
                throw new InvalidDataException("Invalid header");
            }
            _headerStream.Position = 0;
            _headerStream.Write(len);

            return(len);
        }