使用 Haskell 和 Thrift 进行 Ping Pong 时遇到困难

发布于 2024-12-17 11:03:22 字数 1312 浏览 3 评论 0原文

我正在尝试使用 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)

I'm trying to do a simple Ping Pong using Haskell and Thrift. However, it does only one repetition and then it gets stuck. I assume that the problem is in the (in)correct usage of Thrift rather than in Haskell. Probably something is not flushed correctly. Is there anyone with experience with Thrift who could help me making an educated guess on how to fix this?

Server:

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"

Client:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-12-24 11:03:22

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

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

Your problem is you're returning False from serverFunc. Haskell will loop until you return false (by your code, only once)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文