summary refs log tree commit diff stats
path: root/lib/pure/asyncio.nim
blob: 417a220f90ab9bad73c053503399a42d10026a63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#
#
#            Nimrod's Runtime Library
#        (c) Copyright 2012 Andreas Rumpf, Dominik Picheta
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

import sockets, os

## This module implements an asynchronous event loop for sockets. 
## It is akin to Python's asyncore module. Many modules that use sockets
## have an implementation for this module, those modules should all have a 
## ``register`` function which you should use to add it to a dispatcher so
## that you can receive the events associated with that module.
##
## Once everything is registered in a dispatcher, you need to call the ``poll``
## function in a while loop.
##
## **Note:** Most modules have tasks which need to be ran regularly, this is
## why you should not call ``poll`` with a infinite timeout, or even a 
## very long one. In most cases the default timeout is fine.
##
## **Note:** This module currently only supports select(), this is limited by
## FD_SETSIZE, which is usually 1024. So you may only be able to use 1024
## sockets at a time.
## 
## Most (if not all) modules that use asyncio provide a userArg which is passed
## on with the events. The type that you set userArg to must be inheriting from
## TObject!

type
  TDelegate = object
    deleVal*: PObject

    handleRead*: proc (h: PObject)
    handleWrite*: proc (h: PObject)
    handleConnect*: proc (h: PObject)

    handleAccept*: proc (h: PObject)
    getSocket*: proc (h: PObject): tuple[info: TInfo, sock: TSocket]

    task*: proc (h: PObject)
    mode*: TMode
    
  PDelegate* = ref TDelegate

  PDispatcher* = ref TDispatcher
  TDispatcher = object
    delegates: seq[PDelegate]

  PAsyncSocket* = ref TAsyncSocket
  TAsyncSocket = object of TObject
    socket: TSocket
    info: TInfo

    userArg: PObject

    handleRead*: proc (s: PAsyncSocket, arg: PObject)
    handleConnect*: proc (s:  PAsyncSocket, arg: PObject)

    handleAccept*: proc (s:  PAsyncSocket, arg: PObject)

    lineBuffer: TaintedString ## Temporary storage for ``recvLine``

  TInfo* = enum
    SockIdle, SockConnecting, SockConnected, SockListening, SockClosed
  
  TMode* = enum
    MReadable, MWriteable, MReadWrite

proc newDelegate*(): PDelegate =
  ## Creates a new delegate.
  new(result)
  result.handleRead = (proc (h: PObject) = nil)
  result.handleWrite = (proc (h: PObject) = nil)
  result.handleConnect = (proc (h: PObject) = nil)
  result.handleAccept = (proc (h: PObject) = nil)
  result.getSocket = (proc (h: PObject): tuple[info: TInfo, sock: TSocket] =
                        doAssert(false))
  result.task = (proc (h: PObject) = nil)
  result.mode = MReadable

proc newAsyncSocket(userArg: PObject = nil): PAsyncSocket =
  new(result)
  result.info = SockIdle
  result.userArg = userArg

  result.handleRead = (proc (s: PAsyncSocket, arg: PObject) = nil)
  result.handleConnect = (proc (s: PAsyncSocket, arg: PObject) = nil)
  result.handleAccept = (proc (s: PAsyncSocket, arg: PObject) = nil)

  result.lineBuffer = "".TaintedString

proc AsyncSocket*(domain: TDomain = AF_INET, typ: TType = SOCK_STREAM, 
                  protocol: TProtocol = IPPROTO_TCP, 
                  userArg: PObject = nil): PAsyncSocket =
  result = newAsyncSocket(userArg)
  result.socket = socket(domain, typ, protocol)
  if result.socket == InvalidSocket: OSError()
  result.socket.setBlocking(false)

proc toDelegate(sock: PAsyncSocket): PDelegate =
  result = newDelegate()
  result.deleVal = sock
  result.getSocket = (proc (h: PObject): tuple[info: TInfo, sock: TSocket] =
                        return (PAsyncSocket(h).info, PAsyncSocket(h).socket))

  result.handleConnect = (proc (h: PObject) =
                            PAsyncSocket(h).info = SockConnected
                            PAsyncSocket(h).handleConnect(PAsyncSocket(h),
                               PAsyncSocket(h).userArg))
  result.handleRead = (proc (h: PObject) =
                         PAsyncSocket(h).handleRead(PAsyncSocket(h),
                            PAsyncSocket(h).userArg))
  result.handleAccept = (proc (h: PObject) =
                           PAsyncSocket(h).handleAccept(PAsyncSocket(h),
                              PAsyncSocket(h).userArg))

