socket-0.6.0.1: An extensible socket library.

Copyright(c) Lars Petersen 2015
LicenseMIT
Maintainerinfo@lars-petersen.net
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

System.Socket.Unsafe

Contents

Description

 

Synopsis

Socket Constructor

newtype Socket f t p Source

A generic socket type. Use socket to create a new socket.

The socket is just an MVar-wrapped file descriptor. The Socket constructor is exported trough the unsafe module in order to make this library easily extensible, but it is usually not necessary nor advised to work directly on the file descriptor. If you do, the following rules must be obeyed:

  • Make sure not to deadlock. Use withMVar or similar.
  • The lock must not be held during a blocking call. This would make it impossible to send and receive simultaneously or to close the socket.
  • The lock must be held when calling operations that use the file descriptor. Otherwise the socket might get closed or even reused by another thread/capability which might result in reading from or writing totally different connection. This is a security nightmare!
  • The socket is non-blocking and all the code relies on that assumption. You need to use GHC's eventing mechanism primitives to block until something happens. The former rules forbid to use threadWaitRead as it does not separate between registering the file descriptor (for which the lock must be held) and the actual waiting (for which you must not hold the lock). Also see this thread and read the library code to see how the problem is currently circumvented.

Constructors

Socket (MVar Fd) 

Socket Operations

unsafeSend

unsafeSendTo

unsafeReceive

unsafeReceiveFrom

Socket Options

unsafeGetSocketOption

unsafeSetSocketOption

unsafeSetSocketOption :: Storable a => Socket f t p -> CInt -> CInt -> a -> IO () Source

Waiting For Events

unsafeSocketWaitRead

unsafeSocketWaitRead Source

Arguments

:: Fd

Socket descriptor

-> Int

How many times has it been tried unsuccessfully so far? (currently only relevant on Windows)

-> IO (IO ())

The outer action registers the waiting, the inner does the actual wait.

Blocks until a socket should be tried for reading.

safeSocketWaitRead = do
  wait <- withMVar msock $ \sock-> do
    -- Register while holding a lock on the socket descriptor.
    unsafeSocketWaitRead sock 0 
  -- Do the waiting without keeping the socket descriptor locked.
  wait

unsafeSocketWaitWrite

Other Helpers

tryWaitRetryLoop

tryWaitRetryLoop :: Socket f t p -> (Fd -> Int -> IO (IO ())) -> (Fd -> IO CInt) -> IO CInt Source