Skip to content

Commit

Permalink
nimsuggest: add an option to bind to a free port (#10328)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaviss authored and Araq committed Jan 16, 2019
1 parent 1cb5e20 commit fc30cf0
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions nimsuggest/nimsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Usage:
nimsuggest [options] projectfile.nim
Options:
--autobind automatically binds into a free port
--port:PORT port, by default 6000
--address:HOST binds to that address, by default ""
--stdin read commands from stdin and write results to
Expand All @@ -50,6 +51,8 @@ Options:
The server then listens to the connection and takes line-based commands.
If --autobind is used, the binded port number will be printed to stdout.
In addition, all command line options of Nim that do not affect code generation
are supported.
"""
Expand All @@ -68,6 +71,7 @@ var
gEmitEof: bool # whether we write '!EOF!' dummy lines
gLogging = defined(logging)
gRefresh: bool
gAutoBind = false

requests: Channel[string]
results: Channel[Suggest]
Expand Down Expand Up @@ -306,9 +310,15 @@ proc replCmdline(x: ThreadParams) {.thread.} =

proc replTcp(x: ThreadParams) {.thread.} =
var server = newSocket()
server.bindAddr(x.port, x.address)
if gAutoBind:
let port = server.connectToNextFreePort(x.address)
server.listen()
echo port
stdout.flushFile()
else:
server.bindAddr(x.port, x.address)
server.listen()
var inp = "".TaintedString
server.listen()
while true:
var stdoutSocket = newSocket()
accept(server, stdoutSocket)
Expand Down Expand Up @@ -544,6 +554,9 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
of "help", "h":
stdout.writeline(Usage)
quit()
of "autobind":
gMode = mtcp
gAutoBind = true
of "port":
gPort = parseInt(p.val).Port
gMode = mtcp
Expand Down

0 comments on commit fc30cf0

Please sign in to comment.