50 lines
1,022 B
Go
50 lines
1,022 B
Go
|
package govmtools
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/binary"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
var order = binary.BigEndian
|
||
|
|
||
|
func (s *Socket) RpcSend(data []byte) ([]byte, error) {
|
||
|
// TODO error handling
|
||
|
buf := bytes.NewBuffer(nil)
|
||
|
|
||
|
binary.Write(buf, order, FieldInt64)
|
||
|
binary.Write(buf, order, FieldIDType)
|
||
|
binary.Write(buf, order, PacketTypeData)
|
||
|
|
||
|
binary.Write(buf, order, FieldEmpty)
|
||
|
|
||
|
binary.Write(buf, order, FieldString)
|
||
|
binary.Write(buf, order, FieldIDPayload)
|
||
|
binary.Write(buf, order, uint32(len(data)))
|
||
|
|
||
|
buf.Write(data)
|
||
|
|
||
|
res := make([]byte, buf.Len()+4)
|
||
|
order.PutUint32(res, uint32(buf.Len()))
|
||
|
copy(res[4:], buf.Bytes())
|
||
|
|
||
|
_, err := s.conn.Write(res)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("write: %w", err)
|
||
|
}
|
||
|
|
||
|
responseLength := make([]byte, 4)
|
||
|
_, err = s.conn.Read(responseLength)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("read len: %w", err)
|
||
|
}
|
||
|
|
||
|
response := make([]byte, order.Uint32(responseLength))
|
||
|
_, err = s.conn.Read(response)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("read: %w", err)
|
||
|
}
|
||
|
|
||
|
return response, nil
|
||
|
}
|