From 380d9790e603530aae4b5d7f6286ecce7e415206 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 7 Aug 2019 15:02:00 +0100 Subject: [PATCH] add io.ReadWriteCloser ontop of transport.Socket --- util/io/io.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 util/io/io.go diff --git a/util/io/io.go b/util/io/io.go new file mode 100644 index 00000000..133ba6b8 --- /dev/null +++ b/util/io/io.go @@ -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} +}