简体   繁体   English

我可以将GameKit的peerID强制转换为uint32_t

[英]Can I cast GameKit's peerID to uint32_t

I am using GameKit for networking in my C++ application (created an Objective-C class for managing GameKit networking), but the problem is that GameKit uses NSString (not a C++ type) to identify peers (peerID). 我在C ++应用程序中使用GameKit进行网络连接(创建了用于管理GameKit联网的Objective-C类),但是问题是GameKit使用NSString(不是C ++类型)来标识对等方(peerID)。 I have tested GameKit a lot and in my practice peerID is always a number (eg. 754569949). 我已经对GameKit进行了很多测试,而在我的实践中,peerID始终是一个数字(例如754569949)。 Is it safe to lexically cast peerID to uint32_t? 用词法将peerID强制转换为uint32_t是否安全? Are there any cases where peerID is not a number? 是否有peerID不是数字的情况?

The problem is that for compatibility with other parts of code, I need to identify clients by integer, not string 问题是,为了与代码的其他部分兼容,我需要通过整数而不是字符串来标识客户端

If you have to get an integer out of these strings, in the absence of any guarantee it will always be a number, simply build a table of known IDs and assign indexes to them. 如果必须从这些字符串中获取整数,则在没有任何保证的情况下,该整数将始终是数字,只需构建一个已知ID的表并为其分配索引。

57483829 => 0
42937422 => 1
3333fghh => 2

You could use two std::map s to quickly convert between the two representations. 您可以使用两个std::map来在两种表示形式之间快速转换。

Simply convert the NSString to a string that's more C++ friendly. 只需将NSString转换为更C ++友好的字符串。 Alternatively, do the translation and maintain the indexes in Objective-C first and you can give your C++ code an index. 或者,首先进行翻译并在Objective-C中维护索引,然后可以为C ++代码提供索引。

I see a couple of parts to your question: 1) parsing an int from peerID string; 我看到您的问题有两部分:1)从peerID字符串解析一个int; 2) maintaining association between your internals ids and peerID s. 2)维护您的内部ID和peerID之间的关联。

If you need to get peerID into your C++ code and then be able to convert it back to NSString for use with GameKit framework, you shouldn't make any more assumptions about peerID 's contents than the official docs provide. 如果您需要将peerID放入C ++代码中,然后能够将其转换回NSString以与GameKit框架一起使用,则您不应像官方文档所提供的那样对peerID的内容进行更多的假设。

In other words, if you absolutely have to use ints, provide a way to map peerID s to your internal identifiers. 换句话说,如果您绝对必须使用int,请提供一种将peerID映射到内部标识符的方法。 But keep peerID s around and associated with your identifiers to be able to map the other way -- from your id back to NSString . 但是,请保留peerID并与您的标识符相关联,以便能够以另一种方式进行映射-从您的ID回到NSString

Making a map of NSString -> uint32_t might be one way to go about it. 制作NSString -> uint32_t的映射可能是解决该问题的一种方法。

The above mostly covers the 2nd part I mentioned in the first sentence. 以上内容主要涵盖了我在第一句中提到的第二部分。 Now let's consider if it's save to parse peerID as an integer. 现在让我们考虑一下是否保存将peerID解析为整数。

If you need to guarantee that a particular peerID will always map to the same uint32_t , you either assume peerID represents a number that will always fit into uint32_t , or you compute a hash of the string. 如果需要确保特定的peerID始终映射到相同的uint32_t ,则可以假设peerID代表一个始终适合uint32_t ,或者您计算字符串的哈希值。 There are problems with both approaches. 两种方法都存在问题。 Such a short hash might have collisions in real use. 如此短的哈希可能会在实际使用中产生冲突。 On the other hand, your system might brake one day when Apple decides to extend their peerID s to also include letters. 另一方面,当Apple决定扩展其peerID使其也包括字母时,您的系统可能有一天会刹车。

So I can't really make a decision for you, I've just tried to lay out the possibilities you might want to consider. 因此,我无法真正为您做出决定,我只是尝试列出您可能要考虑的可能性。 I'd strongly suggest you to find a way to use original peerID , possibly converted to a C++ string type. 我强烈建议您找到一种使用原始peerID的方法,该方法可能会转换为C ++字符串类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM