2011-03-14 100 views
4

我正在为minecraft创建一个命令行客户端。有关协议的完整规范可以在这里找到:http://mc.kev009.com/Protocol。要事先回答你的问题,是的,我有点C++ noob。通过网络构建和发送二进制数据

我在实现这个协议时遇到了各种问题,其中每个都很重要。

  1. 该协议说,所有类型都是big-endian。我不知道如何检查我的数据是否是小端,如果是,如何转换成大端。
  2. 字符串数据类型有点奇怪。它是一个修改过的UTF-8字符串,前面是一个包含字符串长度的短字符。我不知道如何将它包装到一个简单的char []数组中,也不知道如何将我的简单字符串转换为修改后的UTF-8。
  3. 即使我知道如何将数据转换为big-endian并创建修改后的UTF-8字符串,我仍然不知道如何将它们打包到char []数组中,并将其作为一个包发送。我之前所做的只是简单的HTTP网络,它是纯ASCII的。

解释,链接,相关函数名称和短片段非常感谢!

EDIT

1和3,现在被应答。 1在下面由user470379回答。 3被这个AWESOME线程解释了我想要做得很好:http://cboard.cprogramming.com/networking-device-communication/68196-sending-non-char*-data.html但我不确定修改后的UTF-8。

+0

已经有很多关于对SO字节序好问题和答案。 – GWW 2011-03-14 19:50:17

+0

下次我会更好看。 – orlp 2011-03-14 19:56:58

回答

9

的传统方法是定义为每个协议消息的C++消息结构和实现序列和反串行化的功能它。例如,Login Request可以表示为:

#include <string> 
#include <stdint.h> 

struct LoginRequest 
{ 
    int32_t protocol_version; 
    std::string username; 
    std::string password; 
    int64_t map_seed; 
    int8_t dimension; 
}; 

现在需要序列化功能。首先它需要整数和字符串的序列化函数,因为这些是LoginRequest中的成员类型。

整数序列化函数需要对大端表示进行转换。

#include <boost/detail/endian.hpp> 
#include <algorithm> 

#ifdef BOOST_LITTLE_ENDIAN 

    inline void xcopy(void* dst, void const* src, size_t n) 
    { 
     char const* csrc = static_cast<char const*>(src); 
     std::reverse_copy(csrc, csrc + n, static_cast<char*>(dst)); 
    } 

#elif defined(BOOST_BIG_ENDIAN) 

    inline void xcopy(void* dst, void const* src, size_t n) 
    { 
     char const* csrc = static_cast<char const*>(src); 
     std::copy(csrc, csrc + n, static_cast<char*>(dst)); 
    } 

#endif 

// serialize an integer in big-endian format 
// returns one past the last written byte, or >buf_end if would overflow 
template<class T> 
typename boost::enable_if<boost::is_integral<T>, char*>::type serialize(T val, char* buf_beg, char* buf_end) 
{ 
    char* p = buf_beg + sizeof(T); 
    if(p <= buf_end) 
     xcopy(buf_beg, &val, sizeof(T)); 
    return p; 
} 

// deserialize an integer from big-endian format 
// returns one past the last written byte, or >buf_end if would underflow (incomplete message) 
template<class T> 
typename boost::enable_if<boost::is_integral<T>, char const*>::type deserialize(T& val, char const* buf_beg, char const* buf_end) 
{ 
    char const* p = buf_beg + sizeof(T); 
    if(p <= buf_end) 
     xcopy(&val, buf_beg, sizeof(T)); 
    return p; 
} 

而对串(处理modified UTF-8 the same way as asciiz strings)::

// serialize a UTF-8 string 
// returns one past the last written byte, or >buf_end if would overflow 
char* serialize(std::string const& val, char* buf_beg, char* buf_end) 
{ 
    int16_t len = val.size(); 
    buf_beg = serialize(len, buf_beg, buf_end); 
    char* p = buf_beg + len; 
    if(p <= buf_end) 
     memcpy(buf_beg, val.data(), len); 
    return p; 
} 

// deserialize a UTF-8 string 
// returns one past the last written byte, or >buf_end if would underflow (incomplete message) 
char const* deserialize(std::string& val, char const* buf_beg, char const* buf_end) 
{ 
    int16_t len; 
    buf_beg = deserialize(len, buf_beg, buf_end); 
    if(buf_beg > buf_end) 
     return buf_beg; // incomplete message 
    char const* p = buf_beg + len; 
    if(p <= buf_end) 
     val.assign(buf_beg, p); 
    return p; 
} 

