summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authordom96 <dominikpicheta@googlemail.com>2012-02-20 00:15:50 +0000
committerdom96 <dominikpicheta@googlemail.com>2012-02-20 00:15:50 +0000
commit38eafd30ae87463a2dc844d19b23c30ba2a8cd19 (patch)
treedb23a14f8f66c8911368faf19d734b9dc9a750c7 /lib/pure
parentb6ee92f384ecec0cb47533a20cfdbda488941569 (diff)
downloadNim-38eafd30ae87463a2dc844d19b23c30ba2a8cd19.tar.gz
In the IRC module the socket is now closed when EvDisconnected event is created.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/irc.nim15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/pure/irc.nim b/lib/pure/irc.nim
index 6e9e30281..8946a9d8f 100644
--- a/lib/pure/irc.nim
+++ b/lib/pure/irc.nim
@@ -119,6 +119,13 @@ proc part*(irc: var TIRC, channel, message: string) =
   ## Leaves ``channel`` with ``message``.
   irc.send("PART " & channel & " :" & message)
 
+proc close*(irc: var TIRC) =
+  ## Closes connection to an IRC server.
+  ##
+  ## **Warning:** This procedure does not send a ``QUIT`` message to the server.
+  irc.status = SockClosed
+  irc.sock.close()
+
 proc isNumber(s: string): bool =
   ## Checks if `s` contains only numbers.
   var i = 0
@@ -224,6 +231,7 @@ proc irc*(address: string, port: TPort = 6667.TPort,
 
 proc processLine(irc: var TIRC, line: string): TIRCEvent =
   if line.len == 0:
+    irc.close()
     result.typ = EvDisconnected
   else:
     result = parseMessage(line)
@@ -232,6 +240,7 @@ proc processLine(irc: var TIRC, line: string): TIRCEvent =
     if result.origin == irc.nick: result.origin = result.nick
 
     if result.cmd == MError:
+      irc.close()
       result.typ = EvDisconnected
       return
 
@@ -252,6 +261,7 @@ proc processOther(irc: var TIRC, ev: var TIRCEvent): bool =
     irc.send("PING :" & formatFloat(irc.lastPing), true)
 
   if epochTime() - irc.lastPong >= 120.0 and irc.lastPong != -1.0:
+    irc.close()
     ev.typ = EvDisconnected # TODO: EvTimeout?
     return true
   
@@ -276,7 +286,9 @@ proc poll*(irc: var TIRC, ev: var TIRCEvent,
   ## not need to be running many time critical tasks in the background. If you
   ## require this, use the asyncio implementation.
   
-  if not (irc.status == SockConnected): ev.typ = EvDisconnected
+  if not (irc.status == SockConnected):
+    # Do not close the socket here, it is already closed!
+    ev.typ = EvDisconnected
   var line = TaintedString""
   var socks = @[irc.sock]
   var ret = socks.select(timeout)
@@ -333,6 +345,7 @@ proc handleRead(h: PObject) =
       string(irc.lineBuffer).add(line.string)
   of RecvDisconnected:
     var ev: TIRCEvent
+    irc[].close()
     ev.typ = EvDisconnected
     irc.handleEvent(irc[], ev, irc.userArg)
   of RecvFail: nil