You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Modified from original GoGo Protobuf to return number of bytes written.
  30. package protoio
  31. import (
  32. "bytes"
  33. "encoding/binary"
  34. "io"
  35. "github.com/gogo/protobuf/proto"
  36. )
  37. // NewDelimitedWriter writes a varint-delimited Protobuf message to a writer. It is
  38. // equivalent to the gogoproto NewDelimitedWriter, except WriteMsg() also returns the
  39. // number of bytes written, which is necessary in the p2p package.
  40. func NewDelimitedWriter(w io.Writer) WriteCloser {
  41. return &varintWriter{w, make([]byte, binary.MaxVarintLen64), nil}
  42. }
  43. type varintWriter struct {
  44. w io.Writer
  45. lenBuf []byte
  46. buffer []byte
  47. }
  48. func (w *varintWriter) WriteMsg(msg proto.Message) (int, error) {
  49. if m, ok := msg.(marshaler); ok {
  50. n, ok := getSize(m)
  51. if ok {
  52. if n+binary.MaxVarintLen64 >= len(w.buffer) {
  53. w.buffer = make([]byte, n+binary.MaxVarintLen64)
  54. }
  55. lenOff := binary.PutUvarint(w.buffer, uint64(n))
  56. _, err := m.MarshalTo(w.buffer[lenOff:])
  57. if err != nil {
  58. return 0, err
  59. }
  60. _, err = w.w.Write(w.buffer[:lenOff+n])
  61. return lenOff + n, err
  62. }
  63. }
  64. // fallback
  65. data, err := proto.Marshal(msg)
  66. if err != nil {
  67. return 0, err
  68. }
  69. length := uint64(len(data))
  70. n := binary.PutUvarint(w.lenBuf, length)
  71. _, err = w.w.Write(w.lenBuf[:n])
  72. if err != nil {
  73. return 0, err
  74. }
  75. _, err = w.w.Write(data)
  76. return len(data) + n, err
  77. }
  78. func (w *varintWriter) Close() error {
  79. if closer, ok := w.w.(io.Closer); ok {
  80. return closer.Close()
  81. }
  82. return nil
  83. }
  84. func MarshalDelimited(msg proto.Message) ([]byte, error) {
  85. var buf bytes.Buffer
  86. _, err := NewDelimitedWriter(&buf).WriteMsg(msg)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return buf.Bytes(), nil
  91. }