简体   繁体   English

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

[英]How to handle message responses over (async) network communication (VB.NET)

I'm writing an app that sends messages over a network to another PC that processes the message and sends back a reply (eg a boolean or some other data). 我正在编写一个应用程序,该应用程序通过网络将消息发送到另一台处理该消息并发送回响应的PC(例如,布尔值或某些其他数据)。 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. 我的网络通信用户(基于WCF)仅使我能够向另一台PC发送消息并处理任何接收到的消息。

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." 我的一个想法是添加一个ID号以标识每条消息(另一边将在响应中包括该消息),然后我将获得一个“消息队列”的ArrayList。 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. NetMessageReceived子例程会将任何新消息添加到此ArrayList,并且任何需要回复的函数都将监视队列以查看其响应是否到达。 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函数,例如MoveRightWheel可以调用它; 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). NetSendReceive将调用NetSendMessage,监视消息队列中的响应(可能通过委托/ IAsyncResult),然后将响应消息返回到(将等待它的MoveRightWheel)。

There's probably a better way to do this- any ideas? 这样做可能是更好的方法-有什么想法吗?

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. 这样的想法是,每个实例中只有一个“ NetComm”对象。 Say you call the class MessagingUtility. 假设您将类称为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. 因为毕竟,您想要的给定消息的每个属性都将被巧妙地包含在myMessage对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM