forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
169 lines (145 loc) · 3.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"net"
"rpc"
"os"
"os/signal"
"fmt"
"runtime"
"sync"
)
//-------------------------------------------------------------------------
// Daemon
//-------------------------------------------------------------------------
type Daemon struct {
acr *Server
acc *AutoCompleteContext
pcache PackageCache
declcache *DeclCache
}
func NewDaemon(network, address string) *Daemon {
d := new(Daemon)
d.acr = NewServer(network, address)
d.pcache = NewPackageCache()
d.declcache = NewDeclCache()
d.acc = NewAutoCompleteContext(d.pcache, d.declcache)
return d
}
func (d *Daemon) DropCache() {
d.pcache = NewPackageCache()
d.declcache = NewDeclCache()
d.acc = NewAutoCompleteContext(d.pcache, d.declcache)
}
var daemon *Daemon
//-------------------------------------------------------------------------
// printBacktrace
//-------------------------------------------------------------------------
var btSync sync.Mutex
func printBacktrace(err interface{}) {
btSync.Lock()
defer btSync.Unlock()
fmt.Printf("panic: %v\n", err)
i := 2
for {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
f := runtime.FuncForPC(pc)
fmt.Printf("%d(%s): %s:%d\n", i-1, f.Name(), file, line)
i++
}
fmt.Println("")
}
//-------------------------------------------------------------------------
// Server_* functions
//
// Corresponding Client_* functions are autogenerated by goremote.
//-------------------------------------------------------------------------
func Server_AutoComplete(file []byte, filename string, cursor int) (a, b, c []string, d int) {
defer func() {
if err := recover(); err != nil {
printBacktrace(err)
a = []string{"PANIC"}
b = a
c = a
// drop cache
daemon.DropCache()
}
}()
a, b, c, d = daemon.acc.Apropos(file, filename, cursor)
return
}
func Server_Close(notused int) int {
daemon.acr.Close()
return 0
}
func Server_Status(notused int) string {
return daemon.acc.Status()
}
func Server_DropCache(notused int) int {
// drop cache
daemon.DropCache()
return 0
}
func Server_Set(key, value string) string {
if key == "" {
return listConfig(&Config)
} else if value == "" {
return listOption(&Config, key)
}
return setOption(&Config, key, value)
}
//-------------------------------------------------------------------------
// Server
//-------------------------------------------------------------------------
const (
SERVER_CLOSE = iota
)
type Server struct {
listener net.Listener
cmd_in chan int
}
func NewServer(network, address string) *Server {
var err os.Error
s := new(Server)
s.listener, err = net.Listen(network, address)
if err != nil {
panic(err.String())
}
s.cmd_in = make(chan int, 1)
return s
}
func acceptConnections(in chan net.Conn, listener net.Listener) {
for {
c, err := listener.Accept()
if err != nil {
panic(err.String())
}
in <- c
}
}
func (s *Server) Loop() {
conn_in := make(chan net.Conn)
go acceptConnections(conn_in, s.listener)
for {
// handle connections or server CMDs (currently one CMD)
select {
case c := <-conn_in:
rpc.ServeConn(c)
runtime.GC()
case cmd := <-s.cmd_in:
switch cmd {
case SERVER_CLOSE:
return
}
case sig := <-signal.Incoming:
if IsTerminationSignal(sig) {
return
}
}
}
}
func (s *Server) Close() {
s.cmd_in <- SERVER_CLOSE
}