Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
3270676e77
34 changed files with 567 additions and 167 deletions
|
|
@ -195,8 +195,14 @@ proc open*(connection, user, password, database: string): TDbConn {.
|
|||
## be established.
|
||||
result = mysql.Init(nil)
|
||||
if result == nil: dbError("could not open database connection")
|
||||
if mysql.RealConnect(result, "", user, password, database,
|
||||
0'i32, nil, 0) == nil:
|
||||
let
|
||||
colonPos = connection.find(':')
|
||||
host = if colonPos < 0: connection
|
||||
else: substr(connection, 0, colonPos-1)
|
||||
port: int32 = if colonPos < 0: 0'i32
|
||||
else: substr(connection, colonPos+1).parseInt.int32
|
||||
if mysql.RealConnect(result, host, user, password, database,
|
||||
port, nil, 0) == nil:
|
||||
var errmsg = $mysql.error(result)
|
||||
db_mysql.Close(result)
|
||||
dbError(errmsg)
|
||||
|
|
|
|||
|
|
@ -314,6 +314,9 @@ static unsigned long nimNaN[2]={0xffffffff, 0x7fffffff};
|
|||
# define INF INFINITY
|
||||
# elif defined(HUGE_VAL)
|
||||
# define INF HUGE_VAL
|
||||
# elif defined(_MSC_VER)
|
||||
# include <float.h>
|
||||
# define INF (DBL_MAX+DBL_MAX)
|
||||
# else
|
||||
# define INF (1.0 / 0.0)
|
||||
# endif
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type
|
|||
epoll_data* {.importc: "union epoll_data",
|
||||
header: "<sys/epoll.h>", pure, final.} = object # TODO: This is actually a union.
|
||||
#thePtr* {.importc: "ptr".}: pointer
|
||||
fd*: cint # \
|
||||
fd* {.importc: "fd".}: cint # \
|
||||
#u32*: uint32
|
||||
#u64*: uint64
|
||||
|
||||
|
|
|
|||
|
|
@ -473,7 +473,6 @@ else:
|
|||
|
||||
proc update(p: PDispatcher, sock: TSocketHandle, events: set[TEvent]) =
|
||||
assert sock in p.selector
|
||||
echo("Update: ", events)
|
||||
if events == {}:
|
||||
discard p.selector.unregister(sock)
|
||||
else:
|
||||
|
|
@ -499,23 +498,25 @@ else:
|
|||
for info in p.selector.select(timeout):
|
||||
let data = PData(info.key.data)
|
||||
assert data.sock == info.key.fd
|
||||
echo("R: ", data.readCBs.len, " W: ", data.writeCBs.len, ". ", info.events)
|
||||
|
||||
if EvRead in info.events:
|
||||
var newReadCBs: seq[TCallback] = @[]
|
||||
for cb in data.readCBs:
|
||||
# Callback may add items to ``data.readCBs`` which causes issues if
|
||||
# we are iterating over ``data.readCBs`` at the same time. We therefore
|
||||
# make a copy to iterate over.
|
||||
let currentCBs = data.readCBs
|
||||
data.readCBs = @[]
|
||||
for cb in currentCBs:
|
||||
if not cb(data.sock):
|
||||
# Callback wants to be called again.
|
||||
newReadCBs.add(cb)
|
||||
data.readCBs = newReadCBs
|
||||
data.readCBs.add(cb)
|
||||
|
||||
if EvWrite in info.events:
|
||||
var newWriteCBs: seq[TCallback] = @[]
|
||||
for cb in data.writeCBs:
|
||||
let currentCBs = data.writeCBs
|
||||
data.writeCBs = @[]
|
||||
for cb in currentCBs:
|
||||
if not cb(data.sock):
|
||||
# Callback wants to be called again.
|
||||
newWriteCBs.add(cb)
|
||||
data.writeCBs = newWriteCBs
|
||||
data.writeCBs.add(cb)
|
||||
|
||||
var newEvents: set[TEvent]
|
||||
if data.readCBs.len != 0: newEvents = {EvRead}
|
||||
|
|
@ -615,7 +616,6 @@ else:
|
|||
retFuture.complete(0)
|
||||
addWrite(p, socket, cb)
|
||||
return retFuture
|
||||
|
||||
|
||||
proc acceptAddr*(p: PDispatcher, socket: TSocketHandle):
|
||||
PFuture[tuple[address: string, client: TSocketHandle]] =
|
||||
|
|
@ -854,7 +854,7 @@ when isMainModule:
|
|||
sock.setBlocking false
|
||||
|
||||
|
||||
when false:
|
||||
when true:
|
||||
# Await tests
|
||||
proc main(p: PDispatcher): PFuture[int] {.async.} =
|
||||
discard await p.connect(sock, "irc.freenode.net", TPort(6667))
|
||||
|
|
@ -880,7 +880,7 @@ when isMainModule:
|
|||
|
||||
|
||||
else:
|
||||
when false:
|
||||
when true:
|
||||
|
||||
var f = p.connect(sock, "irc.freenode.org", TPort(6667))
|
||||
f.callback =
|
||||
|
|
@ -919,4 +919,4 @@ when isMainModule:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,13 @@
|
|||
# TODO: Docs.
|
||||
|
||||
import tables, os, unsigned, hashes
|
||||
import sockets2
|
||||
|
||||
when defined(linux): import posix, epoll
|
||||
elif defined(windows): import winlean
|
||||
|
||||
proc hash*(x: TSocketHandle): THash {.borrow.}
|
||||
proc `$`*(x: TSocketHandle): string {.borrow.}
|
||||
|
||||
type
|
||||
TEvent* = enum
|
||||
|
|
@ -31,7 +33,7 @@ when defined(linux) or defined(nimdoc):
|
|||
type
|
||||
PSelector* = ref object
|
||||
epollFD: cint
|
||||
events: array[64, ptr epoll_event]
|
||||
events: array[64, epoll_event]
|
||||
fds: TTable[TSocketHandle, PSelectorKey]
|
||||
|
||||
proc createEventStruct(events: set[TEvent], fd: TSocketHandle): epoll_event =
|
||||
|
|
@ -66,17 +68,25 @@ when defined(linux) or defined(nimdoc):
|
|||
var event = createEventStruct(events, fd)
|
||||
|
||||
s.fds[fd].events = events
|
||||
echo("About to update")
|
||||
if epoll_ctl(s.epollFD, EPOLL_CTL_MOD, fd, addr(event)) != 0:
|
||||
if OSLastError().cint == ENOENT:
|
||||
# Socket has been closed. Epoll automatically removes disconnected
|
||||
# sockets.
|
||||
s.fds.del(fd)
|
||||
osError("Socket has been disconnected")
|
||||
|
||||
OSError(OSLastError())
|
||||
echo("finished updating")
|
||||
result = s.fds[fd]
|
||||
|
||||
proc unregister*(s: PSelector, fd: TSocketHandle): PSelectorKey {.discardable.} =
|
||||
if not s.fds.hasKey(fd):
|
||||
raise newException(EInvalidValue, "File descriptor not found.")
|
||||
if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fd, nil) != 0:
|
||||
OSError(OSLastError())
|
||||
if osLastError().cint == ENOENT:
|
||||
# Socket has been closed. Epoll automatically removes disconnected
|
||||
# sockets so its already been removed.
|
||||
else:
|
||||
OSError(OSLastError())
|
||||
result = s.fds[fd]
|
||||
s.fds.del(fd)
|
||||
|
||||
|
|
@ -92,21 +102,21 @@ when defined(linux) or defined(nimdoc):
|
|||
## on the ``fd``.
|
||||
result = @[]
|
||||
|
||||
let evNum = epoll_wait(s.epollFD, s.events[0], 64.cint, timeout.cint)
|
||||
let evNum = epoll_wait(s.epollFD, addr s.events[0], 64.cint, timeout.cint)
|
||||
if evNum < 0: OSError(OSLastError())
|
||||
if evNum == 0: return @[]
|
||||
for i in 0 .. <evNum:
|
||||
var evSet: set[TEvent] = {}
|
||||
if (s.events[i].events and EPOLLIN) != 0: evSet = evSet + {EvRead}
|
||||
if (s.events[i].events and EPOLLOUT) != 0: evSet = evSet + {EvWrite}
|
||||
|
||||
let selectorKey = s.fds[s.events[i].data.fd.TSocketHandle]
|
||||
assert selectorKey != nil
|
||||
result.add((selectorKey, evSet))
|
||||
|
||||
proc newSelector*(): PSelector =
|
||||
new result
|
||||
result.epollFD = epoll_create(64)
|
||||
result.events = cast[array[64, ptr epoll_event]](alloc0(sizeof(epoll_event)*64))
|
||||
result.events = cast[array[64, epoll_event]](alloc0(sizeof(epoll_event)*64))
|
||||
result.fds = initTable[TSocketHandle, PSelectorKey]()
|
||||
if result.epollFD < 0:
|
||||
OSError(OSLastError())
|
||||
|
|
@ -247,4 +257,4 @@ when isMainModule:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ else:
|
|||
export TSocketHandle, TSockaddr_in, TAddrinfo, INADDR_ANY, TSockAddr, TSockLen,
|
||||
inet_ntoa, recv, `==`, connect, send, accept
|
||||
|
||||
export
|
||||
SO_ERROR,
|
||||
SOL_SOCKET
|
||||
|
||||
type
|
||||
|
||||
TPort* = distinct uint16 ## port type
|
||||
|
|
@ -208,6 +212,24 @@ proc htons*(x: int16): int16 =
|
|||
## order, this is a no-op; otherwise, it performs a 2-byte swap operation.
|
||||
result = sockets2.ntohs(x)
|
||||
|
||||
proc getSockOptInt*(socket: TSocketHandle, level, optname: int): int {.
|
||||
tags: [FReadIO].} =
|
||||
## getsockopt for integer options.
|
||||
var res: cint
|
||||
var size = sizeof(res).TSocklen
|
||||
if getsockopt(socket, cint(level), cint(optname),
|
||||
addr(res), addr(size)) < 0'i32:
|
||||
osError(osLastError())
|
||||
result = int(res)
|
||||
|
||||
proc setSockOptInt*(socket: TSocketHandle, level, optname, optval: int) {.
|
||||
tags: [FWriteIO].} =
|
||||
## setsockopt for integer options.
|
||||
var value = cint(optval)
|
||||
if setsockopt(socket, cint(level), cint(optname), addr(value),
|
||||
sizeof(value).TSocklen) < 0'i32:
|
||||
osError(osLastError())
|
||||
|
||||
when defined(Windows):
|
||||
var wsa: TWSADATA
|
||||
if WSAStartup(0x0101'i16, addr wsa) != 0: OSError(OSLastError())
|
||||
|
|
|
|||
|
|
@ -1164,13 +1164,13 @@ when not defined(nimrodVM):
|
|||
## from it before writing to it is undefined behaviour!
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `allocShared` to allocate from a shared heap.
|
||||
proc alloc*(T: typedesc, size = 1): ptr T {.inline.} =
|
||||
proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline.} =
|
||||
## allocates a new memory block with at least ``T.sizeof * size``
|
||||
## bytes. The block has to be freed with ``realloc(block, 0)`` or
|
||||
## ``dealloc(block)``. The block is not initialized, so reading
|
||||
## bytes. The block has to be freed with ``resize(block, 0)`` or
|
||||
## ``free(block)``. The block is not initialized, so reading
|
||||
## from it before writing to it is undefined behaviour!
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `allocShared` to allocate from a shared heap.
|
||||
## Use `createSharedU` to allocate from a shared heap.
|
||||
cast[ptr T](alloc(T.sizeof * size))
|
||||
proc alloc0*(size: int): pointer {.noconv, rtl, tags: [].}
|
||||
## allocates a new memory block with at least ``size`` bytes. The
|
||||
|
|
@ -1179,13 +1179,13 @@ when not defined(nimrodVM):
|
|||
## containing zero, so it is somewhat safer than ``alloc``.
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `allocShared0` to allocate from a shared heap.
|
||||
proc alloc0*(T: typedesc, size = 1): ptr T {.inline.} =
|
||||
proc create*(T: typedesc, size = 1.Positive): ptr T {.inline.} =
|
||||
## allocates a new memory block with at least ``T.sizeof * size``
|
||||
## bytes. The block has to be freed with ``realloc(block, 0)`` or
|
||||
## ``dealloc(block)``. The block is initialized with all bytes
|
||||
## containing zero, so it is somewhat safer than ``alloc``.
|
||||
## bytes. The block has to be freed with ``resize(block, 0)`` or
|
||||
## ``free(block)``. The block is initialized with all bytes
|
||||
## containing zero, so it is somewhat safer than ``createU``.
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `allocShared0` to allocate from a shared heap.
|
||||
## Use `createShared` to allocate from a shared heap.
|
||||
cast[ptr T](alloc0(T.sizeof * size))
|
||||
proc realloc*(p: pointer, newSize: int): pointer {.noconv, rtl, tags: [].}
|
||||
## grows or shrinks a given memory block. If p is **nil** then a new
|
||||
|
|
@ -1195,14 +1195,14 @@ when not defined(nimrodVM):
|
|||
## be freed with ``dealloc``.
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `reallocShared` to reallocate from a shared heap.
|
||||
proc reallocType*[T](p: ptr T, newSize: int): ptr T {.inline.} =
|
||||
proc resize*[T](p: ptr T, newSize: Natural): ptr T {.inline.} =
|
||||
## grows or shrinks a given memory block. If p is **nil** then a new
|
||||
## memory block is returned. In either way the block has at least
|
||||
## ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is not
|
||||
## **nil** ``realloc`` calls ``dealloc(p)``. In other cases the block
|
||||
## has to be freed with ``dealloc``. The allocated memory belongs to
|
||||
## **nil** ``resize`` calls ``free(p)``. In other cases the block
|
||||
## has to be freed with ``free``. The allocated memory belongs to
|
||||
## its allocating thread!
|
||||
## Use `reallocShared` to reallocate from a shared heap.
|
||||
## Use `resizeShared` to reallocate from a shared heap.
|
||||
cast[ptr T](realloc(p, T.sizeof * newSize))
|
||||
proc dealloc*(p: pointer) {.noconv, rtl, tags: [].}
|
||||
## frees the memory allocated with ``alloc``, ``alloc0`` or
|
||||
|
|
@ -1212,16 +1212,18 @@ when not defined(nimrodVM):
|
|||
## or other memory may be corrupted.
|
||||
## The freed memory must belong to its allocating thread!
|
||||
## Use `deallocShared` to deallocate from a shared heap.
|
||||
proc free*[T](p: ptr T) {.inline.} =
|
||||
dealloc(p)
|
||||
proc allocShared*(size: int): pointer {.noconv, rtl.}
|
||||
## allocates a new memory block on the shared heap with at
|
||||
## least ``size`` bytes. The block has to be freed with
|
||||
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block
|
||||
## is not initialized, so reading from it before writing to it is
|
||||
## undefined behaviour!
|
||||
proc allocShared*(T: typedesc, size: int): ptr T {.inline.} =
|
||||
proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline.} =
|
||||
## allocates a new memory block on the shared heap with at
|
||||
## least ``T.sizeof * size`` bytes. The block has to be freed with
|
||||
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block
|
||||
## ``resizeShared(block, 0)`` or ``freeShared(block)``. The block
|
||||
## is not initialized, so reading from it before writing to it is
|
||||
## undefined behaviour!
|
||||
cast[ptr T](allocShared(T.sizeof * size))
|
||||
|
|
@ -1231,25 +1233,25 @@ when not defined(nimrodVM):
|
|||
## ``reallocShared(block, 0)`` or ``deallocShared(block)``.
|
||||
## The block is initialized with all bytes
|
||||
## containing zero, so it is somewhat safer than ``allocShared``.
|
||||
proc allocShared0*(T: typedesc, size: int): ptr T {.inline.} =
|
||||
proc createShared*(T: typedesc, size = 1.Positive): ptr T {.inline.} =
|
||||
## allocates a new memory block on the shared heap with at
|
||||
## least ``T.sizeof * size`` bytes. The block has to be freed with
|
||||
## ``reallocShared(block, 0)`` or ``deallocShared(block)``.
|
||||
## ``resizeShared(block, 0)`` or ``freeShared(block)``.
|
||||
## The block is initialized with all bytes
|
||||
## containing zero, so it is somewhat safer than ``allocShared``.
|
||||
cast[ptr T](allocShared(T.sizeof * size))
|
||||
## containing zero, so it is somewhat safer than ``createSharedU``.
|
||||
cast[ptr T](allocShared0(T.sizeof * size))
|
||||
proc reallocShared*(p: pointer, newSize: int): pointer {.noconv, rtl.}
|
||||
## grows or shrinks a given memory block on the heap. If p is **nil**
|
||||
## then a new memory block is returned. In either way the block has at
|
||||
## least ``newSize`` bytes. If ``newSize == 0`` and p is not **nil**
|
||||
## ``reallocShared`` calls ``deallocShared(p)``. In other cases the
|
||||
## block has to be freed with ``deallocShared``.
|
||||
proc reallocSharedType*[T](p: ptr T, newSize: int): ptr T {.inline.} =
|
||||
proc resizeShared*[T](p: ptr T, newSize: Natural): ptr T {.inline.} =
|
||||
## grows or shrinks a given memory block on the heap. If p is **nil**
|
||||
## then a new memory block is returned. In either way the block has at
|
||||
## least ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is
|
||||
## not **nil** ``reallocShared`` calls ``deallocShared(p)``. In other
|
||||
## cases the block has to be freed with ``deallocShared``.
|
||||
## not **nil** ``resizeShared`` calls ``freeShared(p)``. In other
|
||||
## cases the block has to be freed with ``freeShared``.
|
||||
cast[ptr T](reallocShared(p, T.sizeof * newSize))
|
||||
proc deallocShared*(p: pointer) {.noconv, rtl.}
|
||||
## frees the memory allocated with ``allocShared``, ``allocShared0`` or
|
||||
|
|
@ -1257,6 +1259,13 @@ when not defined(nimrodVM):
|
|||
## free the memory a leak occurs; if one tries to access freed
|
||||
## memory (or just freeing it twice!) a core dump may happen
|
||||
## or other memory may be corrupted.
|
||||
proc freeShared*[T](p: ptr T) {.inline.} =
|
||||
## frees the memory allocated with ``createShared``, ``createSharedU`` or
|
||||
## ``resizeShared``. This procedure is dangerous! If one forgets to
|
||||
## free the memory a leak occurs; if one tries to access freed
|
||||
## memory (or just freeing it twice!) a core dump may happen
|
||||
## or other memory may be corrupted.
|
||||
deallocShared(p)
|
||||
|
||||
proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.}
|
||||
## swaps the values `a` and `b`. This is often more efficient than
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue