-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpserver.go
124 lines (101 loc) · 3.64 KB
/
udpserver.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
// Landon Wainwright.
package transport
import (
"bytes"
"encoding/gob"
"log"
"net"
"github.com/landonia/keystore"
)
// UDPServer holds the UDP client connection
type UDPServer struct {
addr string // the address to bind to
requests chan<- *keystore.Request // The request event channel to send the requests
udpconn *net.UDPConn // The udp connection
quit chan bool // The channel to wait on to finish the connection
connected bool // Whether the server is currently connected
}
// StartUDPServer will start a new UDP service allowing requests
// to be made to the key store service
func StartUDPServer(addr string, requests chan<- *keystore.Request) {
// Start the udp server
go func() {
// Create the server and start it up
log.Printf("Starting UDP server using address: %s", addr)
server := newUDPServer(addr, requests)
server.connect()
}()
}
// newUDPServer will create a new UDP server using the address
func newUDPServer(addr string, requests chan<- *keystore.Request) *UDPServer {
return &UDPServer{addr: addr, requests: requests, quit: make(chan bool)}
}
// connect will enable the event listener for incoming data
func (server *UDPServer) connect() {
if server.connected {
log.Printf("The UDP server is already connected using address: %s", server.addr)
return
}
// Now create a connection
udpAddr, err := net.ResolveUDPAddr("udp", server.addr)
if err == nil {
server.udpconn, err = net.ListenUDP("udp", udpAddr)
}
if err != nil {
log.Fatal(err)
return
}
log.Printf("UDP server now connected to address: %s", server.addr)
server.connected = true
go func() {
buf := make([]byte, 1024)
for {
// Collect the bytes from the socket
n, client, err := server.udpconn.ReadFromUDP(buf)
if err != nil {
log.Println("Error whilst reading UDP packet: ", err)
} else {
// Attempt to read the data into a request
request := &keystore.Request{}
clientaddr := client.String()
if err := gob.NewDecoder(bytes.NewReader(buf[:n])).Decode(request); err != nil {
log.Printf("Error whilst reading UDP packet: %s", err)
// Send an error response back to the client
go func() {
// The channel can not be sent so will be created
// Now we need to send the response back to the client
log.Printf("Sending UDP error response to client [%s]", clientaddr)
// Handle the response
writeResponse(server.udpconn, client, &keystore.Response{Error: err.Error()})
}()
} else {
log.Printf("Received UDP request from client: [%s]", clientaddr)
// Wait for the response and send it back to the client
go func() {
// The channel can not be sent so will be created
request.ResponseChannel = make(chan *keystore.Response)
// Now send the request on the request channel
server.requests <- request
response := <-request.ResponseChannel
// Now we need to send the response back to the client
log.Printf("Received response... Sending UDP response to client [%s]", clientaddr)
// Handle the response
writeResponse(server.udpconn, client, response)
}()
}
}
}
}()
}
// writeResponse will write the response object back to the udp client
func writeResponse(udpconn *net.UDPConn, client *net.UDPAddr, response *keystore.Response) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(response); err != nil {
log.Printf("Error writing UDP response to buffer: %s", err)
}
// Write the response to the client
_, err := udpconn.WriteToUDP(buf.Bytes(), client)
if err != nil {
log.Printf("Error writing UDP response to client: %s", err)
}
}