Difference between revisions of "IC Python API:RLPy RUdpClient"
Chuck (RL) (Talk | contribs) (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...") |
Chuck (RL) (Talk | contribs) m |
||
Line 1: | Line 1: | ||
{{TOC}} | {{TOC}} | ||
{{Parent|IC_Python_API:RL_Python_Modules|Modules}} | {{Parent|IC_Python_API:RL_Python_Modules|Modules}} | ||
− | == | + | {{last_modified}} |
− | This class is used to receive | + | |
− | The | + | == Description == |
− | + | ||
− | + | 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 | + | |
− | 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. | + | 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 | + | |
− | the data, then | + | See Also: [[#IsConnected ( self )|IsConnected]], [[#Disconnect ( self )|Disconnect]] |
− | + | ||
− | + | ==== Parameters ==== | |
− | + | :'''strIP''' [IN] IP address - string | |
− | + | :'''uPort''' [IN] Port number - integer | |
− | + | ||
− | + | ==== Return ==== | |
− | + | :Success - RLPy.RStatus.Success | |
− | + | :Failure - RLPy.RStatus.Failure | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | <syntaxhighlight lang="python" line='line'> | |
− | print( | + | 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> | ||
− | == | + | |
− | === | + | === Disconnect ( self ) === |
− | <syntaxhighlight lang=" | + | |
− | RLPy. | + | 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> | ||
− | |||
− | |||
− | |||
− | + | === IsConnected ( self ) === | |
− | + | Get the current connection status of the client. | |
− | + | ||
− | + | ==== Returns ==== | |
− | === | + | :'''True''' if is connected, else '''False''' - boolean |
− | <syntaxhighlight lang=" | + | |
− | RLPy. | + | <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> | ||
− | |||
− | |||
− | |||
− | '''pBuffer''' [IN/OUT] buffer - string | + | === 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. | |
− | + | ||
− | <syntaxhighlight lang=" | + | ==== Parameters ==== |
− | RLPy. | + | :'''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> | ||
− | |||
− | |||
− | |||
− | + | === GetDataAt ( self, nIndex, pBuffer ) === | |
− | '''nIndex''' [IN] current data index in the buffer - | + | 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 | |
− | <syntaxhighlight lang=" | + | :'''pBuffer''' [IN/OUT] buffer - string |
− | RLPy. | + | |
+ | <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 ) === |
− | + | ||
− | + | Get the counts of data in the buffer. The maximum value is determined by SetMaximumDataCount (default: 60). | |
− | + | ||
− | + | ==== Returns ==== | |
− | <syntaxhighlight lang=" | + | The data count in the buffer - integer |
− | RLPy. | + | |
+ | <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> | ||
− | |||
− | |||
− | |||
− | + | === GetDataSize ( self ) === | |
− | + | ||
− | === | + | Get the latest data's size. |
− | + | ||
− | + | ==== Returns ==== | |
− | + | Latest data's size - integer | |
− | === | + | |
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="python" line='line'> |
− | RLPy. | + | 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> | ||
− | + | ||
− | ==== | + | === 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 ( self )|GetDataSize]] |
− | <syntaxhighlight lang=" | + | |
− | RLPy. | + | ==== 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> | ||
− | + | ||
− | === | + | === SetMaximumDataCount ( self, nCount ) === |
− | + | ||
− | + | Set buffer the maximum amount of received data (default: 60). | |
− | + | ||
− | === | + | See Also: [[#GetMaximumDataCount ( self )|GetMaximumDataCount]] |
− | <syntaxhighlight lang=" | + | |
− | RLPy. | + | ==== 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. | ||
− | |||
− | |||
− | '''pCallback''' [IN] | + | See Also: [[#UnregisterCallback ( self )|UnregisterCallback]] |
− | + | ||
− | + | ==== Parameters ==== | |
− | + | :'''pCallback''' [IN] UDP callback instance - [[IC_Python_API:RLPy_RTcpCallback|RTcpCallback]] | |
− | <syntaxhighlight lang=" | + | |
− | RLPy. | + | <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> | ||
− | |||
− | |||
− | |||
− | + | === UnregisterCallback ( self ) === | |
− | + | Unregister network Udp event callback. | |
− | + | ||
− | + | <syntaxhighlight lang="python" line='line'> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | <syntaxhighlight lang=" | + | |
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | ''' | + | === 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 | ||
+ | |||
+ | <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 00:00, 23 April 2020
Contents
- 1 Description
- 2 Member Functions
- 2.1 Connect ( self, strIP, uPort )
- 2.2 Disconnect ( self )
- 2.3 IsConnected ( self )
- 2.4 GetData ( self, pBuffer )
- 2.5 GetDataAt ( self, nIndex, pBuffer )
- 2.6 GetDataCount ( self )
- 2.7 GetDataSize ( self )
- 2.8 GetDataSize ( self, nIndex )
- 2.9 SetMaximumDataCount ( self, nCount )
- 2.10 GetMaximumDataCount ( self )
- 2.11 RegisterCallback ( self, pCallback )
- 2.12 UnregisterCallback ( self )
- 2.13 SendData ( self, pBuffer, nDataSize )
- 2.14 JoinMulticastGroup ( self, strIP )
- 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 )