commit
deb3729cb7
2 changed files with 10 additions and 5 deletions
|
|
@ -363,7 +363,7 @@ proc run*(handleRequest: proc (client: Socket,
|
||||||
port = Port(80)) =
|
port = Port(80)) =
|
||||||
## encapsulates the server object and main loop
|
## encapsulates the server object and main loop
|
||||||
var s: TServer
|
var s: TServer
|
||||||
open(s, port)
|
open(s, port, reuseAddr = true)
|
||||||
#echo("httpserver running on port ", s.port)
|
#echo("httpserver running on port ", s.port)
|
||||||
while true:
|
while true:
|
||||||
next(s)
|
next(s)
|
||||||
|
|
|
||||||
|
|
@ -48,12 +48,21 @@ when defined(nimdoc):
|
||||||
events: set[Event]): SelectorKey {.discardable.} =
|
events: set[Event]): SelectorKey {.discardable.} =
|
||||||
## Updates the events which ``fd`` wants notifications for.
|
## Updates the events which ``fd`` wants notifications for.
|
||||||
|
|
||||||
|
proc unregister*(s: Selector, fd: SocketHandle): SelectorKey {.discardable.} =
|
||||||
|
## Unregisters file descriptor ``fd`` from selector ``s``.
|
||||||
|
|
||||||
|
proc close*(s: Selector) =
|
||||||
|
## Closes the selector
|
||||||
|
|
||||||
proc select*(s: Selector, timeout: int): seq[ReadyInfo] =
|
proc select*(s: Selector, timeout: int): seq[ReadyInfo] =
|
||||||
## The ``events`` field of the returned ``key`` contains the original events
|
## The ``events`` field of the returned ``key`` contains the original events
|
||||||
## for which the ``fd`` was bound. This is contrary to the ``events`` field
|
## for which the ``fd`` was bound. This is contrary to the ``events`` field
|
||||||
## of the ``TReadyInfo`` tuple which determines which events are ready
|
## of the ``TReadyInfo`` tuple which determines which events are ready
|
||||||
## on the ``fd``.
|
## on the ``fd``.
|
||||||
|
|
||||||
|
proc newSelector*(): Selector =
|
||||||
|
## Creates a new selector
|
||||||
|
|
||||||
proc contains*(s: Selector, fd: SocketHandle): bool =
|
proc contains*(s: Selector, fd: SocketHandle): bool =
|
||||||
## Determines whether selector contains a file descriptor.
|
## Determines whether selector contains a file descriptor.
|
||||||
|
|
||||||
|
|
@ -78,8 +87,6 @@ elif defined(linux):
|
||||||
|
|
||||||
proc register*(s: Selector, fd: SocketHandle, events: set[Event],
|
proc register*(s: Selector, fd: SocketHandle, events: set[Event],
|
||||||
data: RootRef): SelectorKey {.discardable.} =
|
data: RootRef): SelectorKey {.discardable.} =
|
||||||
## Registers file descriptor ``fd`` to selector ``s`` with a set of TEvent
|
|
||||||
## ``events``.
|
|
||||||
var event = createEventStruct(events, fd)
|
var event = createEventStruct(events, fd)
|
||||||
if events != {}:
|
if events != {}:
|
||||||
if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fd, addr(event)) != 0:
|
if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fd, addr(event)) != 0:
|
||||||
|
|
@ -92,7 +99,6 @@ elif defined(linux):
|
||||||
|
|
||||||
proc update*(s: Selector, fd: SocketHandle,
|
proc update*(s: Selector, fd: SocketHandle,
|
||||||
events: set[Event]): SelectorKey {.discardable.} =
|
events: set[Event]): SelectorKey {.discardable.} =
|
||||||
## Updates the events which ``fd`` wants notifications for.
|
|
||||||
if s.fds[fd].events != events:
|
if s.fds[fd].events != events:
|
||||||
if events == {}:
|
if events == {}:
|
||||||
# This fd is idle -- it should not be registered to epoll.
|
# This fd is idle -- it should not be registered to epoll.
|
||||||
|
|
@ -204,7 +210,6 @@ elif not defined(nimdoc):
|
||||||
|
|
||||||
proc update*(s: Selector, fd: SocketHandle,
|
proc update*(s: Selector, fd: SocketHandle,
|
||||||
events: set[Event]): SelectorKey {.discardable.} =
|
events: set[Event]): SelectorKey {.discardable.} =
|
||||||
## Updates the events which ``fd`` wants notifications for.
|
|
||||||
if not s.fds.hasKey(fd):
|
if not s.fds.hasKey(fd):
|
||||||
raise newException(ValueError, "File descriptor not found.")
|
raise newException(ValueError, "File descriptor not found.")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue