-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
129 lines (114 loc) · 2.34 KB
/
host.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
package main
import (
"context"
"fmt"
"net/http"
"sync"
)
type HostServer struct {
id string
ctx context.Context
cancel context.CancelFunc
connMux sync.RWMutex
conns map[int64]*Conn
}
func NewHostServer(ctx context.Context, id string)(s *HostServer){
ctx0, cancel := context.WithCancel(ctx)
return &HostServer{
ctx: ctx0,
cancel: cancel,
id: id,
conns: make(map[int64]*Conn),
}
}
func (s *HostServer)Id()(string){
return s.id
}
func (s *HostServer)Destroy(){
s.cancel()
s.connMux.Lock()
for _, c := range s.conns {
c.Close()
}
s.conns = nil
s.connMux.Unlock()
}
func (s *HostServer)AcceptConn(rw http.ResponseWriter, req *http.Request)(conn *Conn, err error){
var ccId int64
if ccId, err = readCCID(req); err != nil {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprint(rw, err.Error())
}
s.connMux.Lock()
if _, ok := s.conns[ccId]; ok {
s.connMux.Unlock()
err = fmt.Errorf("Device ID %d is already connected", ccId)
rw.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(rw, err.Error())
return
}
s.conns[ccId] = nil // take the slot first
s.connMux.Unlock()
conn, err = AcceptConn(s, rw, req)
s.connMux.Lock()
s.conns[conn.id] = conn
s.connMux.Unlock()
go func(){
select {
case <-conn.Context().Done():
s.connMux.Lock()
delete(s.conns, conn.id)
s.connMux.Unlock()
case <-s.ctx.Done():
}
}()
go conn.Run("shell")
return
}
func (s *HostServer)GetConn(id int64)(*Conn){
s.connMux.RLock()
defer s.connMux.RUnlock()
return s.conns[id]
}
func (s *HostServer)GetConns()(conns []*Conn){
s.connMux.RLock()
defer s.connMux.RUnlock()
conns = make([]*Conn, 0, len(s.conns))
for _, c := range s.conns {
conns = append(conns, c)
}
return
}
func (s *HostServer)GetConnCount()(n int){
s.connMux.RLock()
defer s.connMux.RUnlock()
return len(s.conns)
}
func (s *HostServer)Broadcast(data Map)(n int, res <-chan error){
s.connMux.RLock()
defer s.connMux.RUnlock()
res0 := make(chan error, len(s.conns))
for _, c := range s.conns {
n++
go func(){
res0 <- c.send(data)
}()
}
res = res0
return
}
func (s *HostServer)broadcastExcept(data Map, except *Conn)(n int, res <-chan error){
s.connMux.RLock()
defer s.connMux.RUnlock()
res0 := make(chan error, len(s.conns))
for _, c := range s.conns {
if c != except {
n++
go func(){
res0 <- c.send(data)
}()
}
}
res = res0
return
}