proc connect*(sock: PAsyncSocket, name: string, port = TPort(0),
                   af: TDomain = AF_INET) =
  ## Begins connecting ``sock`` to ``name``:``port``.
  sock.socket.connectAsync(name, port, af)
  sock.info = SockConnecting

proc close*(sock: PAsyncSocket) =
  ## Closes ``sock``. Terminates any current connections.
  sock.info = SockClosed
  sock.socket.close()

proc bindAddr*(sock: PAsyncSocket, port = TPort(0), address = "") =
  ## Equivalent to ``sockets.bindAddr``.
  sock.socket.bindAddr(port, address)

proc listen*(sock: PAsyncSocket) =
  ## Equivalent to ``sockets.listen``.
  sock.socket.listen()
  sock.info = SockListening

proc acceptAddr*(server: PAsyncSocket): tuple[sock: PAsyncSocket,
                                              address: string] =
  ## Equivalent to ``sockets.acceptAddr``.
  var (client, a) = server.socket.acceptAddr()
  if client == InvalidSocket: OSError()
  client.setBlocking(false) # TODO: Needs to be tested.
  
  var aSock: PAsyncSocket = newAsyncSocket()
  aSock.socket = client
  aSock.info = SockConnected
  
  return (aSock, a)

proc accept*(server: PAsyncSocket): PAsyncSocket =
  ## Equivalent to ``sockets.accept``.
  var (client, a) = server.acceptAddr()
  return client

proc newDispatcher*(): PDispatcher =
  new(result)
  result.delegates = @[]

proc register*(d: PDispatcher, deleg: PDelegate) =
  ## Registers delegate ``deleg`` with dispatcher ``d``.
  d.delegates.add(deleg)

proc register*(d: PDispatcher, sock: PAsyncSocket): PDelegate {.discardable.} =
  ## Registers async socket ``sock`` with dispatcher ``d``.
  result = sock.toDelegate()
  d.register(result)

proc unregister*(d: PDispatcher, deleg: PDelegate) =
  ## Unregisters deleg ``deleg`` from dispatcher ``d``.
  for i in 0..len(d.delegates)-1:
    if d.delegates[i] == deleg:
      d.delegates.del(i)
      return
  raise newException(EInvalidIndex, "Could not find delegate.")

proc isWriteable*(s: PAsyncSocket): bool =
  ## Determines whether socket ``s`` is ready to be written to.
  var writeSock = @[s.socket]
  return selectWrite(writeSock, 1) != 0 and s.socket notin writeSock

proc `userArg=`*(s: PAsyncSocket, val: PObject) =
  s.userArg = val

converter getSocket*(s: PAsyncSocket): TSocket =
  return s.socket

proc isConnected*(s: PAsyncSocket): bool =
  ## Determines whether ``s`` is connected.
  return s.info == SockConnected
proc isListening*(s: PAsyncSocket): bool =
  ## Determines whether ``s`` is listening for incoming connections.  
  return s.info == SockListening
proc isConnecting*(s: PAsyncSocket): bool =
  ## Determines whether ``s`` is connecting.  
  return s.info == SockConnecting

proc recvLine*(s: PAsyncSocket, line: var TaintedString): bool =
  ## Behaves similar to ``sockets.recvLine``, however it handles non-blocking
  ## sockets properly. This function guarantees that ``line`` is a full line,
  ## if this function can only retrieve some data; it will save this data and
  ## add it to the result when a full line is retrieved.
  setLen(line.string, 0)
  var dataReceived = "".TaintedString
  var ret = s.socket.recvLineAsync(dataReceived)
  case ret
  of RecvFullLine:
    if s.lineBuffer.len > 0:
      string(line).add(s.lineBuffer.string)
      setLen(s.lineBuffer.string, 0)
    
    string(line).add(dataReceived.string)
    result = true
  of RecvPartialLine:
    string(s.lineBuffer).add(dataReceived.string)
    result = false
  of RecvDisconnected:
    result = true
  of RecvFail:
    result = false


