网游服务端开发入门知识

论坛 期权论坛     
匿名技术用户   2021-1-11 22:30   48   0
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px"> </span>
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">1  服务器的网络连接</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">  大多数的网络游戏的服务器都会选择非阻塞select这种结构,为什么呢?因为网络游戏的服务器需要处理的连接非常之多,并且大部分会选择在Linux/Unix下运行,那么为每个用户开一个线程实际上是很不划算的,一方面因为在Linux/Unix下的线程是用进程这么一个概念模拟出来的,比较消耗系统资源,另外除了I/O之外,每个线程基本上没有什么多余的需要并行的任务,而且网络游戏是互交性非常强的,所以线程间的同步会成为很麻烦的问题。由此一来,对于这种含有大量网络连接的单线程服务器,用阻塞显然是不现实的。对于网络连接,需要用一个结构来储存,其中需要包含一个向客户端写消息的缓冲,还需要一个从客户端读消息的缓冲,具体的大小根据具体的消息结构来定了。另外对于同步,需要一些时间校对的值,还需要一些各种不同的值来记录当前状态,下面给出一个初步的连接的结构:</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">typedef connection_s {<!-- --></span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    user_t *ob; /* 指向处理服务器端逻辑的结构 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    int fd; /* socket连接 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    struct sockaddr_in addr; /* 连接的地址信息 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    char text[MAX_TEXT]; /* 接收的消息缓冲 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    int text_end; /* 接收消息缓冲的尾指针 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    int text_start; /* 接收消息缓冲的头指针 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    int last_time; /* 上一条消息是什么时候接收到的 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    struct timeval latency; /* 客户端本地时间和服务器本地时间的差值 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">
<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px">    struct timeval last_confirm_time; /* 上一次验证的时间 */</span>
<br style="color:rgb(51,51,51); font-family:Arial; font-size:14.399
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP