如何通过(异步)网络通信处理消息响应 (VB.NET)

发布于 2024-07-29 04:11:15 字数 2510 浏览 9 评论 0原文

我正在编写一个应用程序,它通过网络将消息发送到另一台处理该消息并发回回复的电脑(例如布尔值或其他一些数据)。 我的网络通信子系统(基于 WCF)使我能够向另一台 PC 发送消息并处理任何收到的消息。

问题是,收到的任何响应都由我指定的网络消息事件处理程序子程序处理,但我无法将其反馈给调用发送原始消息的子程序。

为了快速说明,情况是这样的:

Private Function NetSendMessage(ByVal message As String) As Boolean 
    'Send network message to PC with device connected 
    '.... 
    'returns true if message sent 
End Function 

Private Sub NetMessageReceived(ByVal sender As Object, ByVal e As MessageEventArgs) Handles NetComm.OnReceiveMessage 
    'Handles incoming messages 
    '.... 
End Sub 

Private Function MoveForward() As Boolean 
    Return (MoveLeftWheel() And MoveRightWheel()) 
End Function 

Private Function MoveLeftWheel() As Boolean 
    If NetSendMessage("MoveLeftWheel") = False Then Return False 

    'Network message went through, now we need to know if the command was executed, which the remote PC will tell us 
    Return ______  *(here is the trouble-- how do I get the boolean response from the other side as to whether this worked or not?)*
End Function 

Private Function MoveRightWheel() As Boolean 
    If NetSendMessage("MoveRightWheel") = False Then Return False 

    Return ______ 
End Function

到目前为止我想到的解决方案比这可能需要的更复杂。

我的一个想法是添加一个 ID 号来标识每条消息(另一方将在其响应中包含该 ID 号),然后我将拥有一个用于“消息队列”的 ArrayList。 NetMessageReceived 子组件会将任何新消息添加到此 ArrayList,并且任何需要回复的函数都会监视队列以查看其响应是否到达。 它会是这样的:

Private NetMessageQueue as New ArrayList 
Private LatestMessageID as Integer 

Private Sub NetMessageReceived(ByVal sender As Object, ByVal e As MessageEventArgs) Handles NetComm.OnReceiveMessage 
    'Handles incoming messages 
    NetMessageQueue.Add(e.Message.ToString) 
End Sub 

Private Function MoveLeftWheel() As Boolean 
    'Send network message to robot PC 

    Private MyMessageID as Integer = LatestMessageID + 1 

    NetSendMessage(MyMessageID & "|" & "MoveLeftWheel") 

    Do 
            For Each msg As String in NetMessageQueue 
                    If msg.Split("|")(0) = MyMessageID.ToString Then 
                            'This is the response message we're waiting for 
                            Return Boolean.Parse(msg.Split("|")(1)) 
                    End If 
            Next 
    Loop 
End Function 

这似乎是一种非常麻烦/不必要的繁重的方式来做到这一点。 我正在考虑添加一个 NetSendReceive 函数,比如说 MoveRightWheel 可以调用的函数; NetSendReceive将调用NetSendMessage,监视消息队列的响应(可能通过委托/IAsyncResult),然后返回响应消息(到MoveRightWheel,它将等待它)。

可能有更好的方法来做到这一点 - 有什么想法吗?

I'm writing an app that sends messages over a network to another PC that processes the message and sends back a reply (e.g. a boolean or some other data). My network communication subs (based on WCF) just give me the ability to send a message to the other PC and process any received messages.

The issue is that any response received is processed by my designated network message event handler sub, but I have no way of feeding it back to the sub that called for the original message to be sent.

To quickly illustrate, the situation is something like this:

Private Function NetSendMessage(ByVal message As String) As Boolean 
    'Send network message to PC with device connected 
    '.... 
    'returns true if message sent 
End Function 

Private Sub NetMessageReceived(ByVal sender As Object, ByVal e As MessageEventArgs) Handles NetComm.OnReceiveMessage 
    'Handles incoming messages 
    '.... 
End Sub 

Private Function MoveForward() As Boolean 
    Return (MoveLeftWheel() And MoveRightWheel()) 
End Function 

Private Function MoveLeftWheel() As Boolean 
    If NetSendMessage("MoveLeftWheel") = False Then Return False 

    'Network message went through, now we need to know if the command was executed, which the remote PC will tell us 
    Return ______  *(here is the trouble-- how do I get the boolean response from the other side as to whether this worked or not?)*
End Function 

Private Function MoveRightWheel() As Boolean 
    If NetSendMessage("MoveRightWheel") = False Then Return False 

    Return ______ 
End Function

The solutions I've thought of so far are more complex than this probably needs to be.

One idea I had was to add an ID number to identify each message (which the other side would include in its response), and then I'd have an ArrayList for a "message queue." The NetMessageReceived sub would add any new message to this ArrayList, and any function that wants a reply would monitor the queue to see if its response arrived. It'd be something like this:

Private NetMessageQueue as New ArrayList 
Private LatestMessageID as Integer 

Private Sub NetMessageReceived(ByVal sender As Object, ByVal e As MessageEventArgs) Handles NetComm.OnReceiveMessage 
    'Handles incoming messages 
    NetMessageQueue.Add(e.Message.ToString) 
End Sub 

Private Function MoveLeftWheel() As Boolean 
    'Send network message to robot PC 

    Private MyMessageID as Integer = LatestMessageID + 1 

    NetSendMessage(MyMessageID & "|" & "MoveLeftWheel") 

    Do 
            For Each msg As String in NetMessageQueue 
                    If msg.Split("|")(0) = MyMessageID.ToString Then 
                            'This is the response message we're waiting for 
                            Return Boolean.Parse(msg.Split("|")(1)) 
                    End If 
            Next 
    Loop 
End Function 

This seems like a very cumbersome / unnecessarily taxing way to do this. I was thinking along the lines of maybe add a NetSendReceive function that, say, MoveRightWheel could call; NetSendReceive would call NetSendMessage, monitor the message queue for its response (perhaps via a delegate/IAsyncResult), and then return the response message (to MoveRightWheel, which would be waiting for it).

There's probably a better way to do this- any ideas?

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

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

发布评论

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

评论(1

回忆那么伤 2024-08-05 04:11:15

如果您将所有这些都封装在一个类中怎么办? 那么想法是在每个实例中只有一个“NetComm”对象。 假设您调用类 MessagingUtility。 每当您想发送新消息时,您都必须拨打电话

Dim myMessage As New MessagingUtility()
myMessage.NetSendMessage("Hello World")

或类似的电话。

当许多其他消息大约在同一时间发送时,这将防止您丢失消息上下文的可能性。 因为毕竟,您想要的给定消息的每个属性都将整齐地包含在 myMessage 对象中。

What if you wrap all of this in a class? Then the idea would be that you'd only have one "NetComm" object in each instance. Say you call the class MessagingUtility. And whenever you want to send a new message you'll have to call

Dim myMessage As New MessagingUtility()
myMessage.NetSendMessage("Hello World")

Or something like that.

This will prevent any possibility of you losing track of the context of a message when a lot of other messages are being sent around the same time. Because after all, every property of a given message you'll ever want will be contained neatly in the myMessage object.

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