github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/server_conn.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package nphttp2 18 19 import ( 20 "encoding/binary" 21 "io" 22 "net" 23 "time" 24 25 "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/codes" 26 "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/grpc" 27 "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/status" 28 "github.com/cloudwego/kitex/pkg/streaming" 29 ) 30 31 type serverConn struct { 32 tr grpc.ServerTransport 33 s *grpc.Stream 34 } 35 36 var _ GRPCConn = (*serverConn)(nil) 37 38 func newServerConn(tr grpc.ServerTransport, s *grpc.Stream) *serverConn { 39 return &serverConn{ 40 tr: tr, 41 s: s, 42 } 43 } 44 45 func (c *serverConn) ReadFrame() (hdr, data []byte, err error) { 46 hdr = make([]byte, 5) 47 _, err = c.Read(hdr) 48 if err != nil { 49 return nil, nil, err 50 } 51 dLen := int(binary.BigEndian.Uint32(hdr[1:])) 52 data = make([]byte, dLen) 53 _, err = c.Read(data) 54 if err != nil { 55 return nil, nil, err 56 } 57 return hdr, data, nil 58 } 59 60 func GetServerConn(st streaming.Stream) (GRPCConn, error) { 61 serverStream, ok := st.(*stream) 62 if !ok { 63 // err! 64 return nil, status.Errorf(codes.Internal, "failed to get server conn from stream.") 65 } 66 grpcServerConn, ok := serverStream.conn.(GRPCConn) 67 if !ok { 68 // err! 69 return nil, status.Errorf(codes.Internal, "failed to trans conn to grpc conn.") 70 } 71 return grpcServerConn, nil 72 } 73 74 // impl net.Conn 75 func (c *serverConn) Read(b []byte) (n int, err error) { 76 n, err = c.s.Read(b) 77 return n, err 78 } 79 80 func (c *serverConn) Write(b []byte) (n int, err error) { 81 if len(b) < 5 { 82 return 0, io.ErrShortWrite 83 } 84 return c.WriteFrame(b[:5], b[5:]) 85 } 86 87 func (c *serverConn) WriteFrame(hdr, data []byte) (n int, err error) { 88 // server sets the END_STREAM flag in trailer when writeStatus 89 err = c.tr.Write(c.s, hdr, data, &grpc.Options{}) 90 return len(hdr) + len(data), err 91 } 92 93 func (c *serverConn) LocalAddr() net.Addr { return c.tr.LocalAddr() } 94 func (c *serverConn) RemoteAddr() net.Addr { return c.tr.RemoteAddr() } 95 func (c *serverConn) SetDeadline(t time.Time) error { return nil } 96 func (c *serverConn) SetReadDeadline(t time.Time) error { return nil } 97 func (c *serverConn) SetWriteDeadline(t time.Time) error { return nil } 98 func (c *serverConn) Close() error { 99 return c.tr.WriteStatus(c.s, status.New(codes.OK, "")) 100 }