Difference between revisions of "IC Python API:RLPy RUdpClient"

From Reallusion Wiki!
Jump to: navigation, search
(Created page with "{{TOC}} {{Parent|IC_Python_API:RL_Python_Modules|Modules}} ==Detailed Description== This class is used to receive TCP/IP data. The Transmission Control Protocol provides relia...")
 
m
 
Line 1: Line 1:
 
{{TOC}}
 
{{TOC}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
 
{{Parent|IC_Python_API:RL_Python_Modules|Modules}}
==Detailed Description==
+
{{last_modified}}
This class is used to receive TCP/IP data.
+
 
The Transmission Control Protocol provides reliable, ordered, and
+
== Description ==
error-checked delivery of a stream of octets (bytes) between
+
 
applications running on hosts communicating via an IP network. The RTcpClient
+
This class is used to receive UDP data. The User Datagram Protocol allow sending messages across networks without connections. The [[IC_Python_API:RLPy_RUdpClient|RUdpClient]]is 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 ( self, nCount )|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 ( self )|GetDataSize]] and [[#GetData ( self, pBuffer )|GetData]].
is used to receive such data. This class use queue to store the
+
 
received data. The developer can set the buffer size for storing the
+
== Member Functions ==
maximum data counts by using SetMaximumDataCount(),
+
 
the default value is 60. When the total data counts received is greater
+
=== Connect ( self, strIP, uPort ) ===
than the maximum buffer counts, the previously received data will be
+
 
removed. Getting data have to specify index to get the data which
+
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'''.
developer wants in the buffer and create a container of the same size as
+
 
the data, then copy data to the container by using GetDataSize() and GetData().
+
See Also: [[#IsConnected ( self )|IsConnected]], [[#Disconnect ( self )|Disconnect]]
The following is an example to use this class: <syntaxhighlight lang="Python">
+
 
data = None
+
==== Parameters ====
tcp_client = RLPy.RTcpClient()
+
:'''strIP''' [IN] IP address - string
+
:'''uPort''' [IN] Port number - integer
class NetworkEventCallback(RLPy.RTcpCallback):
+
 
  def __init__(self):
+
==== Return ====
   RLPy.RTcpCallback.__init__(self)
+
:Success - RLPy.RStatus.Success
+
:Failure - RLPy.RStatus.Failure
  def OnStatusChanged(self, is_connected):
+
   print(is_connected)
+
+
  def OnFailMessageReceived(self, fail_message):
+
   print(fail_message)
+
  
+
  def OnDataReceived(self):
+
   global data
+
   global tcp_client
+
   data = bytearray(tcp_client.GetDataSize(0)) #index 0 => get the first come in data
+
   tcp_client.GetDataAt(0, data)
+
   print(data)
+
+
  
+
# Tcp Network
+
tcp_client.SetMaximumDataCount(100)
+
network_callback = NetworkEventCallback()
+
tcp_client.RegisterCallback(network_callback) # register network event callback
+
  
tcp_client.Connect("127.0.0.1", 802) # connect to server
+
<syntaxhighlight lang="python" line='line'>
print(tcp_client.IsConnected())
+
udp_client = RLPy.RUdpClient()
...
+
# connect to server
tcp_client.Disconnect()
+
udp_client.Connect("127.0.0.1", 802)
 +
print(udp_client.IsConnected())
 +
# Disconnect from network
 +
udp_client.Disconnect()
 
</syntaxhighlight>
 
</syntaxhighlight>
==Member Functions==
+
 
===Connect===
+
=== Disconnect ( self ) ===
<syntaxhighlight lang="Python">
+
 
RLPy.RTcpClient.Connect ( self, strIP, uPort )
+
Disconnect from the server.
 +
 
 +
==== Return ====
 +
:Success - RLPy.RStatus.Success
 +
:Failure - RLPy.RStatus.Failure
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
print(udp_client.IsConnected())
 +
# Disconnect from network
 +
udp_client.Disconnect()
 
</syntaxhighlight>
 
</syntaxhighlight>
Connect to server.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''strIP''' [IN] IP - string
+
=== IsConnected ( self ) ===
  
'''uPort''' [IN] Port - int
+
Get the current connection status of the client.
</div>
+
 
-----
+
==== Returns ====
===GetData===
+
:'''True''' if is connected, else '''False''' - boolean
<syntaxhighlight lang="Python">
+
 
RLPy.RTcpClient.GetData ( self, pBuffer )
+
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
print(udp_client.IsConnected())
 +
# Disconnect from network
 +
udp_client.Disconnect()
 
</syntaxhighlight>
 
</syntaxhighlight>
copy latest data to buffer
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''pBuffer''' [IN/OUT] buffer - string
+
=== GetData ( self, pBuffer ) ===
</div>
+
 
-----
+
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.
===GetDataAt===
+
 
<syntaxhighlight lang="Python">
+
==== Parameters ====
RLPy.RTcpClient.GetDataAt ( self, nIndex, pBuffer )
+
:'''pBuffer''' [IN/OUT] buffer - string
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
data = None
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
class NetworkEventCallback(RLPy.RUdpCallback):
 +
  def __init__(self):
 +
      RLPy.RUdpCallback.__init__(self)
 +
     
 +
  def OnDataReceived(self):
 +
      global data
 +
      global udp_client
 +
      data = bytearray(udp_client.GetDataSize())     
 +
      udp_client.GetData(data)
 +
      print(data)
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 
</syntaxhighlight>
 
</syntaxhighlight>
copy the data to pBuffer according to the specific index
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''pBuffer''' [IN/OUT] buffer - string
+
=== GetDataAt ( self, nIndex, pBuffer ) ===
  
'''nIndex''' [IN] current data index in the buffer - int
+
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.
</div>
+
 
-----
+
==== Parameters ====
===GetDataCount===
+
:'''nIndex''' [IN] current data index in the buffer - integer
<syntaxhighlight lang="Python">
+
:'''pBuffer''' [IN/OUT] buffer - string
RLPy.RTcpClient.GetDataCount ( self )
+
 
 +
<syntaxhighlight lang="python" line='line'>
 +
data = None
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
class NetworkEventCallback(RLPy.RUdpCallback):
 +
  def __init__(self):
 +
      RLPy.RUdpCallback.__init__(self)
 +
     
 +
  def OnDataReceived(self):
 +
      global data
 +
      global udp_client
 +
      data = bytearray(udp_client.GetDataSize(0))
 +
      #index 0 => get the first come in data     
 +
      udp_client.GetDataAt(0, data) 
 +
      print(data)
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 
</syntaxhighlight>
 
</syntaxhighlight>
Get the counts of data in the buffer.
+
 
====Returns====
+
=== GetDataCount ( self ) ===
<div style="margin-left: 2em;">The counts of data in the buffer - int
+
 
</div>
+
Get the counts of data in the buffer. The maximum value is determined by SetMaximumDataCount (default: 60).
-----
+
 
===GetDataSize===
+
==== Returns ====
<syntaxhighlight lang="Python">
+
The data count in the buffer - integer
RLPy.RTcpClient.GetDataSize ( self, args )
+
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success and data received
 +
data_count = udp_client.GetDataCount()
 
</syntaxhighlight>
 
</syntaxhighlight>
Get data's size according to the specific index.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''nIndex''' [IN] current data index in the buffer - int
+
=== GetDataSize ( self ) ===
</div>
+
 
====Returns====
+
Get the latest data's size.
<div style="margin-left: 2em;">Data size - int
+
 
</div>
+
==== Returns ====
-----
+
Latest data's size - integer
===GetMaximumDataCount===
+
 
<syntaxhighlight lang="Python">
+
<syntaxhighlight lang="python" line='line'>
RLPy.RTcpClient.GetMaximumDataCount ( self )
+
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success and data received
 +
data_size = udp_client.GetDataSize()
 +
Print(data_size)
 
</syntaxhighlight>
 
</syntaxhighlight>
return buffer's maximum amount
+
 
====Returns====
+
=== GetDataSize ( self, nIndex ) ===
<div style="margin-left: 2em;">Buffer's maximum count - int
+
 
</div>
+
Get data's size according to the specific index.  Return the data size for index 0 when given an improper index.
-----
+
 
===IsConnected===
+
See Also: [[#GetDataSize ( self )|GetDataSize]]
<syntaxhighlight lang="Python">
+
 
RLPy.RTcpClient.IsConnected ( self )
+
==== Paremeters ====
 +
:'''nIndex''' [IN] current data index in the buffer - integer
 +
 
 +
==== Returns ====
 +
The data size - integer
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success and data received
 +
data_size = udp_client.GetDataSize(0)
 +
Print(data_size)
 
</syntaxhighlight>
 
</syntaxhighlight>
Get the current connection status of the client.
+
 
====Returns====
+
=== SetMaximumDataCount ( self, nCount ) ===
<div style="margin-left: 2em;">True if is connecting or False if disconnect - bool
+
 
</div>
+
Set buffer the maximum amount of received data (default: 60).
-----
+
 
===RegisterCallback===
+
See Also: [[#GetMaximumDataCount ( self )|GetMaximumDataCount]]
<syntaxhighlight lang="Python">
+
 
RLPy.RTcpClient.RegisterCallback ( self, pCallback )
+
==== Parameters ====
 +
:'''nCount''' [IN] buffer max count - integer
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success and data received
 +
udp_client.SetMaximumDataCount(100)
 +
print(udp_client.GetMaximumDataCount()) # 100
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=== GetMaximumDataCount ( self ) ===
 +
 +
Get buffer max count.
 +
 +
See Also: [[#SetMaximumDataCount ( self, nCount )|SetMaximumDataCount]]
 +
 +
==== Returns ====
 +
Buffer max count - integer
 +
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 +
# Connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# If connect success and data received
 +
udp_client.SetMaximumDataCount(100)
 +
print(udp_client.GetMaximumDataCount()) # 100
 +
</syntaxhighlight>
 +
 +
=== RegisterCallback ( self, pCallback ) ===
 +
 
Register network TCP event callback.
 
Register network TCP event callback.
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''pCallback''' [IN] RTcpCallback - RLPy.RTcpCallback
+
See Also: [[#UnregisterCallback ( self )|UnregisterCallback]]
</div>
+
 
-----
+
==== Parameters ====
===SendData===
+
:'''pCallback''' [IN] UDP callback instance - [[IC_Python_API:RLPy_RTcpCallback|RTcpCallback]]
<syntaxhighlight lang="Python">
+
 
RLPy.RTcpClient.SendData ( self, pBuffer, nDataSize )
+
<syntaxhighlight lang="python" line='line'>
 +
data = None
 +
udp_client = RLPy.RUdpClient()
 +
class NetworkEventCallback(RLPy.RUdpCallback):
 +
  def __init__(self):
 +
      RLPy.RUdpCallback.__init__(self)
 +
 
 +
  def OnStatusChanged(self, is_connected):
 +
      print(is_connected)
 +
     
 +
  def OnFailMessageReceived(self, fail_message):
 +
      print(fail_message)
 +
     
 +
  def OnDataReceived(self):
 +
      global data
 +
      global udp_client
 +
      data = bytearray(udp_client.GetDataSize(0))     
 +
#index 0 => get the first come in data
 +
      udp_client.GetDataAt(0, data)
 +
      print(data)
 +
 
 +
# Network Event Callback
 +
network_callback = NetworkEventCallback()
 +
tcp_client.RegisterCallback(network_callback)
 +
tcp_client.UnregisterCallback()
 
</syntaxhighlight>
 
</syntaxhighlight>
Write data to the device.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''pBuffer''' [IN] buffer - string
+
=== UnregisterCallback ( self ) ===
  
'''nDataSize''' [IN] Size of data - int
+
Unregister network Udp event callback.
</div>
+
 
====Returns====
+
<syntaxhighlight lang="python" line='line'>
<div style="margin-left: 2em;">True if send data success or False if send data fail - bool
+
</div>
+
-----
+
===SetMaximumDataCount===
+
<syntaxhighlight lang="Python">
+
RLPy.RTcpClient.SetMaximumDataCount ( self, nCount )
+
 
</syntaxhighlight>
 
</syntaxhighlight>
Set buffer the maximum amount of received data.
 
====Parameters====
 
<div style="margin-left: 2em;">
 
  
'''nCount''' [IN] buffer's maximum count - int
+
=== SendData ( self, pBuffer, nDataSize ) ===
</div>
+
 
 +
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
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success
 +
data = b'helloiClone'
 +
data_size = len(data)
 +
udp_client.SendData(bytearray(data), data_size)
 +
</syntaxhighlight>
 +
 
 +
=== 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.
 +
 
 +
<syntaxhighlight lang="python" line='line'>
 +
udp_client = RLPy.RUdpClient()
 +
 
 +
# connect to server
 +
udp_client.Connect("127.0.0.1", 802)
 +
# if connect success
 +
udp_client.JoinMulticastGroup( self.ip_address )
 +
</syntaxhighlight>

Latest revision as of 01:00, 23 April 2020

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 )