IC Python API:RLPy RUdpCallback
- Main article: Modules.
- Last modified: 04/22/2020
Description
This class is used to monitor UDP connection status and recieve connection data.
Inheritance
RCallback> RUdpCallback
Member Functions
OnStatusChanged ( self, bIsConnected )
The application will call this function when there is a change in connection status; In which case, the new status message will be given.
Parameters
- bIsConnected [OUT] connected or not - bool
1 udp_client = RLPy.RUdpClient()
2
3 class NetworkEventCallback(RLPy.RUdpCallback):
4 def __init__(self):
5 RLPy.RUdpCallback.__init__(self)
6
7 def OnStatusChanged(self, is_connected):
8 print(is_connected)
9
10 network_callback = NetworkEventCallback()
11 # register network event callback
12 udp_client.RegisterCallback(network_callback)
13 # connect to server
14 udp_client.Connect("127.0.0.1", 802)
15 # if connect success, will print "True", otherwise print "False"
OnDataReceived ( self )
The application will call this function when a connection has been made and once the data has been received. Once this has been achieved, RLPy.RTcpClient.GetDataSize() can be used to establish a sized buffer ready to recieve the incoming data via RLPy.RTcpClient.GetDataAt().
See Also: RUdpClient, RLPy.RUdpClient.GetData(), RLPy.RUdpClient.GetDataAt(), RLPy.RUdpClient.GetDataSize()
1 udp_client = RLPy.RUdpClient()
2
3 class NetworkEventCallback(RLPy.RUdpCallback):
4 def __init__(self):
5 RLPy.RUdpCallback.__init__(self)
6
7 def OnDataReceived(self):
8 global data
9 global tcp_client
10 data = bytearray(tcp_client.GetDataSize(0))
11 #index 0 => get the first come in data
12 tcp_client.GetDataAt(0, data)
13 print(data)
14
15 network_callback = NetworkEventCallback()
16 # register network event callback
17 udp_client.RegisterCallback(network_callback)
18 # connect to server
19 udp_client.Connect("127.0.0.1", 802)
20 # if connect success, will print "True", otherwise print "False"
OnFailMessageReceived ( self, pErrorMsg )
This function is called by the application in the event that a connection can not be established or a connection error has occurred; In which case, an error message will be given.
Parameters
- pErrorMsg [IN] connect fail error message - string
1 udp_client = RLPy.RUdpClient()
2
3 class NetworkEventCallback(RLPy.RUdpCallback):
4 def __init__(self):
5 RLPy.RUdpCallback.__init__(self)
6
7 def OnFailMessageReceived(self, fail_message):
8 print(fail_message)
9
10 network_callback = NetworkEventCallback()
11 # register network event callback
12 udp_client.RegisterCallback(network_callback)
13 # connect to server
14 udp_client.Connect("127.0.0.1", 802)
15 # if connect failed, will print "Couldn't connect to server."