Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serveUnix #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions msgpack-rpc/src/Network/MessagePack/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Network.MessagePack.Server (
method,
-- * Start RPC server
serve,
serveUnix,
) where

import Control.Applicative
Expand All @@ -50,6 +51,7 @@ import Data.Binary
import Data.Conduit
import qualified Data.Conduit.Binary as CB
import Data.Conduit.Network
import qualified Data.Conduit.Network.Unix as U
import Data.Conduit.Serialization.Binary
import Data.List
import Data.MessagePack
Expand Down Expand Up @@ -100,25 +102,36 @@ method :: MethodType m f
-> Method m
method name body = Method name $ toBody body

-- | Start RPC server with a set of RPC methods.
-- | Start an RPC server with a set of RPC methods on a TCP socket.
serve :: (MonadBaseControl IO m, MonadIO m, MonadCatch m, MonadThrow m)
=> Int -- ^ Port number
-> [Method m] -- ^ list of methods
-> m ()
serve port methods = runGeneralTCPServer (serverSettings port "*") $ \ad -> do
(rsrc, _) <- appSource ad $$+ return ()
(_ :: Either ParseError ()) <- try $ processRequests rsrc (appSink ad)
(_ :: Either ParseError ()) <- try $ processRequests methods rsrc (appSink ad)
return ()
where
processRequests rsrc sink = do
(rsrc', res) <- rsrc $$++ do
obj <- sinkGet get
case fromObject obj of
Nothing -> throwM $ ServerError "invalid request"
Just req -> lift $ getResponse (req :: Request)
_ <- CB.sourceLbs (pack res) $$ sink
processRequests rsrc' sink

-- | Start an RPC server with a set of RPC methods on a Unix domain socket.
serveUnix :: (MonadBaseControl IO m, MonadIO m, MonadCatch m, MonadThrow m)
=> FilePath -- ^ Socket path
-> [Method m] -- ^ list of methods
-> m ()
serveUnix path methods = liftBaseWith $ \run ->
U.runUnixServer (U.serverSettings path) $ \ad -> void . run $ do
(rsrc, _) <- appSource ad $$+ return ()
(_ :: Either ParseError ()) <- try $ processRequests methods rsrc (appSink ad)
return ()

processRequests methods rsrc sink = do
(rsrc', res) <- rsrc $$++ do
obj <- sinkGet get
case fromObject obj of
Nothing -> throwM $ ServerError "invalid request"
Just req -> lift $ getResponse (req :: Request)
_ <- CB.sourceLbs (pack res) $$ sink
processRequests methods rsrc' sink
where
getResponse (rtype, msgid, methodName, args) = do
when (rtype /= 0) $
throwM $ ServerError $ "request type is not 0, got " ++ show rtype
Expand Down
2 changes: 1 addition & 1 deletion msgpack/src/Data/MessagePack/Get.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Data.MessagePack.Get(
import Control.Applicative
import Control.Monad
import Data.Binary
import Data.Binary.Get
import Data.Binary.Get (getByteString, getWord16be, getWord16le, getWord32be, getWord64be)
import Data.Binary.IEEE754
import Data.Bits
import qualified Data.ByteString as S
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ packages:
# - msgpack-idl-web/
extra-deps:
- peggy-0.3.2
resolver: lts-2.15
resolver: nightly-2016-09-12