Best Rod code snippet using proto.Len
protocol.go
Source:protocol.go
...28 _seqOffset = _opOffset + _opSize29 _heartOffset = _seqOffset + _seqSize30)31var (32 // ErrProtoPackLen proto packet len error33 ErrProtoPackLen = errors.New("default server codec pack length error")34 // ErrProtoHeaderLen proto header len error35 ErrProtoHeaderLen = errors.New("default server codec header length error")36)37var (38 // ProtoReady proto ready39 ProtoReady = &Proto{Op: OpProtoReady}40 // ProtoFinish proto finish41 ProtoFinish = &Proto{Op: OpProtoFinish}42)43// WriteTo write a proto to bytes writer.44func (p *Proto) WriteTo(b *bytes.Writer) {45 var (46 packLen = _rawHeaderSize + int32(len(p.Body))47 buf = b.Peek(_rawHeaderSize)48 )49 binary.BigEndian.PutInt32(buf[_packOffset:], packLen)50 binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize))51 binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver))52 binary.BigEndian.PutInt32(buf[_opOffset:], p.Op)53 binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq)54 if p.Body != nil {55 b.Write(p.Body)56 }57}58// ReadTCP read a proto from TCP reader.59func (p *Proto) ReadTCP(rr *bufio.Reader) (err error) {60 var (61 bodyLen int62 headerLen int1663 packLen int3264 buf []byte65 )66 if buf, err = rr.Pop(_rawHeaderSize); err != nil {67 return68 }69 packLen = binary.BigEndian.Int32(buf[_packOffset:_headerOffset])70 headerLen = binary.BigEndian.Int16(buf[_headerOffset:_verOffset])71 p.Ver = int32(binary.BigEndian.Int16(buf[_verOffset:_opOffset]))72 p.Op = binary.BigEndian.Int32(buf[_opOffset:_seqOffset])73 p.Seq = binary.BigEndian.Int32(buf[_seqOffset:])74 if packLen > _maxPackSize {75 return ErrProtoPackLen76 }77 if headerLen != _rawHeaderSize {78 return ErrProtoHeaderLen79 }80 if bodyLen = int(packLen - int32(headerLen)); bodyLen > 0 {81 p.Body, err = rr.Pop(bodyLen)82 } else {83 p.Body = nil84 }85 return86}87// WriteTCP write a proto to TCP writer.88func (p *Proto) WriteTCP(wr *bufio.Writer) (err error) {89 var (90 buf []byte91 packLen int3292 )93 if p.Op == OpRaw {94 // write without buffer, job concact proto into raw buffer95 _, err = wr.WriteRaw(p.Body)96 return97 }98 packLen = _rawHeaderSize + int32(len(p.Body))99 if buf, err = wr.Peek(_rawHeaderSize); err != nil {100 return101 }102 binary.BigEndian.PutInt32(buf[_packOffset:], packLen)103 binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize))104 binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver))105 binary.BigEndian.PutInt32(buf[_opOffset:], p.Op)106 binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq)107 if p.Body != nil {108 _, err = wr.Write(p.Body)109 }110 return111}112// WriteTCPHeart write TCP heartbeat with room online.113func (p *Proto) WriteTCPHeart(wr *bufio.Writer, online int32) (err error) {114 var (115 buf []byte116 packLen int117 )118 packLen = _rawHeaderSize + _heartSize119 if buf, err = wr.Peek(packLen); err != nil {120 return121 }122 // header123 binary.BigEndian.PutInt32(buf[_packOffset:], int32(packLen))124 binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize))125 binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver))126 binary.BigEndian.PutInt32(buf[_opOffset:], p.Op)127 binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq)128 // body129 binary.BigEndian.PutInt32(buf[_heartOffset:], online)130 return131}132// ReadWebsocket read a proto from websocket connection.133func (p *Proto) ReadWebsocket(ws *websocket.Conn) (err error) {134 var (135 bodyLen int136 headerLen int16137 packLen int32138 buf []byte139 )140 if _, buf, err = ws.ReadMessage(); err != nil {141 return142 }143 if len(buf) < _rawHeaderSize {144 return ErrProtoPackLen145 }146 packLen = binary.BigEndian.Int32(buf[_packOffset:_headerOffset])147 headerLen = binary.BigEndian.Int16(buf[_headerOffset:_verOffset])148 p.Ver = int32(binary.BigEndian.Int16(buf[_verOffset:_opOffset]))149 p.Op = binary.BigEndian.Int32(buf[_opOffset:_seqOffset])150 p.Seq = binary.BigEndian.Int32(buf[_seqOffset:])151 if packLen < 0 || packLen > _maxPackSize {152 return ErrProtoPackLen153 }154 if headerLen != _rawHeaderSize {155 return ErrProtoHeaderLen156 }157 if bodyLen = int(packLen - int32(headerLen)); bodyLen > 0 {158 p.Body = buf[headerLen:packLen]159 } else {160 p.Body = nil161 }162 return163}164// WriteWebsocket write a proto to websocket connection.165func (p *Proto) WriteWebsocket(ws *websocket.Conn) (err error) {166 var (167 buf []byte168 packLen int169 )170 packLen = _rawHeaderSize + len(p.Body)171 if err = ws.WriteHeader(websocket.BinaryMessage, packLen); err != nil {172 return173 }174 if buf, err = ws.Peek(_rawHeaderSize); err != nil {175 return176 }177 binary.BigEndian.PutInt32(buf[_packOffset:], int32(packLen))178 binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize))179 binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver))180 binary.BigEndian.PutInt32(buf[_opOffset:], p.Op)181 binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq)182 if p.Body != nil {183 err = ws.WriteBody(p.Body)184 }185 return186}187// WriteWebsocketHeart write websocket heartbeat with room online.188func (p *Proto) WriteWebsocketHeart(wr *websocket.Conn, online int32) (err error) {189 var (190 buf []byte191 packLen int192 )193 packLen = _rawHeaderSize + _heartSize194 // websocket header195 if err = wr.WriteHeader(websocket.BinaryMessage, packLen); err != nil {196 return197 }198 if buf, err = wr.Peek(packLen); err != nil {199 return200 }201 // proto header202 binary.BigEndian.PutInt32(buf[_packOffset:], int32(packLen))203 binary.BigEndian.PutInt16(buf[_headerOffset:], int16(_rawHeaderSize))204 binary.BigEndian.PutInt16(buf[_verOffset:], int16(p.Ver))205 binary.BigEndian.PutInt32(buf[_opOffset:], p.Op)206 binary.BigEndian.PutInt32(buf[_seqOffset:], p.Seq)207 // proto body208 binary.BigEndian.PutInt32(buf[_heartOffset:], online)209 return210}...
local.go
Source:local.go
...16)17const (18 // Name is the name of the driver19 Name = "local"20 encodeBinaryLen = 421 initialBufSize = 204822 maxDecodeRetry = 2000023 defaultMaxFileSize int64 = 20 * 1024 * 102424 defaultMaxFileCount = 525 defaultCompressLogs = true26)27// LogOptKeys are the keys names used for log opts passed in to initialize the driver.28var LogOptKeys = map[string]bool{29 "max-file": true,30 "max-size": true,31 "compress": true,32}33// ValidateLogOpt looks for log driver specific options.34func ValidateLogOpt(cfg map[string]string) error {35 for key := range cfg {36 if !LogOptKeys[key] {37 return errors.Errorf("unknown log opt '%s' for log driver %s", key, Name)38 }39 }40 return nil41}42func init() {43 if err := logger.RegisterLogDriver(Name, New); err != nil {44 logrus.Fatal(err)45 }46 if err := logger.RegisterLogOptValidator(Name, ValidateLogOpt); err != nil {47 logrus.Fatal(err)48 }49}50type driver struct {51 mu sync.Mutex52 closed bool53 logfile *loggerutils.LogFile54 readers map[*logger.LogWatcher]struct{} // stores the active log followers55}56// New creates a new local logger57// You must provide the `LogPath` in the passed in info argument, this is the file path that logs are written to.58func New(info logger.Info) (logger.Logger, error) {59 if info.LogPath == "" {60 return nil, errdefs.System(errors.New("log path is missing -- this is a bug and should not happen"))61 }62 cfg := newDefaultConfig()63 if capacity, ok := info.Config["max-size"]; ok {64 var err error65 cfg.MaxFileSize, err = units.FromHumanSize(capacity)66 if err != nil {67 return nil, errdefs.InvalidParameter(errors.Wrapf(err, "invalid value for max-size: %s", capacity))68 }69 }70 if userMaxFileCount, ok := info.Config["max-file"]; ok {71 var err error72 cfg.MaxFileCount, err = strconv.Atoi(userMaxFileCount)73 if err != nil {74 return nil, errdefs.InvalidParameter(errors.Wrapf(err, "invalid value for max-file: %s", userMaxFileCount))75 }76 }77 if userCompress, ok := info.Config["compress"]; ok {78 compressLogs, err := strconv.ParseBool(userCompress)79 if err != nil {80 return nil, errdefs.InvalidParameter(errors.Wrap(err, "error reading compress log option"))81 }82 cfg.DisableCompression = !compressLogs83 }84 return newDriver(info.LogPath, cfg)85}86func makeMarshaller() func(m *logger.Message) ([]byte, error) {87 buf := make([]byte, initialBufSize)88 // allocate the partial log entry separately, which allows for easier re-use89 proto := &logdriver.LogEntry{}90 md := &logdriver.PartialLogEntryMetadata{}91 return func(m *logger.Message) ([]byte, error) {92 resetProto(proto)93 messageToProto(m, proto, md)94 protoSize := proto.Size()95 writeLen := protoSize + (2 * encodeBinaryLen) //+ len(messageDelimiter)96 if writeLen > len(buf) {97 buf = make([]byte, writeLen)98 } else {99 // shrink the buffer back down100 if writeLen <= initialBufSize {101 buf = buf[:initialBufSize]102 } else {103 buf = buf[:writeLen]104 }105 }106 binary.BigEndian.PutUint32(buf[:encodeBinaryLen], uint32(protoSize))107 n, err := proto.MarshalTo(buf[encodeBinaryLen:writeLen])108 if err != nil {109 return nil, errors.Wrap(err, "error marshaling log entry")110 }111 if n+(encodeBinaryLen*2) != writeLen {112 return nil, io.ErrShortWrite113 }114 binary.BigEndian.PutUint32(buf[writeLen-encodeBinaryLen:writeLen], uint32(protoSize))115 return buf[:writeLen], nil116 }117}118func newDriver(logPath string, cfg *CreateConfig) (logger.Logger, error) {119 if err := validateConfig(cfg); err != nil {120 return nil, errdefs.InvalidParameter(err)121 }122 lf, err := loggerutils.NewLogFile(logPath, cfg.MaxFileSize, cfg.MaxFileCount, !cfg.DisableCompression, makeMarshaller(), decodeFunc, 0640, getTailReader)123 if err != nil {124 return nil, err125 }126 return &driver{127 logfile: lf,128 readers: make(map[*logger.LogWatcher]struct{}),129 }, nil...
multipart.go
Source:multipart.go
2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4package icmp5import "golang.org/x/net/internal/iana"6// multipartMessageBodyDataLen takes b as an original datagram and7// exts as extensions, and returns a required length for message body8// and a required length for a padded original datagram in wire9// format.10func multipartMessageBodyDataLen(proto int, withOrigDgram bool, b []byte, exts []Extension) (bodyLen, dataLen int) {11 bodyLen = 4 // length of leading octets12 var extLen int13 var rawExt bool // raw extension may contain an empty object14 for _, ext := range exts {15 extLen += ext.Len(proto)16 if _, ok := ext.(*RawExtension); ok {17 rawExt = true18 }19 }20 if extLen > 0 && withOrigDgram {21 dataLen = multipartMessageOrigDatagramLen(proto, b)22 } else {23 dataLen = len(b)24 }25 if extLen > 0 || rawExt {26 bodyLen += 4 // length of extension header27 }28 bodyLen += dataLen + extLen29 return bodyLen, dataLen30}31// multipartMessageOrigDatagramLen takes b as an original datagram,32// and returns a required length for a padded orignal datagram in wire33// format.34func multipartMessageOrigDatagramLen(proto int, b []byte) int {35 roundup := func(b []byte, align int) int {36 // According to RFC 4884, the padded original datagram37 // field must contain at least 128 octets.38 if len(b) < 128 {39 return 12840 }41 r := len(b)42 return (r + align - 1) &^ (align - 1)43 }44 switch proto {45 case iana.ProtocolICMP:46 return roundup(b, 4)47 case iana.ProtocolIPv6ICMP:48 return roundup(b, 8)49 default:50 return len(b)51 }52}53// marshalMultipartMessageBody takes data as an original datagram and54// exts as extesnsions, and returns a binary encoding of message body.55// It can be used for non-multipart message bodies when exts is nil.56func marshalMultipartMessageBody(proto int, withOrigDgram bool, data []byte, exts []Extension) ([]byte, error) {57 bodyLen, dataLen := multipartMessageBodyDataLen(proto, withOrigDgram, data, exts)58 b := make([]byte, bodyLen)59 copy(b[4:], data)60 if len(exts) > 0 {61 b[4+dataLen] = byte(extensionVersion << 4)62 off := 4 + dataLen + 4 // leading octets, data, extension header63 for _, ext := range exts {64 switch ext := ext.(type) {65 case *MPLSLabelStack:66 if err := ext.marshal(proto, b[off:]); err != nil {67 return nil, err68 }69 off += ext.Len(proto)70 case *InterfaceInfo:71 attrs, l := ext.attrsAndLen(proto)72 if err := ext.marshal(proto, b[off:], attrs, l); err != nil {73 return nil, err74 }75 off += ext.Len(proto)76 case *InterfaceIdent:77 if err := ext.marshal(proto, b[off:]); err != nil {78 return nil, err79 }80 off += ext.Len(proto)81 case *RawExtension:82 copy(b[off:], ext.Data)83 off += ext.Len(proto)84 }85 }86 s := checksum(b[4+dataLen:])87 b[4+dataLen+2] ^= byte(s)88 b[4+dataLen+3] ^= byte(s >> 8)89 if withOrigDgram {90 switch proto {91 case iana.ProtocolICMP:92 b[1] = byte(dataLen / 4)93 case iana.ProtocolIPv6ICMP:94 b[0] = byte(dataLen / 8)95 }96 }97 }98 return b, nil99}100// parseMultipartMessageBody parses b as either a non-multipart101// message body or a multipart message body.102func parseMultipartMessageBody(proto int, typ Type, b []byte) ([]byte, []Extension, error) {103 var l int104 switch proto {105 case iana.ProtocolICMP:106 l = 4 * int(b[1])107 case iana.ProtocolIPv6ICMP:108 l = 8 * int(b[0])...
Len
Using AI Code Generation
1import (2func main() {3 t := timestamp.Timestamp{4 }5 fmt.Println(t.Len())6}
Len
Using AI Code Generation
1import (2func main() {3 p := &Person{4 }5 fmt.Println(proto.Size(p))6}
Len
Using AI Code Generation
1import (2func main() {3 p := &Person{4 }5 out, err := proto.Marshal(p)6 if err != nil {7 fmt.Println(err)8 }9 newP := &Person{}10 err = proto.Unmarshal(out, newP)11 if err != nil {12 fmt.Println(err)13 }14 fmt.Println(newP)15 fmt.Println("Length of the data in bytes is:", proto.Size(p))16}
Len
Using AI Code Generation
1func (m *Message) Len() int {2 return len(m.Body)3}4func (m *Message) Get(index int) int {5 return int(m.Body[index])6}7func (m *Message) Set(index, value int) {8 m.Body[index] = byte(value)9}10func (m *Message) Append(value int) {11 m.Body = append(m.Body, byte(value))12}13func (m *Message) Insert(index, value int) {14 m.Body = append(m.Body, 0)15 copy(m.Body[index+1:], m.Body[index:])16 m.Body[index] = byte(value)17}18func (m *Message) Delete(index int) {19 copy(m.Body[index:], m.Body[index+1:])20 m.Body = m.Body[:len(m.Body)-1]21}22func (m *Message) Clear() {23}24func (m *Message) Trim() {25 m.Body = m.Body[:cap(m.Body)]26}27func (m *Message) Reverse() {28 for i, j := 0, len(m.Body)-1; i < j; i, j = i+1, j-1 {29 }30}31func (m *Message) Pop() int {32 n := len(m.Body)33 return int(x)34}35func (m *Message) Push(x int) {36 m.Body = append(m.Body, byte(x))37}
Len
Using AI Code Generation
1import (2func main() {3 p := proto3.Person{4 }5 fmt.Println("Length of the message:", proto.Size(&p))6}
Len
Using AI Code Generation
1import (2func main() {3 p := &Person{4 }5 fmt.Println(proto.Size(p))6}
Len
Using AI Code Generation
1import (2func main() {3 message := &Message{}4 fmt.Println(message)5 fmt.Println(proto.Size(message))6}
Len
Using AI Code Generation
1import (2func main() {3 person := &proto3.Person{4 }5 data, err := proto.Marshal(person)6 if err != nil {7 fmt.Println("Marshaling error: ", err)8 }9 fmt.Println("Length of the serialized message: ", len(data))10}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!