Allow optional STOMP frame, rather than CONNECT.
[stompngo.git] / frame_methods.go
blobab2bfc3e664e74763cbf74dcc575552a111cf078
1 //
2 // Copyright © 2011-2019 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 package stompngo
19 import (
20 "bytes"
24 Size returns the size of Frame on the wire, in bytes.
26 func (f *Frame) Size(e bool) int64 {
27 var r int64 = 0
28 r += int64(len(f.Command)) + 1 + f.Headers.Size(e) + 1 + int64(len(f.Body)) + 1
29 return r
33 Bytes returns a byte slice of all frame data, ready for the wire
35 func (f *Frame) Bytes(sclok bool) []byte {
36 b := make([]byte, 0, 8*1024)
37 b = append(b, f.Command+"\n"...)
38 hb := f.Headers.Bytes()
39 if len(hb) > 0 {
40 b = append(b, hb...)
42 b = append(b, "\n"...)
43 if len(f.Body) > 0 {
44 if sclok {
45 nz := bytes.IndexByte(f.Body, 0)
46 // fmt.Printf("WDBG41 ok:%v\n", nz)
47 if nz == 0 {
48 f.Body = []byte{}
49 // fmt.Printf("WDBG42 body:%v bodystring: %v\n", f.Body, string(f.Body))
50 } else if nz > 0 {
51 f.Body = f.Body[0:nz]
52 // fmt.Printf("WDBG43 body:%v bodystring: %v\n", f.Body, string(f.Body))
55 if len(f.Body) > 0 {
56 b = append(b, f.Body...)
59 b = append(b, ZRB...)
61 return b