cocos2dx 3.x 网络循环接收数据(RakNet::Packet* packet)单步网络接收

  1  
  2 
  3 void FriendFightLayer::update(float dt)
  4 
  5 {
  6 
  7     dealWithPacket(dt);
  8 
  9     if (m_isNeedSwitchToLobby) {
 10 
 11         PublicMethod::changeToLobby();
 12 
 13     }
 14 
 15 }
 16 
 17 void FriendFightLayer::dealWithPacket(float dt)
 18 
 19 {
 20 
 21     RakNet::Packet* packet = g_net.getNewPacket();
 22 
 23     if (packet == NULL) {
 24 
 25         return;//return之后下面的代码就不执行了,所以除了协议以外的东西,就不能写在里面了。
 26 
 27     }
 28 
 29 //  为什么说是单步呢?看看for循环的条件就知道了,因为有数据进来就进for就会一直取到NULL才会停止
 30 
 31 //    for( RakNet::Packet* packet = g_net.getNewPacket(); packet != NULL; packet = g_net.getNewPacket())
 32 
 33 //    {
 34 
 35  
 36 
 37         if( packet->data[0] == ID_REMOTE_DISCONNECTION_NOTIFICATION)
 38 
 39         {
 40 
 41             CCLOG("远端连接丢失");
 42 
 43             m_isNeedSwitchToLobby = true;
 44 
 45             g_net.releasePacket(packet);
 46 
 47         }
 48 
 49         else if( packet->data[0] == ID_REMOTE_CONNECTION_LOST)
 50 
 51         {
 52 
 53             CCLOG("远端连接丢失");
 54 
 55             m_isNeedSwitchToLobby = true;
 56 
 57             g_net.releasePacket(packet);
 58 
 59         }
 60 
 61         else if( packet->data[0] == ID_REMOTE_NEW_INCOMING_CONNECTION)
 62 
 63         {
 64 
 65             CCLOG("新连接进入");
 66 
 67             g_net.releasePacket(packet);
 68 
 69         }
 70 
 71         else if( packet->data[0] == ID_NEW_INCOMING_CONNECTION)
 72 
 73         {
 74 
 75             CCLOG("新连接进入");
 76 
 77             g_net.releasePacket(packet);
 78 
 79         }
 80 
 81         else if( packet->data[0] == ID_NO_FREE_INCOMING_CONNECTIONS)
 82 
 83         {
 84 
 85             CCLOG("服务器已满");
 86 
 87             m_isNeedSwitchToLobby = true;
 88 
 89             g_net.releasePacket(packet);
 90 
 91         }
 92 
 93         else if( packet->data[0] == ID_DISCONNECTION_NOTIFICATION)
 94 
 95         {
 96 
 97             CCLOG("掉线");
 98 
 99             m_isNeedSwitchToLobby = true;
100 
101             g_net.releasePacket(packet);
102 
103         }
104 
105         else if( packet->data[0] == ID_CONNECTION_LOST)
106 
107         {
108 
109             CCLOG("连接丢失");
110 
111             m_isNeedSwitchToLobby = true;
112 
113             g_net.releasePacket(packet);
114 
115         }
116 
117         else if (packet->data[0] == ID_CONNECTION_ATTEMPT_FAILED)
118 
119         {
120 
121             CCLOG("尝试连接失败");
122 
123             PlatformHelper::showHint("无法连接服务器");
124 
125             MahJong_Foure_Main::s_returnToLobby = true;
126 
127             m_isNeedSwitchToLobby = true;
128 
129             g_net.releasePacket(packet);
130 
131         }
132 
133         else if( packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED)
134 
135         {
136 
137             g_net.m_mahJongFourServerAddress = packet->systemAddress;
138 
139             CCLOG("连接游戏服务器成功");
140 
141             g_net.releasePacket(packet);
142 
143             
144 
145 //            RakNet::BitStream configRequest;
146 
147 //            configRequest.Write((RakNet::MessageID)ID_GET_CONFIG);
148 
149 //            g_net.getPeer()->Send(&configRequest, MEDIUM_PRIORITY, RELIABLE, 0, g_net.getServerAddress(), false);
150 
151             
152 
153             RakNet::BitStream loginRequest;
154 
155             loginRequest.Write((RakNet::MessageID)ID_LOGIN_FROM_CLIENT);//从客户端发送登陆信息
156 
157             loginRequest.Write(DataManager::getInstance()->m_myData.m_id);
158 
159             loginRequest.Write(m_sessionID);
160 
161             loginRequest.Write(m_gameTimes);
162 
163             loginRequest.Write(ConfigData::getInstance()->nProxy);
164 
165             g_net.getPeer()->Send(&loginRequest, MEDIUM_PRIORITY, RELIABLE, 0, g_net.getServerAddress(), false);
166 
167         }
168 
169         else if (packet->data[0] == ID_GET_CONFIG)
170 
171         {
172 
173             dealWithGetConfig(packet);
174 
175         }
176 
177 #pragma mark - 123
178 
179 //        else if(packet->data[0] == ID_PLAYER_LOGIN)
180 
181 //        {
182 
183 ////            LandLordsScene* loadingLayer = LandLordsScene::create();
184 
185 //            CCLOG("ID_PLAYER_LOGIN123456");
186 
187 //        }
188 
189         else if (packet->data[0] == ID_TABLE_LOGIN)
190 
191         {
192 
193             CCLOG("有玩家进入房间);
194 
195         }
196 
197        
198 
199         else if (packet->data[0] == ID_LOGIN_TO_GAME)
200 
201         {
202 
203             dealWithLoginToGame(packet);
204 
205         }
206 
207         else if(packet->data[0] == ID_LOGIN_FROM_CLIENT)
208 
209         {
210 
211             CCLOG("登录游戏服务器成功");
212 
213             dealWithLoginFromClient(packet);
214 
215         }
216 
217 //        else if(packet->data[0] == ID_VIP_SCORE)
218 
219 //        {
220 
221     
222 
223         else if(packet->data[0] == ID_VIP_SCORE)
224 
225         {
226 
227             RakNet::BitStream vipScoreResult( packet->data, packet->length, false);
228 
229             vipScoreResult.IgnoreBytes( sizeof(RakNet::MessageID));
230 
231             RakNet::MessageID realPacketID;
232 
233             vipScoreResult.Read(realPacketID);
234 
235             switch (realPacketID) {
236 
237                 case SVIP_ID_PULLRECORDLIST:
238 
239                     dealWithRecordList(vipScoreResult);
240 
241                     break;
242 
243                 case SVIP_ID_CREATETABLE:
244 
245                     dealWithCreateTable(vipScoreResult);
246 
247                     break;
248 
249                 case SVIP_ID_JOINTABLE:
250 
251                     dealWithJoinTable(vipScoreResult);
252 
253 //                    if(m_selectedName == "landlords")
254 
255 //                    {
256 
257 //                        return;
258 
259 //                    }
260 
261 //                    else
262 
263 //                    {
264 
265                         break;
266 
267 //                    }
268 
269                 case SVIP_ID_EXCHANGE:
270 
271                     CCLOG("SVIP_ID_EXCHANGE:%d",SVIP_ID_EXCHANGE);
272 
273                     dealWithExchange(vipScoreResult);
274 
275                     break;
276 
277                 case SVIP_ID_PULLRECORD:
278 
279                     dealWithRecord(vipScoreResult);
280 
281                     break;
282 
283                     
284 
285 //                case SVIP_ID_DISMISSTABLE_RESULT://解散桌子
286 
287 //                     CCLOG("解散桌子SVIP_ID_DISMISSTABLE_RESULT");
288 
289 //                    break;
290 
291                     
292 
293                 default:
294 
295                     break;
296 
297             }
298 
299             g_net.releasePacket(packet);
300 
301         }
302 
303         else
304 
305         {
306 
307             CCLOG("未知数据报:%d",packet->data[0]);
308 
309         }
310 
311   //  }
312 
313    
314 
315 }
316 
317  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值