proc poll*(d: PDispatcher, timeout: int = 500): bool =
  ## This function checks for events on all the sockets in the `PDispatcher`.
  ## It then proceeds to call the correct event handler.
  ## 
  ## **Note:** There is no event which signifes when you have been disconnected,
  ## it is your job to check whether what you get from ``recv`` is ``""``.
  ## If you have been disconnected, `d`'s ``getSocket`` function should report
  ## this appropriately.
  ##
  ## This function returns ``True`` if there are sockets that are still 
  ## connected (or connecting), otherwise ``False``. Sockets that have been
  ## closed are immediately removed from the dispatcher automatically.
  ##
  ## **Note:** Each delegate has a task associated with it. This gets called
  ## after each select() call, if you make timeout ``-1`` the tasks will
  ## only be executed after one or more sockets becomes readable or writeable.
  
  result = true
  var readSocks, writeSocks: seq[TSocket] = @[]
  
  var L = d.delegates.len
  var dc = 0
  while dc < L:
    template deleg: expr = d.delegates[dc]
    let aSock = deleg.getSocket(deleg.deleVal)
    if (deleg.mode != MWriteable and aSock.info == SockConnected) or
          aSock.info == SockListening:
      readSocks.add(aSock.sock)
    if aSock.info == SockConnecting or
        (aSock.info == SockConnected and deleg.mode != MReadable):
      writeSocks.add(aSock.sock)
    if aSock.info == SockClosed:
      # Socket has been closed remove it from the dispatcher.
      d.delegates[dc] = d.delegates[L-1]
      
      dec L
    else: inc dc
  d.delegates.setLen(L)
  
  if readSocks.len() == 0 and writeSocks.len() == 0:
    return False
  
  if select(readSocks, writeSocks, timeout) != 0:
    for i in 0..len(d.delegates)-1:
      if i > len(d.delegates)-1: break # One delegate might've been removed.
      let deleg = d.delegates[i]
      let sock = deleg.getSocket(deleg.deleVal)
      if sock.info == SockConnected:
        if deleg.mode != MWriteable and sock.sock notin readSocks:
          if not (sock.info == SockConnecting):
            assert(not (sock.info == SockListening))
            deleg.handleRead(deleg.deleVal)
          else:
            assert(false)
        if deleg.mode != MReadable and sock.sock notin writeSocks:
          deleg.handleWrite(deleg.deleVal)
      
      if sock.info == SockListening:
        if sock.sock notin readSocks:
          # This is a server socket, that had listen() called on it.
          # This socket should have a client waiting now.
          deleg.handleAccept(deleg.deleVal)
      
      if sock.info == SockConnecting:
        # Checking whether the socket has connected this way should work on
        # Windows and Posix. I've checked. 
        if sock.sock notin writeSocks:
          deleg.handleConnect(deleg.deleVal)
  
  # Execute tasks
  for i in items(d.delegates):
    i.task(i.deleVal)
  
when isMainModule:
  type
    PIntType = ref TIntType
    TIntType = object of TObject
      val: int

    PMyArg = ref TMyArg
    TMyArg = object of TObject
      dispatcher: PDispatcher
      val: int

  proc testConnect(s: PAsyncSocket, arg: PObject) =
    echo("Connected! " & $PIntType(arg).val)
  
  proc testRead(s: PAsyncSocket, arg: PObject) =
    echo("Reading! " & $PIntType(arg).val)
    var data = s.getSocket.recv()
    if data == "":
      echo("Closing connection. " & $PIntType(arg).val)
      s.close()
    echo(data)
    echo("Finished reading! " & $PIntType(arg).val)

  proc testAccept(s: PAsyncSocket, arg: PObject) =
    echo("Accepting client! " & $PMyArg(arg).val)
    var (client, address) = s.acceptAddr()
    echo("Accepted ", address)
    client.handleRead = testRead
    var userArg: PIntType
    new(userArg)
    userArg.val = 78
    client.userArg = userArg
    PMyArg(arg).dispatcher.register(client)

  var d = newDispatcher()
  
  var userArg: PIntType
  new(userArg)
  userArg.val = 0
  var s = AsyncSocket(userArg = userArg)
  s.connect("amber.tenthbit.net", TPort(6667))
  s.handleConnect = testConnect
  s.handleRead = testRead
  d.register(s)
  
  var userArg1: PMyArg
  new(userArg1)
  userArg1.val = 1
  userArg1.dispatcher = d
  var server = AsyncSocket(userArg = userArg1)
  server.handleAccept = testAccept
  server.bindAddr(TPort(5555))
  server.listen()
  d.register(server)
  
  while d.poll(-1): nil