Skip to content

Commit e1d2f46

Browse files
Fixed implementation of Async Unity services (#199)
1 parent b4358ce commit e1d2f46

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

com.unity.robotics.ros-tcp-connector/CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ Upgrade the TestRosTcpConnector project to use Unity LTS version 2020.3.11f1
3939
[CameraInfo Generator](https://github.com/Unity-Technologies/ROS-TCP-Connector/issues/133)
4040
- Added API to create TransformMsg using local frame of a transform in Unity
4141

42-
- Added an optional pooling system for ros publishers
43-
- Implemented a queueing and latching system to mimic the ROS implementation in Unity
44-
- Add support for visualizations
42+
- Added an optional pooling system for ros publishers
43+
44+
- Implemented a queueing and latching system to mimic the ROS implementation in Unity
45+
46+
- Collected the various service/publisher/subscriber tables into a single table of RosTopicState
47+
48+
- Hud becomes a generic display platform to support visualizations
49+
50+
- Unity service implementations can be async
4551

4652
### Changed
4753
- Publishing a message to an unregistered topic will show an error.

com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ void AddSubscriberInternal(string topic, string rosMessageName, Action<Message>
234234
}
235235

236236
// Implement a service in Unity
237-
public void ImplementService<TRequest>(string topic, Func<TRequest, Message> callback, int? queueSize = null)
237+
public void ImplementService<TRequest, TResponse>(string topic, Func<TRequest, TResponse> callback, int? queueSize = null)
238238
where TRequest : Message
239+
where TResponse : Message
239240
{
240241
string rosMessageName = rosMessageName = MessageRegistry.GetRosMessageName<TRequest>();
241242

@@ -246,7 +247,29 @@ public void ImplementService<TRequest>(string topic, Func<TRequest, Message> cal
246247
}
247248

248249
int resolvedQueueSize = queueSize.GetValueOrDefault(k_DefaultPublisherQueueSize);
249-
info.ImplementService((Message msg) => callback((TRequest)msg), resolvedQueueSize);
250+
info.ImplementService(callback, resolvedQueueSize);
251+
252+
foreach (Action<RosTopicState> topicCallback in m_NewTopicCallbacks)
253+
{
254+
topicCallback(info);
255+
}
256+
}
257+
258+
// Implement a service in Unity
259+
public void ImplementService<TRequest, TResponse>(string topic, Func<TRequest, Task<TResponse>> callback, int? queueSize = null)
260+
where TRequest : Message
261+
where TResponse : Message
262+
{
263+
string rosMessageName = rosMessageName = MessageRegistry.GetRosMessageName<TRequest>();
264+
265+
RosTopicState info;
266+
if (!m_Topics.TryGetValue(topic, out info))
267+
{
268+
info = AddTopic(topic, rosMessageName);
269+
}
270+
271+
int resolvedQueueSize = queueSize.GetValueOrDefault(k_DefaultPublisherQueueSize);
272+
info.ImplementService(callback, resolvedQueueSize);
250273

251274
foreach (Action<RosTopicState> topicCallback in m_NewTopicCallbacks)
252275
{

com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/RosTopicState.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,28 @@ public void UnsubscribeAll()
165165
SentSubscriberRegistration = false;
166166
}
167167

168-
public void ImplementService(Func<Message, Message> implementation, int queueSize)
168+
public void ImplementService<TRequest, TResponse>(Func<TRequest, TResponse> implementation, int queueSize)
169+
where TRequest : Message
170+
where TResponse : Message
169171
{
170-
m_ServiceImplementation = implementation;
172+
m_ServiceImplementation = (Message msg) =>
173+
{
174+
return implementation((TRequest)msg);
175+
};
171176
m_ConnectionInternal.SendUnityServiceRegistration(m_Topic, m_RosMessageName);
172177
m_ServiceResponseTopic = new RosTopicState(m_Topic, m_RosMessageName, m_Connection, m_ConnectionInternal, MessageSubtopic.Response);
173178
CreateMessageSender(queueSize);
174179
}
175180

176-
public void ImplementService(Func<Message, Task<Message>> implementation, int queueSize)
181+
public void ImplementService<TRequest, TResponse>(Func<TRequest, Task<TResponse>> implementation, int queueSize)
182+
where TRequest : Message
183+
where TResponse : Message
177184
{
178-
m_ServiceImplementationAsync = implementation;
185+
m_ServiceImplementationAsync = async (Message msg) =>
186+
{
187+
TResponse response = await implementation((TRequest)msg);
188+
return response;
189+
};
179190
m_ConnectionInternal.SendUnityServiceRegistration(m_Topic, m_RosMessageName);
180191
m_ServiceResponseTopic = new RosTopicState(m_Topic, m_RosMessageName, m_Connection, m_ConnectionInternal, MessageSubtopic.Response);
181192
CreateMessageSender(queueSize);

0 commit comments

Comments
 (0)