-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
96 lines (80 loc) · 1.87 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
package vim
import (
"context"
"io"
"net/rpc"
"sync"
"time"
dlvconfig "github.com/go-delve/delve/pkg/config"
commonrpc "github.com/ovandriyanov/dlvim/proxy/rpc"
"github.com/ovandriyanov/dlvim/proxy/upstream"
"github.com/ovandriyanov/dlvim/proxy/vimevent"
"golang.org/x/xerrors"
)
type Server struct {
events chan vimevent.Event
mutex sync.Mutex
inventory *inventory
debugRPC bool
dlvConfig *dlvconfig.Config
}
func (s *Server) Stop() {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.inventory == nil {
return
}
s.inventory.Stop()
}
func (s *Server) UpstreamClient() commonrpc.Client {
if s.inventory == nil {
return nil
}
return s.inventory.upstreamClient
}
func (s *Server) HandleClient(ctx context.Context, clientConn io.ReadWriteCloser) {
defer clientConn.Close()
rpcDone := make(chan struct{})
srv := rpc.NewServer()
rpcHandler := NewRPCHandler(s, ctx)
var receiver interface{} = rpcHandler
if s.debugRPC {
receiver = NewLoggingRPCHandler(rpcHandler, "vim server")
}
srv.RegisterName(ServiceName, receiver)
go func() {
srv.ServeCodec(NewRPCCodec(clientConn))
rpcDone <- struct{}{}
}()
select {
case <-rpcDone:
return
case <-ctx.Done():
_ = clientConn.Close()
<-rpcDone
return
}
}
func (s *Server) Initialize(startOption upstream.StartOption) (inventory *inventory, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.inventory != nil {
return nil, xerrors.New("already initialized")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
s.inventory, err = NewInventory(ctx, startOption, s.events, s.debugRPC)
if err != nil {
return nil, err
}
s.dlvConfig = dlvconfig.LoadConfig()
return s.inventory, nil
}
func NewServer(debugRPC bool) *Server {
return &Server{
events: make(chan vimevent.Event),
mutex: sync.Mutex{},
inventory: nil,
debugRPC: debugRPC,
}
}