diff options
Diffstat (limited to 'test/syntax/code/haskell')
-rw-r--r-- | test/syntax/code/haskell | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/syntax/code/haskell b/test/syntax/code/haskell new file mode 100644 index 00000000..acb83786 --- /dev/null +++ b/test/syntax/code/haskell @@ -0,0 +1,38 @@ +{-# LANGUAGE TypeSynonymInstances #-} +module Network.UDP +( DataPacket(..) +, openBoundUDPPort +, openListeningUDPPort +, pingUDPPort +, sendUDPPacketTo +, recvUDPPacket +, recvUDPPacketFrom +) where + +import qualified Data.ByteString as Strict (ByteString, concat, singleton) +import qualified Data.ByteString.Lazy as Lazy (ByteString, toChunks, fromChunks) +import Data.ByteString.Char8 (pack, unpack) +import Network.Socket hiding (sendTo, recv, recvFrom) +import Network.Socket.ByteString (sendTo, recv, recvFrom) + +-- Type class for converting StringLike types to and from strict ByteStrings +class DataPacket a where + toStrictBS :: a -> Strict.ByteString + fromStrictBS :: Strict.ByteString -> a + +instance DataPacket Strict.ByteString where + toStrictBS = id + {-# INLINE toStrictBS #-} + fromStrictBS = id + {-# INLINE fromStrictBS #-} + +openBoundUDPPort :: String -> Int -> IO Socket +openBoundUDPPort uri port = do + s <- getUDPSocket + bindAddr <- inet_addr uri + let a = SockAddrInet (toEnum port) bindAddr + bindSocket s a + return s + +pingUDPPort :: Socket -> SockAddr -> IO () +pingUDPPort s a = sendTo s (Strict.singleton 0) a >> return () |