@@ -15,6 +15,9 @@ namespace Unity.Robotics.ROSTCPConnector
15
15
{
16
16
public class ROSConnection : MonoBehaviour
17
17
{
18
+ public const string k_Version = "v0.7.0" ;
19
+ public const string k_CompatibleVersionPrefix = "v0.7." ;
20
+
18
21
// Variables required for ROS communication
19
22
[ SerializeField ]
20
23
[ FormerlySerializedAs ( "hostName" ) ]
@@ -633,6 +636,32 @@ void ReceiveSysCommand(string topic, string json)
633
636
{
634
637
switch ( topic )
635
638
{
639
+ case SysCommand . k_SysCommand_Handshake :
640
+ {
641
+ var handshakeCommand = JsonUtility . FromJson < SysCommand_Handshake > ( json ) ;
642
+ if ( handshakeCommand . version == null )
643
+ {
644
+ Debug . LogError ( $ "Corrupted or unreadable ROS-TCP-Endpoint version data! Expected: { k_Version } ") ;
645
+ }
646
+ else if ( ! handshakeCommand . version . StartsWith ( k_CompatibleVersionPrefix ) )
647
+ {
648
+ Debug . LogError ( $ "Incompatible ROS-TCP-Endpoint version: { handshakeCommand . version } . Expected: { k_Version } ") ;
649
+ }
650
+
651
+ var handshakeMetadata = JsonUtility . FromJson < SysCommand_Handshake_Metadata > ( handshakeCommand . metadata ) ;
652
+ #if ROS2
653
+ if ( handshakeMetadata . protocol != "ROS2" )
654
+ {
655
+ Debug . LogError ( $ "Incompatible protocol: ROS-TCP-Endpoint is using { handshakeMetadata . protocol } , but Unity is in ROS2 mode. Switch it from the Robotics/Ros Settings menu.") ;
656
+ }
657
+ #else
658
+ if ( handshakeMetadata . protocol != "ROS1" )
659
+ {
660
+ Debug . LogError ( $ "Incompatible protocol: ROS-TCP-Endpoint is using { handshakeMetadata . protocol } , but Unity is in ROS1 mode. Switch it from the Robotics/Ros Settings menu.") ;
661
+ }
662
+ #endif
663
+ }
664
+ break ;
636
665
case SysCommand . k_SysCommand_Log :
637
666
{
638
667
var logCommand = JsonUtility . FromJson < SysCommand_Log > ( json ) ;
@@ -843,12 +872,23 @@ static async Task ConnectionThread(
843
872
844
873
static async Task ReaderThread ( int readerIdx , NetworkStream networkStream , ConcurrentQueue < Tuple < string , byte [ ] > > queue , int sleepMilliseconds , CancellationToken token )
845
874
{
875
+ // First message should be the handshake
876
+ Tuple < string , byte [ ] > handshakeContent = await ReadMessageContents ( networkStream , sleepMilliseconds , token ) ;
877
+ if ( handshakeContent . Item1 == SysCommand . k_SysCommand_Handshake )
878
+ {
879
+ ROSConnection . m_HasConnectionError = false ;
880
+ queue . Enqueue ( handshakeContent ) ;
881
+ }
882
+ else
883
+ {
884
+ Debug . LogError ( $ "Invalid ROS-TCP-Endpoint version detected: 0.6.0 or older. Expected: { k_Version } .") ;
885
+ }
886
+
846
887
while ( ! token . IsCancellationRequested )
847
888
{
848
889
try
849
890
{
850
891
Tuple < string , byte [ ] > content = await ReadMessageContents ( networkStream , sleepMilliseconds , token ) ;
851
- // Debug.Log($"Message {content.Item1} received");
852
892
ROSConnection . m_HasConnectionError = false ;
853
893
854
894
if ( content . Item1 != "" ) // ignore keepalive messages
0 commit comments