Re: [game.dev.top] 关于服务器与客户端协议的同步问题

41 views
Skip to first unread message

Milo Yip

unread,
Oct 18, 2011, 12:38:02 AM10/18/11
to gamed...@googlegroups.com
這個最好能滿足DRY principle。
用定義文件編譯成目標平台的源代碼是最常見的方法。有時需要跨平台、跨語言。
或可參考 google的protobuf的做法 (本人沒研究過)
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html

2011/10/18 sail tsao <sail...@gmail.com>:
> 服务器客户端之间的协议需要经常改动,而且很容易出错,我现在用过的办法有以下几种:
> 1.最原始的办法,纯手工,客户端服务器商量好消息格式,确定好消息ID,各自有各自单独的文件定义。(最容易出错)
> 2.制作协议生成工具。协议用XML定义,然后用语法分析库(boost.Spirit)解析XML生成客户端服务器相应的消息定义文件。(现在使用的
> 办法,现对于第一种方便了很多)
>
> 想请问下各位同行还有没有其他更好的办法。

--
Milo Yip

Twitter @miloyip
http://www.cnblogs.com/miloyip/
http://miloyip.seezone.net/

Alex

unread,
Oct 18, 2011, 1:30:14 AM10/18/11
to game.dev.top
Milo Yip 兄说的很有道理, protobuf是一个不错的方案,无论在速度还是占用空间上都是很有优势的,只是序列化后的数据是不有好的,可
能还需要解析工具。

On 10月18日, 下午12时38分, Milo Yip <milo...@gmail.com> wrote:
> 這個最好能滿足DRY principle。
> 用定義文件編譯成目標平台的源代碼是最常見的方法。有時需要跨平台、跨語言。

> 或可參考 google的protobuf的做法 (本人沒研究過)http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview....
>
> 2011/10/18 sail tsao <sail.t...@gmail.com>:

sail tsao

unread,
Oct 18, 2011, 2:47:50 AM10/18/11
to gamed...@googlegroups.com
To Milo:protobuf之前看过,可惜我们客户端Unity用的是c#,protobuf没法用,确实,现在最好的办法也就是通过xml定义生成跨平台的文件了。
To Alex:“各自有各自单独的文件定义”的意思是指客户端服务器是不同语言的时候就没法用一个文件定义消息了
--
Sail Tsao
I'm Just a Simple Person.
My Webpage:
http://www.sailtsao.com/

Alex

unread,
Oct 18, 2011, 2:59:30 AM10/18/11
to game.dev.top
晓得你的意思了,我们服务器是纯C++,客户端用的是flash,也不是一个文件来的。。。
本来protobuf 只是支持Java 、c++和Pyton,不过有一些addon的,可以看一下:
http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns

On 10月18日, 下午2时47分, sail tsao <sail.t...@gmail.com> wrote:
> To
> Milo:protobuf之前看过,可惜我们客户端Unity用的是c#,protobuf没法用,确实,现在最好的办法也就是通过xml定义生成跨平台的文 件了。
> To Alex:“各自有各自单独的文件定义”的意思是指客户端服务器是不同语言的时候就没法用一个文件定义消息了
>

niuzhangming

unread,
Oct 18, 2011, 10:04:51 AM10/18/11
to game.dev.top
There are lots of choices:

Jason, XML, pro buf, Corba, or just original bit stream.


It depends on the requirements,
I had exp of building a dis sys using just bit stream(struct or
class), with around 50 to 100 servers,
but better make sure the servers are based on the same system.(for
small sized system, this is not difficult)

Pro buf is good but time consuming to use it for small and medium
sized projects in my mind.

better write a small RPC api first, based on bit stream is just trick
when handle big little Endian.

William.Wang

unread,
Oct 20, 2011, 3:42:05 AM10/20/11
to game.dev.top
我们这边直接用结构体定义消息,发送的就是结构体占用的内存.如果版本不一致,就惨了...


On Oct 18, 12:38 pm, Milo Yip <milo...@gmail.com> wrote:
> 這個最好能滿足DRY principle。
> 用定義文件編譯成目標平台的源代碼是最常見的方法。有時需要跨平台、跨語言。

> 或可參考 google的protobuf的做法 (本人沒研究過)http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview....
>
> 2011/10/18 sail tsao <sail.t...@gmail.com>:
>

Milo Yip

unread,
Oct 20, 2011, 4:09:56 AM10/20/11
to gamed...@googlegroups.com
這樣做還會有幾個問題:
1. pointer的field不能正確傳遞
2. 機器的architecture或compiler不同,內存佈局會不同(32/64-bit, endianess, member alignment)
3. 如果struct裡有固定數組,而實際不是滿的,會浪費bandwidth

2011/10/20 William.Wang <egm...@gmail.com>:
> 我们这边直接用结构体定义消息,发送的就是结构体占用的内存.如果版本不一致,就惨了...

Zhangming Niu

unread,
Oct 20, 2011, 4:23:18 AM10/20/11
to gamed...@googlegroups.com
To: milo

1 and 3 are not matters
pointer的field不能正確傳遞
(1)normally no one would pass a pointer, but this can be done although it is tricky .
如果struct裡有固定數組,而實際不是滿的,
(2)I do not really understand,
maybe you mean 2 power x architecture in the computer, but it wont affect performance
because TCP or TCP will tell the length of the byte stream.  if 9bytes is sent, then 9 bytes will be received.


