add io.ReadWriteCloser ontop of transport.Socket

This commit is contained in:
Asim Aslam 2019-08-07 15:02:00 +01:00
parent edb0fe4b16
commit 380d9790e6

40
util/io/io.go Normal file
View File

@ -0,0 +1,40 @@
// Package io is for io management
package io
import (
"io"
"github.com/micro/go-micro/transport"
)
type rwc struct {
socket transport.Socket
}
func (r *rwc) Read(p []byte) (n int, err error) {
m := new(transport.Message)
if err := r.socket.Recv(m); err != nil {
return 0, err
}
copy(p, m.Body)
return len(m.Body), nil
}
func (r *rwc) Write(p []byte) (n int, err error) {
err = r.socket.Send(&transport.Message{
Body: p,
})
if err != nil {
return 0, err
}
return len(p), nil
}
func (r *rwc) Close() error {
return r.socket.Close()
}
// NewRWC returns a new ReadWriteCloser
func NewRWC(sock transport.Socket) io.ReadWriteCloser {
return &rwc{sock}
}