-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version for the address server
- Loading branch information
Luis Fonseca
committed
Jan 13, 2018
1 parent
b22801f
commit 72ee232
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"fmt" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// This is the default port for client and server; if running locally, change the game to use `hostPort=2301` | ||
AddressServerPort = 2300 | ||
UdpBufferSize = 1200 | ||
) | ||
|
||
func main() { | ||
log.WithField("port", AddressServerPort).Infoln("Starting address server") | ||
|
||
udpAddress, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", "0.0.0.0", AddressServerPort)) | ||
if err != nil { | ||
log.WithError(err).Panic("Failed to resolve udp address") | ||
} | ||
|
||
udpConn, err := net.ListenUDP("udp", udpAddress) | ||
if err != nil { | ||
log.WithError(err).Panic("Failed to listen for UDP packets.") | ||
} | ||
|
||
defer udpConn.Close() | ||
|
||
udpBuffer := make([]byte, UdpBufferSize) | ||
|
||
for { | ||
bytes, udpClientAddress, err := udpConn.ReadFromUDP(udpBuffer) | ||
if err != nil { | ||
log.WithError(err).Warn("Failed to read UDP packet.") | ||
continue | ||
} | ||
|
||
if bytes == 0 { | ||
log.Warn("Failed to read any data from the UDP packet.") | ||
continue | ||
} | ||
|
||
switch udpBuffer[0] { | ||
case 9: | ||
reply := make([]byte, 16) | ||
reply[0] = 9 // reply with the same first byte | ||
copy(reply[1:], []byte {0x02, 0x00, 0xd9, 0x49}) // these 4 are unknown, but it seems to work | ||
copy(reply[5:], udpClientAddress.IP) | ||
// from [9:12], all 0x00s | ||
copy(reply[13:], udpBuffer[1:4]) | ||
|
||
log.WithField("reply", reply).WithField("ip", udpClientAddress.IP).Info("Replying to request.") | ||
udpConn.WriteToUDP(reply, udpClientAddress) | ||
case 10: | ||
// TODO: reply back with the same message | ||
} | ||
|
||
log.WithField("fist_byte", udpBuffer[0]).WithField("ip", udpClientAddress.IP).Info("Got first byte") | ||
} | ||
} |