和一对夫妇由于消息的成员被复制到和从缓冲器,字节顺序的反转可以在复制完成辅助函子:

struct Serializer 
{ 
    template<class T> 
    char* operator()(T const& val, char* buf_beg, char* buf_end) 
    { 
     return serialize(val, buf_beg, buf_end); 
    } 
}; 

struct Deserializer 
{ 
    template<class T> 
    char const* operator()(T& val, char const* buf_beg, char const* buf_end) 
    { 
     return deserialize(val, buf_beg, buf_end); 
    } 
}; 

现在使用这些基本功能,我们可以很容易地进行序列化和反序列化LoginRequest消息:

template<class Iterator, class Functor> 
Iterator do_io(LoginRequest& msg, Iterator buf_beg, Iterator buf_end, Functor f) 
{ 
    buf_beg = f(msg.protocol_version, buf_beg, buf_end); 
    buf_beg = f(msg.username, buf_beg, buf_end); 
    buf_beg = f(msg.password, buf_beg, buf_end); 
    buf_beg = f(msg.map_seed, buf_beg, buf_end); 
    buf_beg = f(msg.dimension, buf_beg, buf_end); 
    return buf_beg; 
} 

char* serialize(LoginRequest const& msg, char* buf_beg, char* buf_end) 
{ 
    return do_io(const_cast<LoginRequest&>(msg), buf_beg, buf_end, Serializer()); 
} 

char const* deserialize(LoginRequest& msg, char const* buf_beg, char const* buf_end) 
{ 
    return do_io(msg, buf_beg, buf_end, Deserializer()); 
} 

使用上述辅助函子和表示输入/输出缓冲器因为只有一个函数模板需要做消息的序列化和反序列化char迭代器的范围内。

,并把所有的一起,用法:

int main() 
{ 
    char buf[0x100]; 
    char* buf_beg = buf; 
    char* buf_end = buf + sizeof buf; 

    LoginRequest msg; 

    char* msg_end_1 = serialize(msg, buf, buf_end); 
    if(msg_end_1 > buf_end) 
     ; // more buffer space required to serialize the message 

    char const* msg_end_2 = deserialize(msg, buf_beg, buf_end); 
    if(msg_end_2 > buf_end) 
     ; // incomplete message, more data required 
} 
+0

你。先生。是。真棒。享受你的+25。 – orlp 2011-03-14 21:26:10

+0

虽然不会使用默认网络endian功能会更好吗? http://beej.us/guide/bgnet/output/html/multipage/htonsman.html – orlp 2011-03-14 21:57:05

+0

没有64位版本的hton-functions,只有16和32.'betoh'和'hoteb'只是不同的Linux - 具有64位整数支持的相同功能的特定名称。在将big-endian转换成little-endian时,你可以避免使用'memcpy()'的反转版本。 – 2011-03-14 22:14:17

1

对于#1,您需要使用ntohs和朋友。对于16位整数使用*s(简写)版本,对于32位整数使用*l(长)版本。 hton*(网络主机)将输出数据转换为大端,而不依赖于您所在平台的字节序列号,并且网络到主机将会将输入数据转换回来(再次独立于平台排序)

+0

谢谢。任何想法如何通过分割字节将这些int存储在char []数组中? – orlp 2011-03-14 20:15:07

1

了我的头顶部...

const char* s; // the string you want to send 
short len = strlen(s); 

// allocate a buffer with enough room for the length info and the string 
char* xfer = new char[ len + sizeof(short) ]; 

// copy the length info into the start of the buffer 
// note: you need to hanle endian-ness of the short here. 
memcpy(xfer, &len, sizeof(short)); 

// copy the string into the buffer 
strncpy(xfer + sizeof(short), s, len); 

// now xfer is the string you want to send across the wire. 
// it starts with a short to identify its length. 
// it is NOT null-terminated. 
+0

不知道为什么这是downvoted,谢谢! – orlp 2011-03-14 20:42:54

+0

你真的应该为长度声明两个变量,一个是size_t来保存实际长度;或者至少使'len'成为未签名的短片。就像现在这样,当strlen(s)%65536> 32768'时,这段代码将会失败。 – user470379 2011-03-15 00:51:33

+0

好点。或者我们可以做'short len = boost :: numeric_cast (strlen(s));'如果它不适合简写,它将抛出异常。 – Tim 2011-03-15 01:22:21