2011/10/20 Milo Yip <mil...@gmail.com>



--
--------------------------------------------------------------------
Best Regards,

Zhangming Niu




Zhangming Niu

unread,
Oct 20, 2011, 4:24:13 AM10/20/11
to gamed...@googlegroups.com
Tco or TCp  =>  TCP or UDP

2011/10/20 Zhangming Niu <niuzha...@gmail.com>

Milo Yip

unread,
Oct 20, 2011, 4:46:32 AM10/20/11
to gamed...@googlegroups.com
I just stated some obvious limitations of the method.

(1) In C/C++, it is often needed to store data as pointers in field,
for example, null-terminated string.

(2) Fixed-size array which are not fully populated data will waste
bandwidth, e.g. struct Inventory { Item items[100]; size_t itemCount;
};

2011/10/20 Zhangming Niu <niuzha...@gmail.com>:

egmkang wang

unread,
Oct 20, 2011, 11:11:26 PM10/20/11
to game.dev.top
结构体里面不允许出现指针....
如果出现的话,只能是char data[0];这样的.
里面可以出现数组,如果是数组是固定大小,那么就是固定的;
如果不知道大小,前面搞一个变量,标记后面的大小,后面就直接char data[0];了

On Oct 20, 4:46 pm, Milo Yip <milo...@gmail.com> wrote:
> I just stated some obvious limitations of the method.
>
> (1) In C/C++, it is often needed to store data as pointers in field,
> for example, null-terminated string.
>
> (2) Fixed-size array which are not fully populated data will waste
> bandwidth, e.g. struct Inventory { Item items[100]; size_t itemCount;
>
> };
>

> 2011/10/20 Zhangming Niu <niuzhangm...@gmail.com>:


>
>
>
>
>
>
>
>
>
> > To: milo
>
> > 1 and 3 are not matters
> > pointer的field不能正確傳遞
> > (1)normally no one would pass a pointer, but this can be done although it is
> > tricky .
> > 如果struct裡有固定數組,而實際不是滿的,
> > (2)I do not really understand,
> > maybe you mean 2 power x architecture in the computer, but it wont affect
> > performance
> > because TCP or TCP will tell the length of the byte stream. if 9bytes is
> > sent, then 9 bytes will be received.
>

> > 2011/10/20 Milo Yip <milo...@gmail.com>


>
> >> 這樣做還會有幾個問題:
> >> 1. pointer的field不能正確傳遞
> >> 2. 機器的architecture或compiler不同,內存佈局會不同(32/64-bit, endianess, member
> >> alignment)
> >> 3. 如果struct裡有固定數組,而實際不是滿的,會浪費bandwidth
>

> >> 2011/10/20 William.Wang <egmk...@gmail.com>:

egmkang wang

unread,
Oct 20, 2011, 11:13:13 PM10/20/11
to game.dev.top
第三点其实是 这样的,我们这边都有gzip压缩......

On Oct 20, 4:09 pm, Milo Yip <milo...@gmail.com> wrote:
> 這樣做還會有幾個問題:
> 1. pointer的field不能正確傳遞
> 2. 機器的architecture或compiler不同,內存佈局會不同(32/64-bit, endianess, member alignment)
> 3. 如果struct裡有固定數組,而實際不是滿的,會浪費bandwidth
>

> 2011/10/20 William.Wang <egmk...@gmail.com>:

sail tsao

unread,
Oct 20, 2011, 11:45:52 PM10/20/11
to gamed...@googlegroups.com
压缩并不能解决固定数组没用满的问题。如果固定数组的大小是1000,而协议中只用了其中的1个不是很浪费么?最好就是不用固定数组
to egmkang:我们也是在序列化时将字符串转为2字节的长度信息和字符数组放入二进制流,动态数组也是如此处理。

Zhangming Niu

unread,
Oct 21, 2011, 3:29:39 AM10/21/11
to gamed...@googlegroups.com
egmkang,

pointers could show up in the struct. just tricky to implement.

2011/10/21 sail tsao <sail...@gmail.com>

egmkang wang

unread,
Oct 21, 2011, 4:47:14 AM10/21/11
to game.dev.top
tricky is not cannot
:-)

On Oct 21, 3:29 pm, Zhangming Niu <niuzhangm...@gmail.com> wrote:
> egmkang,
>
> pointers could show up in the struct. just tricky to implement.
>

> 2011/10/21 sail tsao <sail.t...@gmail.com>


>
>
>
>
>
>
>
>
>
> > 压缩并不能解决固定数组没用满的问题。如果固定数组的大小是1000,而协议中只用了其中的1个不是很浪费么?最好就是不用固定数组
> > to egmkang:我们也是在序列化时将字符串转为2字节的长度信息和字符数组放入二进制流,动态数组也是如此处理。
>

Zhangming Niu

unread,
Oct 21, 2011, 4:57:23 AM10/21/11
to gamed...@googlegroups.com
you said "结构体里面不允许出现指针..."

I know, was debating with you...:)

2011/10/21 egmkang wang <egm...@gmail.com>
Reply all
Reply to author
Forward
0 new messages