úÎJHw      experimental%Adam Langley <agl@imperialviolet.org>GA Connection uses the functions from a BaseConnection and wraps them a ) number of commonly needed behaviours. IFirstly, a write queue is introduced so that writes can be non-blocking. GSecondly, the Connection can manage a number of threads. Almost always L there will be a writer thread which is taking items from the write queue M and writing them to the BaseConnection. In addition, there can be zero or N more other threads managed by the Connection. If a thread which is managed M dies, by throwing an exception or otherwise, it will close the connection 1 and all other managed threads will be killed. IThere is also the concept of pushing data back into the Connection. This K is useful in a chain of reader functions where, for efficiency reasons, B you would want to read large blocks at a time, but the data is L self-deliminating so you would otherwise end up in a situation where you = had read too much. See the pushBack function for details. 5A BaseConnection abstracts a stream like connection. HRead, at most, the given number of bytes from the connection and return H a ByteString of the data. EOF is signaled by an exception and a zero / length string is never a valid return value GWrite the given ByteString to the connection. The write may write less J than the requested number of bytes (but must always write at least one  byte) Close a connection .Return a BaseConnection for the given socket. 5Create a new Connection from a BaseConnection object -the action to run when the connection closes 1the socket-like object to make a connection from KThis creates most of a Connection, purely in the STM monad. The Connection L returned from this must be passed to forkWriterThread, otherwise nothing  will ever get written. *the action run when the connection closes 1the socket-like object to make a connection from IIf you created the Connection in the STM monad using newSTM, you need to N call this on it in order to create the thread which processes the outgoing  queue. -the connection to fork the writer thread for KRun the given action, as if by forkIO, and manage the thread. If the given N action completes or throws an exception, the connection will be closed and , all other managed threads will be killed !the connection to close on death the action to run AWait for the given TVar to be true and then run the given action KNot all exceptions are safe to catch because of the way the GC works. If a  thread is killed because it'/s waiting on a TVar which is now garbage (e.g. M our writer thread when the Connection goes out of scope), all ForeignPtrs ( held by the thread are also garbage, at the same time. Thus we can end K up holding invalid ForeignPtrs if we catch unsafe exceptions and try to  cleanup. HWrap a connection thread so that, when the thread dies, it races to set J the dead flag. If it does so, it closes the socket and kills the other  threads Close a connection ;Enqueue a ByteString to a connection. This does not block. JBlock until the write queue has less than the given number of bytes in it " then enqueue a new ByteString. @the max number of bytes in the queue before we enqueue anything the connection to write to the data to enqueue FRead some number of bytes from a connection. The size is only a hint, ? the returned data may be shorter. A zero length read is EOF $Read exactly a give number of bytes JUnread some amount of data. It will be returned in the next call to read. JThe function pushes data to the front of the queue. Thus you need to push L all the data base in one go, or the order of future reads will be wrong. EThis might seem like an error, but consider the case of two actions: K the first reads 20 bytes and pushs back the last 10 of them. The second L reads 5 bytes and pushs back the last 4. If we appended to the push back N queue the second action would put those 4 bytes after the remaining 5 from  the first action. KAtomically take elements from the end of the given sequence and write them @ to the given socket. Throw an exception when the write fails data is removed from the end the write function GWrite a given number of bytes to a socket. This wraps a write function M which may write less than the requested number of bytes so that the whole + of the given ByteString is written out. the write function the data to write         network-connection-0.1.1Network.Connection ConnectionBaseConnectionbaseRead baseWrite baseClosebaseConnectionFromSocketnewnewSTMforkWriterThreadforkInConnectionclosewritewriteAtLowWaterreadreadapushBackconnbaseconnoutq connthreads connpushback conndeathconndead updateTVarwaitForReadySignal killThreads safeExceptionconnectionThreadWrapper seqToSocketwritea