Ping Pong与Haskell和Thrift陷入困境

时间:2011-11-23 17:20:17

标签: haskell thrift

我正在尝试使用Haskell和Thrift做一个简单的Ping Pong。但是,它只会重复一次,然后就会卡住。我假设问题出在正确使用Thrift而不是Haskell中。可能没有正确刷新的东西。是否有任何有Thrift经验的人可以帮助我对如何解决这个问题做出有根据的猜测?

服务器

echorequest :: TXT
echorequest = TXT {
    f_TXT_anytxt = Just "Ping"
    }

echoreply :: TXT
echoreply = TXT {
    f_TXT_anytxt = Just "Pong"
    }

serverFunc :: a -> (BinaryProtocol Handle, BinaryProtocol Handle)
              -> IO Bool
serverFunc a (h1,h2) = do
  let t1 = getTransport h1
  dat <- read_TXT h1
-- the following two lines are only for debugging
  putStrLn "Recieved data:"
  print dat
  write_TXT h1 echoreply
  tFlush t1
-- the following line is for debugging
  putStrLn "Data written"

  return False


main :: IO ()
main = do
   runBasicServer () serverFunc 4390
   putStrLn "Server stopped"

客户端:

main :: IO ()
main = do
  h <- connectTo "127.0.0.1" $ PortNumber 4390
  let proto = BinaryProtocol h
  putStrLn "Client started"
  let tryOnePing c i = do
      write_TXT proto echorequest
      putStrLn "ping sent"
      tFlush h
      w <- read_TXT proto
      putStrLn "pong received"
      return $ if w == echoreply then c+1 else c
  c <- foldM tryOnePing 0 [0 .. 1000]
  tClose h
  print (c, 10000 - c)

1 个答案:

答案 0 :(得分:1)

你的问题是你从serverFunc返回False。 Haskell将循环,直到您返回false(通过您的代码,只返回一次)

http://hackage.haskell.org/packages/archive/thrift/0.6.0/doc/html/src/Thrift-Server.html#line-65