gecko/vms/rpcchainvm/ghttp/gconn/conn_server.go

81 lines
2.2 KiB
Go
Raw Normal View History

2020-04-16 23:03:17 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gconn
import (
"context"
"net"
"time"
2020-04-17 06:47:19 -07:00
"github.com/ava-labs/gecko/vms/rpcchainvm/ghttp/gconn/gconnproto"
2020-04-16 23:03:17 -07:00
)
// Server is a http.Handler that is managed over RPC.
type Server struct{ conn net.Conn }
// NewServer returns a http.Handler instance manage remotely
func NewServer(conn net.Conn) *Server {
return &Server{conn: conn}
}
// Read ...
2020-04-17 06:47:19 -07:00
func (s *Server) Read(ctx context.Context, req *gconnproto.ReadRequest) (*gconnproto.ReadResponse, error) {
2020-04-16 23:03:17 -07:00
buf := make([]byte, int(req.Length))
n, err := s.conn.Read(buf)
2020-04-17 06:47:19 -07:00
resp := &gconnproto.ReadResponse{
2020-04-16 23:03:17 -07:00
Read: buf[:n],
}
if err != nil {
resp.Errored = true
resp.Error = err.Error()
}
return resp, nil
}
// Write ...
2020-04-17 06:47:19 -07:00
func (s *Server) Write(ctx context.Context, req *gconnproto.WriteRequest) (*gconnproto.WriteResponse, error) {
2020-04-16 23:03:17 -07:00
n, err := s.conn.Write(req.Payload)
if err != nil {
return nil, err
}
2020-04-17 06:47:19 -07:00
return &gconnproto.WriteResponse{
2020-04-16 23:03:17 -07:00
Length: int32(n),
}, nil
}
// Close ...
2020-04-17 06:47:19 -07:00
func (s *Server) Close(ctx context.Context, req *gconnproto.CloseRequest) (*gconnproto.CloseResponse, error) {
return &gconnproto.CloseResponse{}, s.conn.Close()
2020-04-16 23:03:17 -07:00
}
// SetDeadline ...
2020-04-17 06:47:19 -07:00
func (s *Server) SetDeadline(ctx context.Context, req *gconnproto.SetDeadlineRequest) (*gconnproto.SetDeadlineResponse, error) {
2020-04-16 23:03:17 -07:00
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
2020-04-17 06:47:19 -07:00
return &gconnproto.SetDeadlineResponse{}, s.conn.SetDeadline(deadline)
2020-04-16 23:03:17 -07:00
}
// SetReadDeadline ...
2020-04-17 06:47:19 -07:00
func (s *Server) SetReadDeadline(ctx context.Context, req *gconnproto.SetReadDeadlineRequest) (*gconnproto.SetReadDeadlineResponse, error) {
2020-04-16 23:03:17 -07:00
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
2020-04-17 06:47:19 -07:00
return &gconnproto.SetReadDeadlineResponse{}, s.conn.SetReadDeadline(deadline)
2020-04-16 23:03:17 -07:00
}
// SetWriteDeadline ...
2020-04-17 06:47:19 -07:00
func (s *Server) SetWriteDeadline(ctx context.Context, req *gconnproto.SetWriteDeadlineRequest) (*gconnproto.SetWriteDeadlineResponse, error) {
2020-04-16 23:03:17 -07:00
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
2020-04-17 06:47:19 -07:00
return &gconnproto.SetWriteDeadlineResponse{}, s.conn.SetWriteDeadline(deadline)
2020-04-16 23:03:17 -07:00
}