Copyright | (c) Francisco Casas Barrientos, 2017 |
---|---|
License | MIT |
Maintainer | franciscojacb@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
The main module contains all the operations needed to create a channel and send or receive packages.
import SecureUDP import qualified Network.Socket as So import qualified System.IO as SI main = do -- This is needed so lazyness doens't mess with inner thread communication. SI.hSetBuffering SI.stdout SI.NoBuffering -- Create the socket: sock <- So.socket So.AF_INET So.Datagram So.defaultProtocol -- Bind the socket to the address 0.0.0.0 so it can receive packages from everywhere. let port = 7272 let address = So.tupleToHostAddress (0,0,0,0) So.bind sock (So.SockAddrInet port address) -- Create the configuration for a new channel, -- this values should be OK for most purposes: let chcfg = ChannelConfig { socket = sock, resendTimeout = 280000000000, -- 0.28 seconds. maxResends = 8, allowed = (_ -> return (True)), -- Allow any incomming address. maxPacketSize = 500, recvRetention = 10 } -- Start the channel with the given configuration: channel <- startChannel chcfg -- ...
Then you can use the channel to send and receive messages from and to any address, using
sendMessages
and getReceived
.
You can also use getLoss
to check which messages that you sent weren't ACKed.
Make sure that you call getReceived
and getLoss
once in a while, even if you don't use them,
so the messages won't accumulate in memory.
- data ChannelConfig = ChannelConfig {
- socket :: Socket
- resendTimeout :: Integer
- maxResends :: Int
- allowed :: SockAddr -> IO Bool
- maxPacketSize :: Int
- recvRetention :: Integer
- startChannel :: ChannelConfig -> IO ChannelSt
- closeChannel :: ChannelSt -> IO ()
- checkClosed :: ChannelSt -> IO Bool
- channelConf :: ChannelSt -> ChannelConfig
- getReceived :: ChannelSt -> IO [(SockAddr, ByteString)]
- getLoss :: ChannelSt -> IO [(SockAddr, ByteString)]
- sendMessages :: ChannelSt -> [(SockAddr, ByteString)] -> IO Bool
- type ChannelSt = (ChannelConfig, MVar ChannelStatus)
Channel configuration
data ChannelConfig Source
Holds the configuration of a channel.
ChannelConfig | |
|
Channel control
startChannel :: ChannelConfig -> IO ChannelSt Source
Starts a sending and a receiving threads for the protocol, returns a channel that can be used to insert and extract messages.
closeChannel :: ChannelSt -> IO () Source
Terminates a channel, ending its threads and making it unable to send or receive messages.
checkClosed :: ChannelSt -> IO Bool Source
Check if the given channel has been closed.
channelConf :: ChannelSt -> ChannelConfig Source
The configuration that was used to start the given channel.
Channel manipulation
getReceived :: ChannelSt -> IO [(SockAddr, ByteString)] Source
Get the received messages and their sender addresses, then erases them.
It's important that your program calls this once in a while or the packages will remain in memory.
getLoss :: ChannelSt -> IO [(SockAddr, ByteString)] Source
Get the messages that weren't ACKed from the target recipent host and erases them. Useful to detect missing connections.
It's also important that your program calls this once in a while or the packages will remain in memory.
sendMessages :: ChannelSt -> [(SockAddr, ByteString)] -> IO Bool Source
Trought the given channel, send packages to the given addresses and message bytestrings.
Returns False
if the channel has being closed, True
otherwise.
type ChannelSt = (ChannelConfig, MVar ChannelStatus) Source
Represents the status of a channel, also holds, internally, information about the threads that it uses. After a channel is created, it is used as an argument for the main functions.