我正在写一个基于文本的Minecraft机器人,主要是(试图)惹恼一些玩家/管理员并查看Minecraft protocol的内部。
无论如何,我还处于设计阶段并编写发送和接收数据包的功能。声明现在看起来像这样:
int send_packet(int sock, con_state_t state, send_packet_t ptype, ...);
int recv_packet(int sock, con_state_t state, recv_packet_t ptype, ...);
(我计划为typedef
s的错误和套接字类型添加int
。)
到目前为止,这么好。我知道变量参数是类型不安全的,但是我可以编写更好的代码而不是struct
。我可以编写这样的代码(send_packet
函数):
/* BTW: I had to decide between structs passed as void* to imitate
* inheritance and variable-length arguments.
* Both are equally type-unsafe but variable-length arguments offer the
* significant advantage of portably iterating over them. Therefore, I can
* implement a packet-independent packet sending function without remarkable
* struggle or packet-specific functions, which would be QUITE a hassle.
* A drawback is that for every argument from the variable-length arguments,
* data has to be pushed on the stack, so the stack frame grows linearly
* to the argument count. Not so with a struct; only the address of it had to
* be passed.
* (Is variable-length argument the right term at all? I deduced it from
* variable-length array...)
*/
int send_packet(int sock, send_packet_t ptype, ...) {
va_list fields;
uint8_t data[0]; /* TODO: replace 0 by something like PACK_MAX_LEN
* or so! */
/* TODO: as mentioned above, data should actually have the size of
* PACK_MAX_LEN. because this might be very big, you could use some
* dynamic memory allocation mechanism! same goes for string_t, chat_t,
* and all similar data types. */
uint8_t* data_it = data;
/* TODO: ADD PREPENDING OF PACKET ID AND PACKET LENGTH HERE! */
/* TODO: this function isn't finished. add some code, man! resuming the
* above comment. */
/* TODO: add support for legacy server list ping! (packet id = 0xfe) */
/* TODO: for more complex data types: add code to convert from
* simple data types to these complex ones like fix_varint_t to
* varint_t! */
va_start(fields, ptype);
for (size_t i = 0; i < sp_structs[ptype].len; ++i) {
/* Jeez, I hope my code is understandable... */
switch (sp_structs[ptype].fields[i]) {
case map_boolean:
cpy_data_sp(boolean_t);
break;
case map_byte:
cpy_data_sp(byte_t);
break;
case map_ubyte:
cpy_data_sp(ubyte_t);
break;
case map_short:
cpy_data_sp(short_t);
break;
case map_ushort:
cpy_data_sp(ushort_t);
break;
case map_int:
cpy_data_sp(int_t);
break;
case map_long:
cpy_data_sp(long_t);
break;
case map_chat:
cpy_data_sp(chat_t);
break;
case map_string:
cpy_data_sp(string_t);
break;
case map_varint:
/* TODO: maybe write a macro to generalize wr_varint
* and wr_varlong. */
fix_varint_t fix = va_arg(fields, fix_varint_t);
data_it += wr_varint(data_it, fix);
break;
case map_varlong:
fix_varlong_t fix = va_arg(fields, fix_varlong_t);
data_it += wr_varlong(data_it, fix);
break;
/*case map_chunk:
cpy_data_sp(chunk_t);
break;*/
}
}
/*
if (sock_send(sock, data, packet_len) == -1) {
return -1;
}
*/
va_end(packet);
return 0;
}
我只是想告诉你,所以你得到为什么变量参数在这里更好。
使用struct
s,函数原型如下所示:
int send_packet(int sock, con_state_t state, send_packet_t ptype, void* pack);
pack
指向struct
。现在我们可以使用ptype
来推断数据包类型,但我们无法在字段上方便地进行迭代,如上面代码中第一条评论所述。
使用该函数的每个人都必须知道参数的确切顺序,否则可能会发生未定义的行为。由于指定的初始值设定项,struct
在这里很不错,它可以启用与订单无关的成员初始化。
你能想出一些模仿变量参数的方法吗?
也许有些va_list
魔法?但它应该严格遵守。
注意:使用C ++已经出现在我的脑海中,这可以解决我的所有问题,但我想用漂亮的标准C99和一些C11来做这件事。
答案 0 :(得分:1)
您可以使用键/值对而不是定位值:
int func(int argc, ...) {
}
并将其称为
func(3,
P_NAME, "John",
P_SURNAME, "Myers",
P_AGE, 10
);
或使用0值作为终结符以避免argc。