forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
147 lines (125 loc) · 2.81 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
package main
import (
"fmt"
"net"
"net/rpc"
"os"
"runtime"
)
func do_server() int {
g_config.read()
addr := *g_addr
if *g_sock == "unix" {
addr = get_socket_filename()
if file_exists(addr) {
fmt.Printf("unix socket: '%s' already exists\n", addr)
return 1
}
}
g_daemon = new_daemon(*g_sock, addr)
if *g_sock == "unix" {
// cleanup unix socket file
defer os.Remove(addr)
}
rpc.Register(new(RPC))
g_daemon.loop()
return 0
}
//-------------------------------------------------------------------------
// daemon
//-------------------------------------------------------------------------
type daemon struct {
listener net.Listener
cmd_in chan int
autocomplete *auto_complete_context
pkgcache package_cache
declcache *decl_cache
}
func new_daemon(network, address string) *daemon {
var err error
d := new(daemon)
d.listener, err = net.Listen(network, address)
if err != nil {
panic(err)
}
d.cmd_in = make(chan int, 1)
d.pkgcache = new_package_cache()
d.declcache = new_decl_cache()
d.autocomplete = new_auto_complete_context(d.pkgcache, d.declcache)
return d
}
func (this *daemon) drop_cache() {
this.pkgcache = new_package_cache()
this.declcache = new_decl_cache()
this.autocomplete = new_auto_complete_context(this.pkgcache, this.declcache)
}
const (
daemon_close = iota
)
func (this *daemon) loop() {
conn_in := make(chan net.Conn)
go func() {
for {
c, err := this.listener.Accept()
if err != nil {
panic(err)
}
conn_in <- c
}
}()
for {
// handle connections or server CMDs (currently one CMD)
select {
case c := <-conn_in:
rpc.ServeConn(c)
runtime.GC()
case cmd := <-this.cmd_in:
switch cmd {
case daemon_close:
return
}
}
}
}
func (this *daemon) close() {
this.cmd_in <- daemon_close
}
var g_daemon *daemon
//-------------------------------------------------------------------------
// server_* functions
//
// Corresponding client_* functions are autogenerated by goremote.
//-------------------------------------------------------------------------
func server_auto_complete(file []byte, filename string, cursor int) (c []candidate, d int) {
defer func() {
if err := recover(); err != nil {
print_backtrace(err)
c = []candidate{
{"PANIC", "PANIC", decl_invalid},
}
// drop cache
g_daemon.drop_cache()
}
}()
return g_daemon.autocomplete.apropos(file, filename, cursor)
}
func server_close(notused int) int {
g_daemon.close()
return 0
}
func server_status(notused int) string {
return g_daemon.autocomplete.status()
}
func server_drop_cache(notused int) int {
// drop cache
g_daemon.drop_cache()
return 0
}
func server_set(key, value string) string {
if key == "\x00" {
return g_config.list()
} else if value == "\x00" {
return g_config.list_option(key)
}
return g_config.set_option(key, value)
}