首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >游戏服务器之多线程发送(中)

游戏服务器之多线程发送(中)

作者头像
李海彬
发布于 2018-03-22 08:37:48
发布于 2018-03-22 08:37:48
76300
代码可运行
举报
文章被收录于专栏:Golang语言社区Golang语言社区
运行总次数:0
代码可运行

4、拷贝数据到会话的发送缓冲区

交换发送队列和添加队列,拷贝会话的发送队列的数据到会话的发送缓冲区

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
BOOL ExecSockDataMgr::CopyWaitSendBuffers(PEXECDATASENDTHREAD pSendThread, BOOL boForceCopy)  
{  
    CGateSendPacket *pSendPacket, **ppPacketList;  
    CBList<CGateSendPacket*> *pBufferList;  
    PRUNGATEUSERSESSION pSession;  
 INT_PTR nCount;  
    common::NetPacketHeader *pPackHdr;  
 int nSize, nTotalSize, nAppendSize, nPacketSize;  
    TICKCOUNT dwMsgTick;  
 
 if ( !boForceCopy )  
        boForceCopy = TryEnterCriticalSection( &pSendThread->SendQueueLock );  
 else EnterCriticalSection( &pSendThread->SendQueueLock );  
 
 if ( boForceCopy )  
    {  
        pBufferList = pSendThread->pSendAppendList;  
        pSendThread->pSendAppendList = pSendThread->pSendProcList;  
        pSendThread->pSendProcList = pBufferList;  
        LeaveCriticalSection( &pSendThread->SendQueueLock );  
 
        nCount = pBufferList->count();  
 if ( nCount > 0 )  
        {  
            dwMsgTick = _getTickCount();  
            ppPacketList = (CGateSendPacket**)*pBufferList;  
            nTotalSize = 0;  
            nAppendSize = 0;  
 do 
            {  
                pSendPacket = *ppPacketList;  
                ppPacketList++;  
                nCount--;  
 
                nPacketSize = (int)pSendPacket->getLength();  
 if (pSendPacket->m_nUserIndex < m_SessionList.count())  
                    pSession = m_SessionList[pSendPacket->m_nUserIndex];  
 else pSession = NULL;  
 
 if ( pSession && pSession->nSocket != INVALID_SOCKET && !pSession->boMarkToClose   
                    && !pSession->boRemoteClosed && pSession->nVerifyIdx == pSendPacket->m_nVerify )  
                {  
 //检测并保留会话缓冲区空间(1024字节对齐) 
                    nSize = pSession->SendBuf.nOffset + sizeof(*pPackHdr) + nPacketSize;  
 if ( pSession->SendBuf.nSize < nSize )//拓展发送缓冲区的大小 
                    {  
                        nSize = (nSize / SESSION_DATAGROW_SIZE + 1) * SESSION_DATAGROW_SIZE;  
                        pSession->SendBuf.nSize = nSize;  
                        pSession->SendBuf.lpBuffer = (char*)realloc( pSession->SendBuf.lpBuffer, nSize );  
                    }  
 //写入包头 
                    pPackHdr = (common::NetPacketHeader*)&pSession->SendBuf.lpBuffer[pSession->SendBuf.nOffset];  
                    pPackHdr->dwIdent = common::NetPacketHeader::NetPacketHeaderIdent;  
                    pPackHdr->wPacketSize = nPacketSize;  
                    pPackHdr->wVerification = 0;  
                    pSession->SendBuf.nOffset += sizeof(*pPackHdr);  
                    nAppendSize += sizeof(*pPackHdr);  
 //写入包体数据(从数据包到发送缓冲区) 
                    memcpy( &pSession->SendBuf.lpBuffer[pSession->SendBuf.nOffset], pSendPacket->getMemoryPtr(), nPacketSize );  
                    pSession->SendBuf.nOffset += nPacketSize;  
                    pSession->dwServerMsgTick = dwMsgTick;  
                    nAppendSize += nPacketSize;  
                }  
                nTotalSize += nPacketSize;  
                pSendPacket->freeBack();  
            }  
 while ( nCount > 0 );  
            pBufferList->clear();  
 
            InterlockedExchangeAdd( (LONG*)&m_dwWaitSendUserSize, nAppendSize );//待发送给用户的数据包大小(该变量记录用于程序调试) 
            InterlockedExchangeAdd( (LONG*)&m_dwWaitSendQueueSize, -nTotalSize );//减少待发给用户的数据包队列大小 
        }  
    }  
 return boForceCopy;  
}   

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-09-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
PAT(甲级)1143. Lowest Common Ancestor(30)
PAT 1143. Lowest Common Ancestor(30) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following properties: 1.The left subtree of a node contains only nodes with keys less than the node’s key. 2.The right subtree of a node contains only nodes with keys greater than or equal to the node’s key. 3.Both the left and right subtrees must also be binary search trees. Given any two nodes in a BST, you are supposed to find their LCA. 输入格式: Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
lexingsen
2022/02/25
2240
PAT(甲级)1051.LCA in a Binary Tree(30)
PAT 1051.LCA in a Binary Tree(30) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA.
lexingsen
2022/02/25
2270
PAT(甲级)1051.LCA in a Binary Tree(30)
PAT甲级题目
PAT甲级的题目有关于树的题目,1053,1086,1090,1102,1106,1115,1119,1038,1110,1020,1043
西红柿炒鸡蛋
2020/03/20
5490
PAT甲级题目
【PAT甲级】Is It a Binary Search Tree
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
3460
PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are suppo
ShenduCC
2018/04/27
9680
二叉树的数据结构与C++实例
二叉树是一种每个结点至多只有两个子树(即二叉树的每个结点的度不大于2),并且二叉树的子树有左右之分,其次序不能任意颠倒。
里克贝斯
2021/05/21
5040
二叉树的数据结构与C++实例
Binary Tree Traversals(二叉树) - HDU 1710
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
ACM算法日常
2018/08/07
4040
Binary Tree Traversals(二叉树) - HDU 1710
hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3475    Accepted Submission(s): 1555 Problem Description A binary tree is a finite set of vertices that is either empty or
Gxjun
2018/03/26
8560
hdu1710(Binary Tree Traversals)(二叉树遍历)
03-树3 Tree Traversals Again
这一题,需要清楚非递归遍历二叉树的知识,你是否和我一样又回头预习了复习了这个知识呢
废江_小江
2022/09/05
2410
03-树3 Tree Traversals Again
Leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 中序和后序还原二叉树。 直接在上一题的代码上做一点小修改就行了。 /** * Definition for a binary tree node. * struct TreeNode { * int val;
triplebee
2018/01/12
6490
PAT 1020 Tree Traversals (25分) 解读
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
vivi
2020/07/14
5300
Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree.
卡尔曼和玻尔兹曼谁曼
2019/01/22
4720
「二叉树进阶题解:构建、遍历与结构转化全解析」
可以看见,唯一特殊的就是左子树,当右子树存在的时候左子树不存在的时候,我们需要用()代表空,但是没有左子树,又没有右子树的时候,我们不需要做任何处理。
用户11305458
2024/11/21
1350
「二叉树进阶题解:构建、遍历与结构转化全解析」
[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 难度:Medium 题目:106. Construct Binary Tree from Inorder and Postorder Traversal
尾尾部落
2018/09/04
5270
【编程练习】复习一下树的遍历
深度有限遍历记录层数:增加一个level //深度优先遍历 void depthFirstSearch(Tree root){ stack<pair<int, Node *> > nodeStack; //使用C++的STL标准模板库 nodeStack.push(make_pair(0, root)); Node *node; while(!nodeStack.empty()){ node = nodeStack.top().second;
流川疯
2022/05/06
1880
二叉树的非递归遍历
                                                            二叉树的非递归遍历
bear_fish
2018/09/20
8980
数据结构与算法之九 树结构
在本章中,你将学习: 在树中存储数据 实现二叉树 实现二叉搜索树 假设你被要求呈现操作系统的目录结构。 目录结构含有不同的文件夹和文件。一个文件夹可能含有更多的子文件夹和文件。 在这种情况下,要用线型结构来表示这种结构几乎是不可能的,因为所有的项目之间都有层级 关系。 要表示这样的结构,就需要一种非线型的数据存储机制。
张哥编程
2024/12/17
1680
根据前序和中序(后序和中序)遍历构造树 #算法#
Given preorder and inorder traversal of a tree, construct the binary tree. 根据前序和中序遍历序列构建二叉树。 Note: You may assume that duplicates do not exist in the tree.
梦飞
2022/06/23
3010
数据结构 第五章 树和二叉树
树:n(n≥0)个结点的有限集合。 当n=0时,称为空树; 任意一棵非空树满足以下条件: ⑴ 有且仅有一个特定的称为根的结点; ⑵ 当n>1时,除根结点之外的其余结点被分成m(m>0)个互不相交的有限集合T1,T2,… ,Tm,其中每个集合又是一棵树,并称为这个根结点的子树。 结点的度:结点所拥有的子树的个数。 树的度:树中各结点度的最大值。 叶子结点:度为0的结点,也称为终端结点。 分支结点:度不为0的结点,也称为非终端结点。 孩子、双亲:树中某结点子树的根结点称为这个结点的孩子结点,这个结点称为它孩子结点的双亲结点; 兄弟:具有同一个双亲的孩子结点互称为兄弟。 路径:如果树的结点序列n1, n2, …, nk有如下关系:结点ni是ni+1的双亲(1<=i<k),则把n1, n2, …, nk称为一条由n1至nk的路径;路径上经过的边的个数称为路径长度。 祖先、子孙:在树中,如果有一条路径从结点x到结点y,那么x就称为y的祖先,而y称为x的子孙。 结点所在层数:根结点的层数为1;对其余任何结点,若某结点在第k层,则其孩子结点在第k+1层。 树的深度:树中所有结点的最大层数,也称高度。 层序编号:将树中结点按照从上层到下层、同层从左到右的次序依次给他们编以从1开始的连续自然数。 有序树、无序树:如果一棵树中结点的各子树从左到右是有次序的,称这棵树为有序树;反之,称为无序树。 森林:m (m≥0)棵互不相交的树的集合。 同构:对两棵树,若通过对结点适当地重命名,就可以使这两棵树完全相等(结点对应相等,结点对应关系也相等),则称这两棵树同构。 前序遍历:树的前序遍历操作定义为: 若树为空,不进行遍历;否则 ⑴ 访问根结点; ⑵ 按照从左到右的顺序前序遍历根结点的每一棵子树。 后序遍历:树的后序遍历操作定义为: 若树为空,则遍历结束;否则 ⑴ 按照从左到右的顺序后序遍历根结点的每一棵子树; ⑵ 访问根结点。 层序遍历:树的层序遍历操作定义为: 从树的第一层(即根结点)开始,自上而下逐层遍历,在同一层中,按从左到右的顺序对结点逐个访问。
Twcat_tree
2022/11/29
3160
Python笔记题编程题答案
最近刚到一个新公司上班,比较忙,不能每天分享文章,希望大家见谅。我会尽最大努力找时间进行分享。
用户7685359
2020/08/24
6560
Python笔记题编程题答案
推荐阅读
相关推荐
PAT(甲级)1143. Lowest Common Ancestor(30)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验