go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/write/write.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package write contains the structures describing write request.
     6  // See xrootd protocol specification (http://xrootd.org/doc/dev45/XRdv310.pdf, p. 124) for details.
     7  package write // import "go-hep.org/x/hep/xrootd/xrdproto/write"
     8  
     9  import (
    10  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    11  	"go-hep.org/x/hep/xrootd/xrdfs"
    12  	"go-hep.org/x/hep/xrootd/xrdproto"
    13  )
    14  
    15  // RequestID is the id of the request, it is sent as part of message.
    16  // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format.
    17  const RequestID uint16 = 3019
    18  
    19  // Request holds write request parameters.
    20  type Request struct {
    21  	Handle xrdfs.FileHandle
    22  	Offset int64
    23  	pathID xrdproto.PathID
    24  	_      [3]uint8
    25  	Data   []uint8
    26  }
    27  
    28  // MarshalXrd implements xrdproto.Marshaler.
    29  func (req Request) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    30  	wBuffer.WriteBytes(req.Handle[:])
    31  	wBuffer.WriteI64(req.Offset)
    32  	wBuffer.WriteU8(uint8(req.pathID))
    33  	wBuffer.Next(3)
    34  	wBuffer.WriteLen(len(req.Data))
    35  
    36  	// If we are using a non-zero path ID, then data goes to the other connection.
    37  	// Otherwise, marshal it.
    38  	if req.pathID == 0 {
    39  		wBuffer.WriteBytes(req.Data)
    40  	}
    41  	return nil
    42  }
    43  
    44  // UnmarshalXrd implements xrdproto.Unmarshaler.
    45  func (req *Request) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    46  	rBuffer.ReadBytes(req.Handle[:])
    47  	req.Offset = rBuffer.ReadI64()
    48  	req.pathID = xrdproto.PathID(rBuffer.ReadU8())
    49  	rBuffer.Skip(3)
    50  	req.Data = make([]uint8, rBuffer.ReadLen())
    51  	rBuffer.ReadBytes(req.Data)
    52  	return nil
    53  }
    54  
    55  // ReqID implements xrdproto.Request.ReqID.
    56  func (req *Request) ReqID() uint16 { return RequestID }
    57  
    58  // ShouldSign implements xrdproto.Request.ShouldSign.
    59  func (req *Request) ShouldSign() bool { return false }
    60  
    61  // PathID implements xrdproto.DataRequest.PathID.
    62  func (o *Request) PathID() xrdproto.PathID {
    63  	return o.pathID
    64  }
    65  
    66  // PathID implements xrdproto.DataRequest.SetPathID.
    67  func (o *Request) SetPathID(pathID xrdproto.PathID) {
    68  	o.pathID = pathID
    69  }
    70  
    71  // PathID implements xrdproto.DataRequest.Direction.
    72  func (o *Request) Direction() xrdproto.DataRequestDirection {
    73  	return xrdproto.DataRequestWrite
    74  }
    75  
    76  // PathID implements xrdproto.DataRequest.PathData.
    77  func (o *Request) PathData() []byte {
    78  	if o.pathID != 0 {
    79  		return o.Data
    80  	}
    81  	return nil
    82  }
    83  
    84  var (
    85  	_ xrdproto.DataRequest = (*Request)(nil)
    86  )