项目作者: jiowcl

项目描述 :
ZMQ Wrapper for FreeBasic Programming Language
高级语言: FreeBasic
项目地址: git://github.com/jiowcl/FreeBasicZMQ.git
创建时间: 2021-06-18T11:42:07Z
项目社区:https://github.com/jiowcl/FreeBasicZMQ

开源协议:MIT License

下载


FreeBasicZMQ

ZMQ Wrapper for FreeBasic Programming Language.

GitHub
FreeBasic

Environment

  • Windows 7 above (recommend)
  • FreeBasic 1.10.1 above (recommend)
  • ZeroMQ

How to Build

Building requires FreeBasic Compiler and test under Windows 10.

Example

Publisher Server

  1. fbc PubServer.bas -target win64
  1. #Include "../../Core/Enums.bi"
  2. #Include "../../Core/ZeroMQWrapper.bi"
  3. Dim lpszLibZmqDll As String = "libzmq.dll"
  4. ' Rnd with Range
  5. ' Source Code from: https://documentation.help/FreeBASIC/KeyPgRnd.html
  6. Function RndRange(Byval first As Double, Byval last As Double) As Double
  7. Function = Rnd * (last - first) + first
  8. End Function
  9. Const lpszServerAddr As String = "tcp://*:1689"
  10. Dim ZmqContextRec As LibZmqContext
  11. Dim ZmqSocketRec As LibZmqSocket
  12. Dim ZmqHelperRec As LibZmqHelper
  13. If LibZMQWrapper.DllOpen(lpszLibZmqDll) Then
  14. Dim Context As Any Ptr = ZmqContextRec.NewCtx()
  15. Dim Socket As Any Ptr = ZmqSocketRec.Socket(Context, ZMQ_PUB)
  16. Dim Rc As Long = ZmqSocketRec.Bind(Socket, lpszServerAddr)
  17. Print("Bind an IP address: " & lpszServerAddr)
  18. Randomize
  19. While 1
  20. Dim lpszRecvBufferPtr As Any Ptr = CAllocate(32)
  21. Dim lpszSendBufferPtr As ZString Ptr
  22. Dim lpszTopicBufferPtr As ZString Ptr
  23. Dim lpszTopic As String = "quotes"
  24. Dim lpszSendMessage As String = "Bid: " & Str(RndRange(9000, 1000)) & ",Ask:" + Str(RndRange(9000, 1000))
  25. ZmqSocketRec.Recv(Socket, lpszRecvBufferPtr, 32, 0)
  26. Sleep(2)
  27. Dim lpszReturnMessage As String = *CPtr(Zstring Ptr, lpszRecvBufferPtr)
  28. If lpszReturnMessage <> "" Then
  29. Print("Received: ")
  30. Print(lpszReturnMessage)
  31. End If
  32. lpszTopicBufferPtr = CAllocate(Len(lpszTopic), SizeOfDefZStringPtr(lpszTopicBufferPtr))
  33. *lpszTopicBufferPtr = lpszTopic
  34. lpszSendBufferPtr = CAllocate(Len(lpszSendMessage), SizeOfDefZStringPtr(lpszSendBufferPtr))
  35. *lpszSendBufferPtr = lpszSendMessage
  36. ZmqSocketRec.Send(Socket, lpszTopicBufferPtr, Len(lpszTopic), ZMQ_SNDMORE)
  37. ZmqSocketRec.Send(Socket, lpszSendBufferPtr, Len(lpszSendMessage), 0)
  38. Deallocate(lpszRecvBufferPtr)
  39. Deallocate(lpszSendBufferPtr)
  40. Deallocate(lpszTopicBufferPtr)
  41. lpszRecvBufferPtr = 0
  42. lpszSendBufferPtr = 0
  43. lpszTopicBufferPtr = 0
  44. Wend
  45. ZmqSocketRec.Close(Socket)
  46. ZmqContextRec.Shutdown(Context)
  47. LibZMQWrapper.DllClose()
  48. End If

Subscribe Client

  1. fbc SubClient.bas -target win64
  1. #Include "../../Core/Enums.bi"
  2. #Include "../../Core/ZeroMQWrapper.bi"
  3. Dim lpszCurrentDir As String = Curdir()
  4. Dim lpszLibZmqDll As String = "libzmq.dll"
  5. Const lpszServerAddr As String = "tcp://localhost:1689"
  6. Dim ZmqContextRec As LibZmqContext
  7. Dim ZmqSocketRec As LibZmqSocket
  8. Dim ZmqHelperRec As LibZmqHelper
  9. If LibZMQWrapper.DllOpen(lpszLibZmqDll) Then
  10. Dim Context As Any Ptr = ZmqContextRec.NewCtx()
  11. Dim Socket As Any Ptr = ZmqSocketRec.Socket(Context, ZMQ_SUB)
  12. Dim Rc As Long = ZmqSocketRec.Connect(Socket, lpszServerAddr)
  13. Dim lpszSubscribePtr As ZString Ptr
  14. Dim lpszSubscribe As String = "quotes"
  15. lpszSubscribePtr = CAllocate(Len(lpszSubscribe), SizeOfDefZStringPtr(lpszSubscribePtr))
  16. *lpszSubscribePtr = lpszSubscribe
  17. ZmqSocketRec.Setsockopt(Socket, ZMQ_SUBSCRIBE, lpszSubscribePtr, Len(lpszSubscribe))
  18. While 1
  19. Dim lpszTopicBufferPtr As Any Ptr = CAllocate(32)
  20. Dim lpszRecvBufferPtr As Any Ptr = CAllocate(64)
  21. ZmqSocketRec.Recv(Socket, lpszTopicBufferPtr, 32, 0)
  22. ZmqSocketRec.Recv(Socket, lpszRecvBufferPtr, 64, 0)
  23. Print(*CPtr(Zstring Ptr, lpszRecvBufferPtr))
  24. Deallocate(lpszTopicBufferPtr)
  25. Deallocate(lpszRecvBufferPtr)
  26. lpszTopicBufferPtr = 0
  27. lpszRecvBufferPtr = 0
  28. Sleep(2)
  29. Wend
  30. Deallocate(lpszSubscribePtr)
  31. lpszSubscribePtr = 0
  32. ZmqSocketRec.Close(Socket)
  33. ZmqContextRec.Shutdown(Context)
  34. LibZMQWrapper.DllClose()
  35. End If

License

Copyright (c) 2017-2024 Ji-Feng Tsai.
Code released under the MIT license.

TODO

  • More examples

Donation

If this application help you reduce time to coding, you can give me a cup of coffee :)

paypal

Paypal Me