package ipp import "encoding/binary" type ippResponseHeader struct { versionNumber versionNumber statusCode statusCode requestId uint32 } func (h *ippResponseHeader) marshal() []byte { a := make([]byte, 8, 8) binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber)) binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode)) binary.BigEndian.PutUint32(a[4:8], h.requestId) return a } type Response struct { header ippResponseHeader operationAttributes []Attribute jobAttributes []Attribute printerAttributes []Attribute } func NewResponse(code statusCode, requestId uint32) *Response { r := new(Response) r.header.versionNumber = 0x0101 r.header.requestId = requestId r.header.statusCode = code r.operationAttributes = make([]Attribute, 0) r.printerAttributes = make([]Attribute, 0) r.jobAttributes = make([]Attribute, 0) return r } // func (r *Response) length() int { // l := 8 + 1 // if len(r.jobAttributes) > 0 { // l += 1 // // for _, e := range r.jobAttributes { // l += e.length() // } // } // for _, e := range r.operationAttributes { // l += e.length() // } // for _, e := range r.printerAttributes { // l += e.length() // } // } func (r *Response) Marshal() []byte { a := make([]byte, 0, 20) a = append(a, r.header.marshal()...) if len(r.operationAttributes) > 0 { a = append(a, byte(operationAttributes)) for _, e := range r.operationAttributes { a = append(a, e.marshal()...) } } if len(r.jobAttributes) > 0 { a = append(a, byte(jobAttributes)) for _, e := range r.jobAttributes { a = append(a, e.marshal()...) } } if len(r.printerAttributes) > 0 { a = append(a, byte(printerAttributes)) for _, e := range r.printerAttributes { a = append(a, e.marshal()...) } } a = append(a, byte(endOfAttributes)) return a } func (r *Response) AddPrinterAttribute(a Attribute) { r.printerAttributes = append(r.printerAttributes, a) } func (r *Response) AddOperatonAttribute(a Attribute) { r.operationAttributes = append(r.operationAttributes, a) } func (r *Response) AddJobAttribute(a Attribute) { r.jobAttributes = append(r.jobAttributes, a) }