-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.go
176 lines (157 loc) · 4.16 KB
/
streamer.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
170
171
172
173
174
175
176
package main
import (
"flag"
"fmt"
"log"
"net"
"runtime"
"strings"
"time"
)
func main() {
incomingPort := flag.String("incoming-port", "33333", "The server listens for an incoming stream on a port defined by a command line argument `incoming-port`. Any packet received on this port is immediately sent to any connected client.")
outgoingPort := flag.String("outgoing-port", "44444", "The server listens for client connections on a port defined by a command line argument `outgoing-port`.")
flag.Parse()
log.Println("incoming-port", *incomingPort)
log.Println("outgoing-port", *outgoingPort)
output := make(chan string)
RunStreamer(*incomingPort, *outgoingPort, output)
for {
log.Println(<-output)
}
}
type (
Id string
Client struct {
Id Id
Addr *net.UDPAddr
LastAliveTime time.Time
}
Data struct {
Bytes []byte
N int
Addr *net.UDPAddr
}
)
var (
Clients = make(map[Id]*Client)
DISCONNECT_TIMEOUT time.Duration = 30 * time.Second
)
func RunStreamer(incomingPort, outgoingPort string, output chan string) {
go incomingServer(incomingPort, output)
go outgoingServer(outgoingPort, output)
}
func outgoingServer(outgoingPort string, logger chan string) {
dataChan := make(chan *Data)
go listen(outgoingPort, logger, dataChan)
for {
data := <-dataChan
outgoingClientMsg := data.String()
msgParts := strings.Split(outgoingClientMsg, " ")
const allowedWordsInMsg = 2
if len(msgParts) != allowedWordsInMsg {
continue
}
command := msgParts[0]
id := Id(msgParts[1])
switch command {
case "ALIVE":
if Clients[id] == nil {
continue
}
client := Clients[id]
client.LastAliveTime = time.Now()
go client.checkTimoutLater(logger)
break
case "CONNECT":
client := &Client{
Id: id,
Addr: data.Addr,
LastAliveTime: time.Now(),
}
Clients[id] = client
go client.checkTimoutLater(logger)
logger <- fmt.Sprintf("client with id %s connected", id)
break
case "DISCONNECT":
delete(Clients, id)
logger <- fmt.Sprintf("client with id %s disconnected", id)
break
}
}
}
func (client *Client) checkTimoutLater(output chan string) {
now := time.Now()
time.Sleep(DISCONNECT_TIMEOUT)
if Clients[client.Id] == nil {
return
}
if client.LastAliveTime.Before(now) {
delete(Clients, client.Id)
msg := fmt.Sprint("client ", client.Id, " was removed because he didn't send ALIVE command more than ", DISCONNECT_TIMEOUT)
output <- msg
}
}
func (d *Data) String() string {
return string(d.Bytes[:d.N])
}
func listen(port string, logger chan string, received chan *Data) {
ServerAddr, err := net.ResolveUDPAddr("udp", ":"+port)
CheckError(err)
/* Now listen at selected port */
ServerConn, err := net.ListenUDP("udp", ServerAddr)
CheckError(err)
defer ServerConn.Close()
buf := make([]byte, 1024)
logger <- fmt.Sprint("udp port is listening: ", port)
for {
n, addr, err := ServerConn.ReadFromUDP(buf)
CheckError(err)
received <- &Data{
Bytes: buf[:n],
N: n,
Addr: addr,
}
}
}
func incomingServer(incomingPort string, logger chan string) {
incomingPackages := make(chan *Data)
go listen(incomingPort, logger, incomingPackages)
for {
buf := <-incomingPackages
for _, client := range Clients {
// send via any free port
sendTo("0", client, buf)
}
}
}
func sendTo(outgoingPort string, client *Client, data *Data) {
send(outgoingPort, client.Addr, data)
}
func send(outgoingPort string, targetAddr *net.UDPAddr, data *Data) {
sendBytes(outgoingPort, targetAddr, data.Bytes)
}
func sendBytes(outgoingPort string, remoteAddr *net.UDPAddr, buf []byte) {
localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+outgoingPort)
CheckError(err)
Conn, err := net.DialUDP("udp", localAddr, remoteAddr)
CheckError(err)
defer Conn.Close()
_, err = Conn.Write(buf)
CheckError(err)
}
func sendStr(outgoingPort string, targetAddr *net.UDPAddr, msg string) {
sendBytes(outgoingPort, targetAddr, []byte(msg))
}
func CheckError(err error) {
if err != nil {
printStack()
log.Fatal("Error: ", err)
}
}
func printStack() {
b := make([]byte, 2048)
n := runtime.Stack(b, false)
stack := string(b[:n])
log.Println(stack)
}