-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
130 lines (113 loc) · 2.88 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
package main
import (
"net"
"fmt"
"github.com/akamensky/argparse"
"os"
)
const (
MESSAGE_SIZE = 1024
CONNECT_MSG = "CONNECT\r\n"
DISCONNECT_MSG = "DISCONNECT\r\n"
)
func StartServer(host *string, pin, pout *int){
fmt.Println("Started Server execution")
pin_addr := net.UDPAddr{Port: *pin, IP: net.ParseIP(*host)}
pout_addr :=net.UDPAddr{Port: *pout, IP: net.ParseIP(*host)}
conn_in, err := net.ListenUDP("udp", &pin_addr)
if err != nil {
fmt.Printf("Some error %v\n", err)
}
conn_out, err := net.ListenUDP("udp", &pout_addr)
if err != nil {
fmt.Printf("Some error %v\n", err)
}
go StartServerListener(conn_in, conn_out)
}
func StartServerListener(conn_in *net.UDPConn, conn_out *net.UDPConn){
p := make([]byte, MESSAGE_SIZE)
comm := make(chan []byte)
go outputListener(conn_out, comm)
for {
_, _, err := conn_in.ReadFromUDP(p)
if err != nil {
fmt.Printf("Couldn't send response %v", err)
continue
}
comm <- p
}
}
func outputListener(ser *net.UDPConn, comm <- chan []byte) {
packet := make([]byte, MESSAGE_SIZE)
sessions := make(map[string]chan bool)
for {
n, addr, err := ser.ReadFromUDP(packet)
if err != nil {
fmt.Printf("Couldn't get a message %v\n", err)
}
msg := string(packet[0:n])
ipaddr := addr.String()
switch msg {
case CONNECT_MSG:
fmt.Println("Connect", ipaddr)
sessions[ipaddr] = make(chan bool)
go broadcast(ser, addr, comm, sessions[ipaddr])
case DISCONNECT_MSG:
fmt.Println("Disconect ", ipaddr)
sessions[addr.String()] <- false
delete(sessions, ipaddr)
}
}
}
func broadcast(ser *net.UDPConn, addr *net.UDPAddr, comm <- chan []byte, quit <- chan bool) {
for {
select {
case msg:= <-comm:
_,err := ser.WriteToUDP(msg, addr)
if err != nil {
fmt.Printf("Couldn't send response %v", err)
}
case <- quit:
break
}
}
}
func cliOutput() (host* string, pout* int, pin* int){
parser := argparse.NewParser("server", "Simple UDP server written in GO")
host = parser.String("H", "host", &argparse.Options{Required: true, Help: "Specify ip address"})
pout = parser.Int("o", "output", &argparse.Options{Required: true, Help: "Specify output port"})
pin = parser.Int("i", "input", &argparse.Options{Required: true, Help: "Specify input port"})
err := parser.Parse(os.Args)
if err != nil {
// In case of error print error and print usage
// This can also be done by passing -h or --help flags
fmt.Print(parser.Usage(err))
os.Exit(1)
}
return
}
func main() {
host, pout, pin := cliOutput()
switch {
case *host == "":
fmt.Println("Specify Servers ip address")
os.Exit(1)
case *pout == 0:
fmt.Println("Specify output port")
os.Exit(1)
case *pin == 0:
fmt.Println("Specify input port")
os.Exit(1)
default:
// start the server
quit := make(chan bool)
go StartServer(host, pin, pout)
for {
select {
case <-quit:
break
}
}
fmt.Println("Server Execution Finished")
}
}