IC Python API:RLPy RUdpClient

From Reallusion Wiki!
Revision as of 00:00, 23 April 2020 by Chuck (RL) (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Main article: Modules.
Last modified: 04/23/2020

Description

This class is used to receive UDP data. The User Datagram Protocol allow sending messages across networks without connections. The RUdpClientis used to create to receive UDP messages. This class use queue to store the received data. The developer can set the buffer size for storing the maximum data counts by using SetMaximumDataCount, the default value is 60. When the total data counts received is greater than the maximum buffer counts, the previously received data will be removed. In order to fetch data, a specified index must be provided to get the data which developer wants in the buffer. Then a container must be created of the same size as the data, then data copied to the container by using GetDataSize and GetData.

Member Functions

Connect ( self, strIP, uPort )

Connect to a designated server. Returns RLPy.RStatus.Success when connected, however, this does not mean that the connection was successful, just means that a connection has been made. Upon receiving IP format error, returns a RLPy.RStatus.Failure.

See Also: IsConnected, Disconnect

Parameters

strIP [IN] IP address - string
uPort [IN] Port number - integer

Return

Success - RLPy.RStatus.Success
Failure - RLPy.RStatus.Failure
1 udp_client = RLPy.RUdpClient()
2 # connect to server
3 udp_client.Connect("127.0.0.1", 802)
4 print(udp_client.IsConnected())
5 # Disconnect from network
6 udp_client.Disconnect()

Disconnect ( self )

Disconnect from the server.

Return

Success - RLPy.RStatus.Success
Failure - RLPy.RStatus.Failure
1 udp_client = RLPy.RUdpClient()
2 # connect to server
3 udp_client.Connect("127.0.0.1", 802)
4 print(udp_client.IsConnected())
5 # Disconnect from network
6 udp_client.Disconnect()

IsConnected ( self )

Get the current connection status of the client.

Returns

True if is connected, else False - boolean
1 udp_client = RLPy.RUdpClient()
2 # connect to server
3 udp_client.Connect("127.0.0.1", 802)
4 print(udp_client.IsConnected())
5 # Disconnect from network
6 udp_client.Disconnect()

GetData ( self, pBuffer )

Get the latest data to buffer. Currently an iClone network system needs to first fetch the data size, create a byte array of an equal size, and use GetData to fill this byte array buffer.

Parameters

pBuffer [IN/OUT] buffer - string
 1 data = None
 2 udp_client = RLPy.RUdpClient()
 3   
 4 class NetworkEventCallback(RLPy.RUdpCallback):
 5    def __init__(self):
 6        RLPy.RUdpCallback.__init__(self)
 7       
 8    def OnDataReceived(self):
 9        global data
10        global udp_client
11        data = bytearray(udp_client.GetDataSize())       
12        udp_client.GetData(data) 
13        print(data)
14 
15 # connect to server
16 udp_client.Connect("127.0.0.1", 802)

GetDataAt ( self, nIndex, pBuffer )

Get the data to pBuffer according to the specific index. The size of the data for the given index must be pre-fetched in order to create a byte array of an identitical size; Then GetDataAt can be used to retrieve the data to fill this buffer. An index of 0 fetches the first (oldest) data.

Parameters

nIndex [IN] current data index in the buffer - integer
pBuffer [IN/OUT] buffer - string
 1 data = None
 2 udp_client = RLPy.RUdpClient()
 3   
 4 class NetworkEventCallback(RLPy.RUdpCallback):
 5    def __init__(self):
 6        RLPy.RUdpCallback.__init__(self)
 7       
 8    def OnDataReceived(self):
 9        global data
10        global udp_client
11        data = bytearray(udp_client.GetDataSize(0))
12        #index 0 => get the first come in data       
13        udp_client.GetDataAt(0, data)  
14        print(data)
15 
16 # connect to server
17 udp_client.Connect("127.0.0.1", 802)

GetDataCount ( self )

Get the counts of data in the buffer. The maximum value is determined by SetMaximumDataCount (default: 60).

Returns

The data count in the buffer - integer

1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success and data received
6 data_count = udp_client.GetDataCount()

GetDataSize ( self )

Get the latest data's size.

Returns

Latest data's size - integer

1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success and data received
6 data_size = udp_client.GetDataSize()
7 Print(data_size)

GetDataSize ( self, nIndex )

Get data's size according to the specific index. Return the data size for index 0 when given an improper index.

See Also: GetDataSize

Paremeters

nIndex [IN] current data index in the buffer - integer

Returns

The data size - integer

1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success and data received
6 data_size = udp_client.GetDataSize(0)
7 Print(data_size)

SetMaximumDataCount ( self, nCount )

Set buffer the maximum amount of received data (default: 60).

See Also: GetMaximumDataCount

Parameters

nCount [IN] buffer max count - integer
1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success and data received
6 udp_client.SetMaximumDataCount(100)
7 print(udp_client.GetMaximumDataCount()) # 100

GetMaximumDataCount ( self )

Get buffer max count.

See Also: SetMaximumDataCount

Returns

Buffer max count - integer

1 udp_client = RLPy.RUdpClient()
2 
3 # Connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # If connect success and data received
6 udp_client.SetMaximumDataCount(100)
7 print(udp_client.GetMaximumDataCount()) # 100

RegisterCallback ( self, pCallback )

Register network TCP event callback.

See Also: UnregisterCallback

Parameters

pCallback [IN] UDP callback instance - RTcpCallback
 1 data = None
 2 udp_client = RLPy.RUdpClient()
 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    def OnFailMessageReceived(self, fail_message):
11        print(fail_message)
12       
13    def OnDataReceived(self):
14        global data
15        global udp_client
16        data = bytearray(udp_client.GetDataSize(0))       
17  #index 0 => get the first come in data
18        udp_client.GetDataAt(0, data) 
19        print(data)
20 
21 # Network Event Callback
22 network_callback = NetworkEventCallback()
23 tcp_client.RegisterCallback(network_callback)
24 tcp_client.UnregisterCallback()

UnregisterCallback ( self )

Unregister network Udp event callback.

1 

SendData ( self, pBuffer, nDataSize )

Write data to the Server.

Parameters

pBuffer [IN] buffer - byte array
nDataSize [IN] Size of data - integer

Returns

True if send data success, else False - boolean

1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success
6 data = b'helloiClone'
7 data_size = len(data)
8 udp_client.SendData(bytearray(data), data_size)

JoinMulticastGroup ( self, strIP )

Join the multicast group at a specific IP address. Use Qt QUdpSocket::joinMulticastGroup mechanism to add more groups to the UDP client. The UDP client must enter bound state by being connected in the first place.

1 udp_client = RLPy.RUdpClient()
2 
3 # connect to server
4 udp_client.Connect("127.0.0.1", 802)
5 # if connect success
6 udp_client.JoinMulticastGroup( self.ip_address )