-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
113 lines (94 loc) · 2.82 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
package main
import (
"net"
"fmt"
"os"
)
// Constants
const(
PORT_A string = "7777" // port for single client A
PORT_B string = "8888" // port for multiple clients B
PACKET_SIZE int = 1024 // packets size sent and received
)
func create_connection(ip, port string) *net.UDPConn{
// UDPAddr used to create listener
udpaddr,_ := net.ResolveUDPAddr("udp", ip + ":" + port)
// UDP listeners one on port A and one on Port B.
conn, err := net.ListenUDP("udp", udpaddr)
if err != nil{
fmt.Println("listener ", err)
os.Exit(0)
}
return conn
}
func receive_bytes(conn_sender *net.UDPConn, messages chan []byte){
// Variables
var client_A *net.UDPAddr
buffer := make([]byte, PACKET_SIZE) // buffer will receive the data from A
// Loop waiting for data from client A and sending to all clients B
for{
n,raddr,err1 := conn_sender.ReadFromUDP(buffer) // Gets data from client A
if err1 != nil{
fmt.Println(err1)
continue
}
// If not from same client then refuse
if client_A != nil && raddr.String() != client_A.String(){
continue
}
if client_A == nil{
client_A = raddr
fmt.Println(raddr.String(), "connected (A)")
}
messages <- buffer[0:n]
//fmt.Println(string(buffer[0:n]))
}
}
func send_to_B(conn_receiver *net.UDPConn, clients_B map[string]*net.UDPAddr, messages chan []byte){
for{
msg := <-messages
// Loop send data from client A to all clients B
i := 0
for key, client := range clients_B{
_,err2 := conn_receiver.WriteToUDP(msg, client)
if err2 != nil{
fmt.Println(key, err2)
}
i++
}
}
}
func main(){
fmt.Println("Starting server")
// Variables
clients_B := make(map[string]*net.UDPAddr) // map of all the connected B clients
cmd_from_B := make([]byte, 10) // command sent by clients: B CONNECT or DISCONNECT
messages := make(chan []byte)
// UDP listeners one on port A and one on Port B.
conn_sender := create_connection("", PORT_A)
conn_receiver := create_connection("", PORT_B)
// Close connection when exiting main thread
defer conn_sender.Close()
defer conn_receiver.Close()
go receive_bytes(conn_sender, messages)
go send_to_B(conn_receiver, clients_B, messages)
// Checking for new client B and adding them to the map
// map clients_B keys are string representation of the udpaddr
// and the value are the udpaddr object
for{
n,raddr,err := conn_receiver.ReadFromUDP(cmd_from_B)
if err != nil{
fmt.Println(err)
continue
}
switch string(cmd_from_B[0:n]) {
case "CONNECT":
clients_B[raddr.String()] = raddr // Adds the new connected client to the map
fmt.Println(raddr.String(), "connected (B)")
case "DISCONNECT":
delete(clients_B, raddr.String()) // Removes the disconnected client form the clientB list
fmt.Println(raddr.String(), "disconnected (B)")
}
//fmt.Println(clients_B)
}
}