Last active
August 29, 2015 13:56
-
-
Save BitPuffin/8984557 to your computer and use it in GitHub Desktop.
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
import asyncio, sockets, logging | |
var socket: PAsyncSocket | |
var port: int | |
proc setupSocket() = | |
debug("Creating asynchronous socket") | |
socket = asyncsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, false) | |
doAssert(socket != nil, "Socket creation was unsuccessful") | |
debug("Done.") | |
debug("Binding socket to port") | |
const defaultPort = 13337 | |
const highestPort = 13400 | |
for port in defaultPort..highestPort: | |
try: | |
debug("Trying port: $#", port) | |
socket.bindAddr(TPort(port)) | |
port = port # This part | |
break | |
except EOS: | |
let msg = getCurrentExceptionMsg() | |
error(msg) | |
if port == highestPort: | |
let msg = getCurrentExceptionMsg() | |
error("Tried all ports, failure: ", msg) | |
raise newException(EOS, getCurrentExceptionMsg()) | |
else: | |
continue | |
proc serve*() = | |
info("Starting server..") | |
setupSocket() | |
echo "yay" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment