-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
83 lines (64 loc) · 1.67 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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package mitosis
import (
"bytes"
"errors"
"fmt"
"net"
"strconv"
)
const (
defaultPort = 32000 // First port at which we start looking for available sockets.
protocol = "tcp" // Transport protocol being used.
)
var magick = []byte("MTSS") // Protocol header, identifying a valid mitosis session.
// spawnServer opens the sever's listening socket.
func spawnServer(state *State, done chan<- bool) (uint, error) {
var listener net.Listener
var port uint
var err error
port = defaultPort
// open a port and get the port number
listener, err = net.Listen(protocol, ":0")
if err != nil {
return 0, errors.New("Failed to open a listen port")
}
laddr := listener.Addr().String()
_, portstr, err := net.SplitHostPort(laddr)
if err != nil {
return 0, fmt.Errorf("Failed to get port from listen port address string: %s", laddr)
}
n, err := strconv.ParseUint(portstr, 10, 16)
if err != nil {
return 0, fmt.Errorf("Bad port number string: %s", portstr)
}
port = uint(n)
go func() {
defer listener.Close()
for {
client, err := listener.Accept()
if err != nil {
return
}
handleClient(client, state, done)
}
}()
return port, nil
}
// handleClient handles an incoming client connection.
func handleClient(client net.Conn, state *State, done chan<- bool) {
defer func() {
client.Close()
recover()
}()
// Make sure the protocol header is there and valid.
hdr := readRaw(client, uint32(len(magick)))
if !bytes.Equal(hdr, magick) {
return
}
// Send pending state information.
state.write(client)
// Signal completion.
done <- true
}