Hello all,

I have been playing with the chat-server example from the asyncnet module but I 
hit this strange data loss: if the 7.5s time-out kicks in, the subsequent 
clients' data is lost (seems to start receiving after a few send attempts 
within current 7 sec frame).

Pretty sure the OS (on Linux) buffers internally small data, so there should be 
no internal data discarding. If using buffered sockets then there is no data 
received at all?

Any ideas what may happen here? Cheers.
    
    
    import asyncnet, asyncdispatch, strutils, sequtils, oids
    
    var clients {.threadvar.}: seq[AsyncSocket]
    
    proc processClient(client: AsyncSocket, id: Oid) {.async.} =
      while true:
        echo "awaiting data from client: ", $id
        let dataEvt = client.recv(256)
        let readEvt = withTimeout(dataEvt, 7500)
        
        if await(readEvt) == false:
            echo "nothing yet"
            continue
        
        let line = dataEvt.read
        if line.len == 0:
            echo "socket gone"
            break
        
        if line.startsWith("quit"):
            client.close
            echo "has quit: ", $id
            for i,c in pairs(clients):
                if c == client:
                    clients.delete(i)
                    break
            break
        
        for c in clients:
          await c.send(line)
    
    proc serve() {.async.} =
      clients = @[]
      var server = newAsyncSocket(buffered = false)
      server.setSockOpt(OptReuseAddr, true)
      server.bindAddr(Port(12345))
      server.listen()
      
      while true:
        let client = await server.accept() # client will inherit .isBuffered
        clients.add client
        
        let id = genOid()
        asyncCheck processClient(client, id)
    
    asyncCheck serve()
    runForever()
    
    
    Run

Reply via email to