summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Common/Framing/ContentHeaderBody.cs
blob: 82889c23c8548d2899d6093b4cfd4fda42a913b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
using System;
using Apache.Qpid.Buffer;

namespace Apache.Qpid.Framing
{
    public class ContentHeaderBody : IBody
    {
        public static readonly byte TYPE = 2;

        public ushort ClassId;

        public ushort Weight;
        
        public ulong BodySize;

        /** must never be null */
        public IContentHeaderProperties Properties;

        public ContentHeaderBody()
        {
        }

        public ContentHeaderBody(IContentHeaderProperties props, ushort classId)
        {
            Properties = props;
            ClassId = classId;
        }

        public ContentHeaderBody(ushort classId, ushort weight, IContentHeaderProperties props, uint bodySize)
            : this(props, classId)
        {            
            Weight = weight;
            BodySize = bodySize;
        }

        #region IBody Members

        public byte BodyType
        {
            get
            {
                return TYPE;
            }
        }

        public uint Size
        {
            get
            {
                return (2 + 2 + 8 + 2 + Properties.PropertyListSize);
            }
        }

        public void WritePayload(ByteBuffer buffer)
        {
            buffer.Put(ClassId);
            buffer.Put(Weight);
            buffer.Put(BodySize);
            buffer.Put(Properties.PropertyFlags);
            Properties.WritePropertyListPayload(buffer);
        }

        public void PopulateFromBuffer(ByteBuffer buffer, uint size)
        {     
            ClassId = buffer.GetUInt16();
            Weight = buffer.GetUInt16();
            BodySize = buffer.GetUInt64();
            ushort propertyFlags = buffer.GetUInt16();
            ContentHeaderPropertiesFactory factory = ContentHeaderPropertiesFactory.GetInstance();
            Properties = factory.CreateContentHeaderProperties(ClassId, propertyFlags, buffer);    
        }

        #endregion

        public static AMQFrame CreateAMQFrame(ushort channelId, ushort classId, ushort weight, BasicContentHeaderProperties properties,
                                              uint bodySize)
        {
            AMQFrame frame = new AMQFrame();
            frame.Channel = channelId;
            frame.BodyFrame = new ContentHeaderBody(classId, weight, properties, bodySize);
            return frame;
        }

        public static AMQFrame CreateAMQFrame(ushort channelId, ContentHeaderBody body)
        {
            AMQFrame frame = new AMQFrame();
            frame.Channel = channelId;
            frame.BodyFrame = body;
            return frame;
        }

        public override string ToString()
        {
            return String.Format("ContentHeaderBody: ClassId {0}, Weight {1}, BodySize {2}, Properties {3}", ClassId, Weight,
                                 BodySize, Properties);
        }
    }
}