When I want to make an Extension that can transport data through TCP/IP, I add the Server code in the extension.py and compile the Client.py in the same folder with extension.py. When I load the Extension in Omniverse.Kit, the other function can work but the code about Server cannot work. Here is the Error:
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] TypeError: tcpip() takes 0 positional arguments but 1 was given
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin]
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] At:
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\extension\kit-exts-project\exts\omni.gym.4LegRL\omni\gym\4LegRL\extension.py(50): on_startup
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(143): _startup_ext
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(191): startup_all_extensions_in_module
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:/wayne/software/ov/pkg/code-2022.1.0/kit/plugins/bindings-python\omni\ext\impl_internal.py(247): startup_all_extensions_in_module
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] PythonExtension.cpp::startup()(2):
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_commands.py(30): do
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\extscore\omni.kit.commands\omni\kit\undo\undo.py(75): execute
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\extscore\omni.kit.commands\omni\kit\commands\command.py(249): execute
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\utils.py(50): toggle_extension
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_components.py(79): toggle
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin] e:\wayne\software\ov\pkg\code-2022.1.0\kit\exts\omni.kit.window.extensions\omni\kit\window\extensions\ext_components.py(89):
2022-07-01 08:45:06 [Error] [carb.scripting-python.plugin]
Thank you for your attention! I tried to add the TCP in omniverse.connectsample-102.1.5, and it can work successfully, but I still cannot find out the error in the Extension.
Hoping for your any suggestions!
Thank you for your tips!
I have modified it, but when I enabled the extension, the kit.exe blocked and I cannot saw any error in console. I don’t know whether the problem is about my computer, because it worked without blocking yesterday but opening the apps in omniverse took a longer time today and sometimes even failed, since I have just changed a new RTX3090, I don’t know where the problem is.
The problem is that the tpcip method is blocking (there is an infinite loop) and it doesn’t let the Omniverse application to continue with the loading process…
A possible solution is to move it to a secondary thread…
Hi
I have tried, added the code below into “def on_startup(self, ext_id)”, but it blocked still. Can you have a try? I don’t know if it’s a problem with my computer.
Again the problem is the infinite loop that is blocking the main application thread…
Next, there is a quick implementation of the threaded method to work with the socket.
Note that the while loop and the recv are inside the threaded method.
Also, there are some flags to handle the secondary thread and the extension shutdown…
import omni.ext
import time
import socket
import threading
class Extension(omni.ext.IExt):
def on_startup(self, ext_id):
self.running = False
self.should_continue = False
self.tcp_socket = None
threading.Thread(target=self.tcpip).start()
def on_shutdown(self):
self.should_continue = False
if self.tcp_socket:
print("shutdown socket")
self.tcp_socket.shutdown(socket.SHUT_RDWR)
# wait for the thread to finish
while self.running:
print("waiting for thread to finish")
time.sleep(0.1)
self.tcp_socket = None
def tcpip(self):
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
max_attempts = 10
for i in range(max_attempts):
print("attempting to connect to server:", i)
try:
self.tcp_socket.bind(("127.0.0.1", 40005))
break
except:
time.sleep(1)
if i == max_attempts:
print("failed to connect to server")
self.tcp_socket = None
return
self.tcp_socket.listen(1)
print("connecting...")
try:
self.client_socket, self.ip_port = self.tcp_socket.accept()
except Exception as e:
print(e)
return
print("client", self.ip_port, "connected")
self.running = True
self.should_continue = True
while self.should_continue:
data = self.client_socket.recv(1024)
if not data:
break
print(data)
print("client disconnected")
self.tcp_socket.close()
self.tcp_socket = None
self.running = False