-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
132 lines (115 loc) · 2.84 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"learn_GeeRPC/codec"
"log"
"net"
"reflect"
"sync"
)
const MagicNumber = 0x3bef5c
type Option struct {
MagicNumber int
CodecType codec.Type
}
var DefaultOption = &Option{
MagicNumber: MagicNumber,
CodecType: codec.GobType,
}
type Server struct{}
func NewServer() *Server {
return &Server{}
}
var DefaultServer = NewServer()
func (server *Server) Accept(lis net.Listener) {
for {
conn, err := lis.Accept()
if err != nil {
log.Println("rpc server : accept err:", err)
}
go server.ServeConn(conn)
}
}
func Accept(lis net.Listener) {
DefaultServer.Accept(lis)
}
func (server *Server) ServeConn(conn io.ReadWriteCloser) {
defer func() { _ = conn.Close() }()
var opt Option
if err := json.NewDecoder(conn).Decode(&opt); err != nil {
log.Println("rpc server : options error:", err)
return
}
if opt.MagicNumber != MagicNumber {
log.Printf("rpc server : invalid magicnumber %x", opt.MagicNumber)
return
}
f := codec.NewCodecFuncMap[opt.CodecType]
if f == nil {
log.Printf("rpc server : invalid codec type %s", opt.CodecType)
return
}
server.serveCodec(f(conn))
}
var invalidRequest = struct{}{} //零大小结构体
func (server *Server) serveCodec(cc codec.Codec) {
sending := new(sync.Mutex)
wg := new(sync.WaitGroup)
for {
req, err := server.readRequest(cc)
if err != nil {
if req == nil {
break
}
req.h.Error = err.Error()
server.sendResponse(cc, req.h, invalidRequest, sending)
continue
}
wg.Add(1)
go server.handleRequest(cc, req, sending, wg)
}
wg.Wait()
_ = cc.Close()
}
type request struct {
h *codec.Header
argv, reply reflect.Value
}
func (server *Server) readRequestHeader(cc codec.Codec) (*codec.Header, error) {
var h codec.Header
if err := cc.ReadHeader(&h); err != nil {
if err != io.EOF && !errors.Is(err, io.ErrUnexpectedEOF) {
log.Println("rpc server : read header err:", err)
}
return nil, err
}
return &h, nil
}
func (server *Server) readRequest(cc codec.Codec) (*request, error) {
h, err := server.readRequestHeader(cc)
if err != nil {
return nil, err
}
req := &request{h: h}
req.argv = reflect.New(reflect.TypeOf(""))
if err := cc.ReadBody(req.argv.Interface()); err != nil {
log.Println("rpc server : read argv err:", err)
}
return req, nil
}
func (server *Server) sendResponse(cc codec.Codec, h *codec.Header, body interface{}, sending *sync.Mutex) {
sending.Lock()
defer sending.Unlock()
if err := cc.Write(h, body); err != nil {
log.Println("rpc server : send response err:", err)
}
}
func (server *Server) handleRequest(cc codec.Codec, req *request, sending *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
log.Println(req.h, req.argv.Elem())
req.reply = reflect.ValueOf(fmt.Sprintf("geerpc resp %d", req.h.Seq))
server.sendResponse(cc, req.h, req.reply.Interface(), sending)
}