当前位置: 首页>>代码示例>>C++>>正文


C++ PacketReader::next方法代码示例

本文整理汇总了C++中PacketReader::next方法的典型用法代码示例。如果您正苦于以下问题:C++ PacketReader::next方法的具体用法?C++ PacketReader::next怎么用?C++ PacketReader::next使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PacketReader的用法示例。


在下文中一共展示了PacketReader::next方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: unpack

AMF::ContentType RTMFPFlow::unpack(PacketReader& packet,UInt32& time) {
	if(packet.available()==0)
		return AMF::EMPTY;
	AMF::ContentType type = (AMF::ContentType)packet.read8();
	switch(type) {
		// amf content
		case AMF::INVOCATION_AMF3:
			packet.next(1);
		case AMF::INVOCATION:
			packet.next(4);
			return AMF::INVOCATION;
		case AMF::AUDIO:
		case AMF::VIDEO:
			time = packet.read32();
			break;
		case AMF::DATA:
			packet.next(1);
		case AMF::RAW:
			packet.next(4);
		case AMF::CHUNKSIZE:
			break;
		default:
			ERROR("Unpacking type '",Format<UInt8>("%02x",(UInt8)type),"' unknown");
			break;
	}
	return type;
}
开发者ID:blueram,项目名称:MonaServer,代码行数:27,代码来源:RTMFPFlow.cpp

示例2: unpack

Message::Type Flow::unpack(PacketReader& reader) {
	if(reader.available()==0)
		return Message::EMPTY;
	Message::Type type = (Message::Type)reader.read8();
	switch(type) {
		// amf content
		case 0x11:
			reader.next(1);
		case Message::AMF_WITH_HANDLER:
			reader.next(4);
			return Message::AMF_WITH_HANDLER;
		case Message::AMF:
			reader.next(5);
		case Message::AUDIO:
		case Message::VIDEO:
			break;
		// raw data
		case 0x04:
			reader.next(4);
		case 0x01:
			break;
		default:
			ERROR("Unpacking type '%02x' unknown",type);
			break;
	}
	return type;
}
开发者ID:wilkystorm,项目名称:Cumulus,代码行数:27,代码来源:Flow.cpp

示例3: packetHandler

void ServerConnection::packetHandler(PacketReader& packet) {

	UInt8 marker = packet.read8();
	if(marker!=0x0b) {
		ERROR("ServerConnection with an unknown %u marker, it should be 0x0b",marker);
		return;
	}

	packet.next(2);
	UInt8 id = packet.read8();
	switch(id) {
		case 0x71: {
			packet.next(2);
			string tag;
			packet.readString8(tag);
			map<string,P2PHandshakerAddress>::iterator it = _p2pHandshakers.find(tag);
			if(it==_p2pHandshakers.end()) {
				ERROR("Unknown ServerConnection tag %s on P2P handshake",tag.c_str());
				break;
			}

			(SocketAddress&)_handshake.peer.address = it->second;
			packet.reset(0);
			PacketWriter writer(packet.current(),packet.available()+16); // +16 for futur 0xFFFF padding
			writer.clear(packet.available());
			_handshake.send(writer);
			_p2pHandshakers.erase(it);

			break;
		}
		case 0x40: {
			if(!_connected) {
				// Edge hello response
				_connected=true;
				return;
			}
			// Edge keepalive
			PacketWriter& packet(writer());
			packet.write8(0x41);
			packet.write16(0);
			flush();
			INFO("Keepalive RTMFP server");
			break;
		}
		case 0x45: {
			// Server is death!
			(bool&)died=true;
			break;
		}
		default:
			ERROR("Unkown ServerConnection packet id %u",id);
	}


}
开发者ID:Windslash,项目名称:Cumulus,代码行数:55,代码来源:ServerConnection.cpp

示例4: packetHandler


//.........这里部分代码省略.........
				// I don't unsertand the usefulness...
				//pFlow = &flow(message.read8());
				//stage = pFlow->stageSnd();
				// For the moment, we considerate it like a exception
				fail("ack negative from server"); // send fail message immediatly
				break;

			case 0x51 : {
				/// Acknowledgment
				UInt64 id = message.read7BitLongValue();
				RTMFPWriter* pRTMFPWriter = writer(id);
				if(pRTMFPWriter)
					pRTMFPWriter->acknowledgment(message);
				else
					WARN("RTMFPWriter ",id," unfound for acknowledgment on session ",name());
				break;
			}
			/// Request
			// 0x10 normal request
			// 0x11 special request, in repeat case (following stage request)
			case 0x10 : {
				flags = message.read8();
				UInt64 idFlow = message.read7BitLongValue();
				stage = message.read7BitLongValue()-1;
				deltaNAck = message.read7BitLongValue()-1;
				
				if (_failed)
					break;

				map<UInt64,RTMFPFlow*>::const_iterator it = _flows.find(idFlow);
				pFlow = it==_flows.end() ? NULL : it->second;

				// Header part if present
				if(flags & MESSAGE_HEADER) {
					string signature;
					message.readString8(signature);

					if(!pFlow)
						pFlow = createFlow(idFlow,signature);

					if(message.read8()>0) {

						// Fullduplex header part
						if(message.read8()!=0x0A)
							WARN("Unknown fullduplex header part for the flow ",idFlow)
						else 
							message.read7BitLongValue(); // Fullduplex useless here! Because we are creating a new RTMFPFlow!

						// Useless header part 
						UInt8 length=message.read8();
						while(length>0 && message.available()) {
							WARN("Unknown message part on flow ",idFlow);
							message.next(length);
							length=message.read8();
						}
						if(length>0)
							ERROR("Bad header message part, finished before scheduled");
					}

				}
				
				if(!pFlow) {
					WARN("RTMFPFlow ",idFlow," unfound");
					((UInt64&)_pFlowNull->id) = idFlow;
					pFlow = _pFlowNull;
				}

			}	
			case 0x11 : {
				++stage;
				++deltaNAck;

				// has Header?
				if(type==0x11)
					flags = message.read8();

				// Process request
				if (pFlow)
					pFlow->fragmentHandler(stage, deltaNAck, message, flags);

				break;
			}
			default :
				ERROR("RTMFPMessage type '", Format<UInt8>("%02x", type), "' unknown");
		}

		// Next
		packet.next(size);
		type = packet.available()>0 ? packet.read8() : 0xFF;

		// Commit RTMFPFlow
		if(pFlow && type!= 0x11) {
			pFlow->commit();
			if(pFlow->consumed()) {
				_flows.erase(pFlow->id);
				delete pFlow;
			}
			pFlow=NULL;
		}
	}
开发者ID:jaejungkim,项目名称:MonaServer,代码行数:101,代码来源:RTMFPSession.cpp

示例5: packetHandler

void Middle::packetHandler(PacketReader& packet) {
	if(!_pMiddleAesEncrypt)
		manage(); // to wait the target handshake response

	// Middle to target
	PacketWriter& request = requester();

	UInt8 marker = packet.read8();
	request << marker;

	request << packet.read16();

	if((marker|0xF0) == 0xFD)
		request.write16(packet.read16()); // time echo

	int pos = request.position();

	UInt8 type = packet.available()>0 ? packet.read8() : 0xFF;
	while(type!=0xFF) {
		UInt16 size = packet.read16();
		PacketReader content(packet.current(),size);

		PacketWriter out(request.begin()+request.position(),request.available()); // 3 for future type and size
		out.clear(3);

		if(type==0x10) {

			out.write8(content.read8());
			UInt64 idFlow = content.read7BitLongValue();out.write7BitLongValue(idFlow);
			UInt64 stage = content.read7BitLongValue();out.write7BitLongValue(stage);

			if(idFlow==0x02 && stage==0x01) {
				if(!_isPeer) {
				
					/// Replace NetConnection infos

					out.writeRaw(content.current(),14);content.next(14);

					// first string
					string tmp;
					content.readString16(tmp);out.writeString16(tmp);

					AMFWriter writer(out);
					writer.amf0Preference=true;
					AMFReader reader(content);
					writer.writeNumber(reader.readNumber()); // double
					
					AMFSimpleObject obj;
					reader.readSimpleObject(obj);
					
					/// Replace tcUrl
					if(obj.has("tcUrl"))
						obj.setString("tcUrl",_queryUrl);
					
					writer.writeSimpleObject(obj);

				} else {

					out.writeRaw(content.current(),3);content.next(3);
					UInt16 netGroupHeader = content.read16();out.write16(netGroupHeader);
					if(netGroupHeader==0x4752) {

						map<string,string>::const_iterator it = peer.properties.find("groupspec");
						if(it!=peer.properties.end()) {
							out.writeRaw(content.current(),71);content.next(71);
							UInt8 result1[AES_KEY_SIZE];
							UInt8 result2[AES_KEY_SIZE];
							HMAC(EVP_sha256(),&_sharedSecret[0],_sharedSecret.size(),&_targetNonce[0],_targetNonce.size(),result1,NULL);
							HMAC(EVP_sha256(),it->second.c_str(),it->second.size(),result1,AES_KEY_SIZE,result2,NULL);
							out.writeRaw(result2,AES_KEY_SIZE);content.next(AES_KEY_SIZE);
							out.writeRaw(content.current(),4);content.next(4);
							out.writeRaw(_target.peerId,ID_SIZE);content.next(ID_SIZE);
						} else
							WARN("No groupspec client property indicated to make working the 'man-in-the-middle' mode between peers in a NetGroup case");
						
					}
				}
			}

		}  else if(type == 0x4C) {
			 kill();
			 return;
		}  else if(type == 0x51) {
			//printf("%s\n",Util::FormatHex(content.current(),content.available()).c_str());
		}
		out.writeRaw(content.current(),content.available());

		packet.next(size);

		if(out.length()>=3) {
			request<<type;
			size = out.length()-3;
			request.write16(size);request.next(size);
		}


		type = packet.available()>0 ? packet.read8() : 0xFF;
	}

	if(request.length()>pos)
//.........这里部分代码省略.........
开发者ID:OpenRTMFP,项目名称:Cumulus,代码行数:101,代码来源:Middle.cpp

示例6: handshakeHandler

UInt8 Handshake::handshakeHandler(UInt8 id,PacketReader& request,PacketWriter& response) {

	switch(id){
		case 0x30: {
			
			request.read8(); // passer un caractere (boite dans boite)
			UInt8 epdLen = request.read8()-1;

			UInt8 type = request.read8();

			string epd;
			request.readRaw(epdLen,epd);

			string tag;
			request.readRaw(16,tag);
			response.writeString8(tag);
			
			if(type == 0x0f)
				return _gateway.p2pHandshake(tag,response,peer.address,(const UInt8*)epd.c_str());

			if(type == 0x0a){
				/// Handshake
				HelloAttempt& attempt = helloAttempt<HelloAttempt>(tag);

				// Fill peer infos
				UInt16 port;
				string host;
				Util::UnpackUrl(epd,host,port,(string&)peer.path,(map<string,string>&)peer.properties);
				set<string> addresses;
				peer.onHandshake(attempt.count+1,addresses);
				if(!addresses.empty()) {
					set<string>::iterator it;
					for(it=addresses.begin();it!=addresses.end();++it) {
						try {
							if((*it)=="again")
								((string&)*it).assign(format("%s:%hu",host,port));
							SocketAddress address(*it);
							response.writeAddress(address,it==addresses.begin());
						} catch(Exception& ex) {
							ERROR("Bad redirection address %s in hello attempt, %s",(*it)=="again" ? epd.c_str() : (*it).c_str(),ex.displayText().c_str());
						}
					}
					return 0x71;
				}

				// New Cookie
				createCookie(response,attempt,tag,epd);

				// instance id (certificat in the middle)
				response.writeRaw(_certificat,sizeof(_certificat));
				return 0x70;
			} else
				ERROR("Unkown handshake first way with '%02x' type",type);
			break;
		}
		case 0x38: {
			(UInt32&)farId = request.read32();

			if(request.read7BitLongValue()!=COOKIE_SIZE) {
				ERROR("Bad handshake cookie '%s': its size should be 64 bytes",Util::FormatHex(request.current(),COOKIE_SIZE).c_str());
				return 0;
			}
	
			map<const UInt8*,Cookie*,CompareCookies>::iterator itCookie = _cookies.find(request.current());
			if(itCookie==_cookies.end()) {
				WARN("Cookie %s unknown, maybe already connected (udpBuffer congested?)",Util::FormatHex(request.current(),COOKIE_SIZE).c_str());
				return 0;
			}

			Cookie& cookie(*itCookie->second);
			(SocketAddress&)cookie.peerAddress = peer.address;

			if(cookie.farId==0) {
				((UInt32&)cookie.farId) = farId;
				request.next(COOKIE_SIZE);

				size_t size = (size_t)request.read7BitLongValue();
				// peerId = SHA256(farPubKey)
				EVP_Digest(request.current(),size,(UInt8*)cookie.peerId,NULL,EVP_sha256(),NULL);

				cookie.initiatorKey().resize(request.read7BitValue()-2);
				request.next(2); // unknown
				request.readRaw(&cookie.initiatorKey()[0],cookie.initiatorKey().size());

				cookie.initiatorNonce().resize(request.read7BitValue());
				request.readRaw(&cookie.initiatorNonce()[0],cookie.initiatorNonce().size());

				cookie.computeKeys();
			} else if(cookie.id>0) {
				// Repeat cookie reponse!
				cookie.read(response);
				return 0x78;
			} // else Keys are computing (multi-thread)

			break;
		}
		default:
			ERROR("Unkown handshake packet id %u",id);
	}

//.........这里部分代码省略.........
开发者ID:null-null-cn,项目名称:Cumulus,代码行数:101,代码来源:Handshake.cpp

示例7: handshakeHandler


//.........这里部分代码省略.........
								break;
						}
						if(it3!=it2->second.end())
							break;
					}
					return 0x71;

				}

				if(edges().size()>0)
					WARN("After %u hello attempts, impossible to connect to edges. Edges are busy? or unreachable?",_invoker.edgesAttemptsBeforeFallback);
	
				// New Cookie
				createCookie(response,attempt,tag,epd);
				 
				// instance id (certificat in the middle)
				response.writeRaw(_certificat,sizeof(_certificat));
				
				return 0x70;
			} else
				ERROR("Unkown handshake first way with '%02x' type",type);
			break;
		}
		case 0x39:
		case 0x38: {
			(UInt32&)farId = request.read32();

			if(request.read7BitLongValue()!=COOKIE_SIZE) {
				ERROR("Bad handshake cookie '%s': its size should be 64 bytes",Util::FormatHex(request.current(),COOKIE_SIZE).c_str());
				return 0;
			}
	
			map<const UInt8*,Cookie*,CompareCookies>::iterator itCookie = _cookies.find(request.current());
			if(itCookie==_cookies.end()) {
				if(id!=0x39) {
					ERROR("Handshake cookie '%s' unknown",Util::FormatHex(request.current(),COOKIE_SIZE).c_str());
					return 0;
				}
				Cookie* pCookie = new Cookie();
				UInt32 pos = request.position();
				request.readRaw((UInt8*)pCookie->value,COOKIE_SIZE);
				request >> (string&)pCookie->queryUrl;
				request.reset(pos);
				itCookie = _cookies.insert(pair<const UInt8*,Cookie*>(pCookie->value,pCookie)).first;
			}

			Cookie& cookie(*itCookie->second);

			if(cookie.id==0) {

				UInt8 decryptKey[AES_KEY_SIZE];UInt8* pDecryptKey=&decryptKey[0];
				UInt8 encryptKey[AES_KEY_SIZE];UInt8* pEncryptKey=&encryptKey[0];

				if(id==0x38) {
					request.next(COOKIE_SIZE);
					size_t size = (size_t)request.read7BitLongValue();
					// peerId = SHA256(farPubKey)
					EVP_Digest(request.current(),size,(UInt8*)peer.id,NULL,EVP_sha256(),NULL);

					vector<UInt8> publicKey(request.read7BitValue()-2);
					request.next(2); // unknown
					request.readRaw(&publicKey[0],publicKey.size());

					size = request.read7BitValue();

					cookie.computeKeys(&publicKey[0],publicKey.size(),request.current(),(UInt16)size,decryptKey,encryptKey);
				} else {
					// edge
					pDecryptKey=NULL;
					pEncryptKey=NULL;
					memcpy((UInt8*)peer.id,request.current(),ID_SIZE);
					request.next(COOKIE_SIZE);
					request.next(request.read7BitEncoded());
				}

				// Fill peer infos
				Util::UnpackUrl(cookie.queryUrl,(string&)peer.path,(map<string,string>&)peer.properties);

				// RESPONSE
				Session& session = _gateway.createSession(farId,peer,pDecryptKey,pEncryptKey,cookie);
				(UInt32&)cookie.id = session.id;

				string address;
				if(id==0x39) {
					// Session by edge 
					session.flags |= SESSION_BY_EDGE;
					Edge* pEdge = _invoker.edges(peer.address);
					if(!pEdge)
						ERROR("Edge session creation by an unknown server edge %s",peer.address.toString().c_str())
					else
						pEdge->addSession(session);
					request >> address;
				} else // Session direct
					address = session.peer.address.toString();

				session.peer.addresses.clear();
				session.peer.addresses.push_back(address);

				cookie.write();
			} else
开发者ID:DQvsRA,项目名称:Cumulus,代码行数:101,代码来源:Handshake.cpp

示例8: packetHandler


//.........这里部分代码省略.........
				/// Acknowledgment
				UInt32 id = message.read7BitValue();
				FlowWriter* pFlowWriter = flowWriter(id);
				if(pFlowWriter) {
					UInt8 ack = message.read8();
					while(ack==0xFF)
						ack = message.read8();
					if(ack>0)
						pFlowWriter->acknowledgment(message.read7BitValue());
					else {
						// In fact here, we should send a 0x18 message (with id flow),
						// but it can create a loop... We prefer the following behavior
						pFlowWriter->fail("ack negative from client");
					}

				} else
					WARN("FlowWriter %u unfound for acknowledgment",id);
				break;
			}
			/// Request
			// 0x10 normal request
			// 0x11 special request, in repeat case (following stage request)
			case 0x10 : {
				flags = message.read8();
				UInt32 idFlow = message.read7BitValue();
				stage = message.read7BitValue()-1;
				deltaNAck = message.read7BitValue()-1;

				map<UInt32,Flow*>::const_iterator it = _flows.find(idFlow);
				pFlow = it==_flows.end() ? NULL : it->second;

				// Header part if present
				if(flags & MESSAGE_HEADER) {
					string signature;
					message.readString8(signature);

					if(!pFlow)
						pFlow = createFlow(idFlow,signature);

					if(message.read8()>0) {

						// Fullduplex header part
						if(message.read8()!=0x0A)
							WARN("Unknown fullduplex header part for the flow '%u'",idFlow)
						else
							message.read7BitValue(); // Fullduplex useless here! Because we are creating a new Flow!

						// Useless header part 
						UInt8 length=message.read8();
						while(length>0 && message.available()) {
							WARN("Unknown message part on flow '%u'",idFlow);
							message.next(length);
							length=message.read8();
						}
						if(length>0)
							ERROR("Bad header message part, finished before scheduled");
					}

				}
				
				if(!pFlow) {
					WARN("Flow %u unfound",idFlow);
					((UInt32&)_pFlowNull->id) = idFlow;
					pFlow = _pFlowNull;
				}

			}	
			case 0x11 : {
				++stage;
				++deltaNAck;

				// has Header?
				if(type==0x11)
					flags = message.read8();

				// Process request
				pFlow->fragmentHandler(stage,deltaNAck,message,flags);
				if(!pFlow->error().empty())
					fail(pFlow->error()); // send fail message immediatly

				break;
			}
			default :
				ERROR("Message type '%02x' unknown",type);
		}

		// Next
		packet.next(size);
		type = packet.available()>0 ? packet.read8() : 0xFF;

		// Commit Flow
		if(pFlow && stage>0 && type!= 0x11) {
			pFlow->commit();
			if(pFlow->consumed()) {
				_flows.erase(pFlow->id);
				delete pFlow;
			}
			pFlow=NULL;
		}
	}
开发者ID:holdensmagicalunicorn,项目名称:Cumulus,代码行数:101,代码来源:Session.cpp

示例9: targetPacketHandler

void Middle::targetPacketHandler(PacketReader& packet) {

	if(_firstResponse)
		_recvTimestamp.update(); // To emulate a long ping corresponding, otherwise client send multiple times each packet
	_firstResponse = false;

	UInt8 marker = packet.read8();
	
	UInt16 timestamp = packet.read16(); // time

	if((marker|0xF0) == 0xFE)
		_timeSent = packet.read16(); // time echo

	PacketWriter& packetOut = writer();

	int pos = packetOut.position();

	UInt8 type = packet.available()>0 ? packet.read8() : 0xFF;

	UInt32 idFlow,stage;
	UInt8 nbPeerSent = 0;

	while(type!=0xFF) {
		int posType = packetOut.position();
		packetOut.write8(type);

		UInt16 size = packet.read16();
		PacketReader content(packet.current(),size);packetOut.write16(size);
		
		if(type==0x10 || type==0x11) {
			UInt8 flag = content.read8();packetOut.write8(flag);
			if(type==0x10) {
				idFlow = content.read7BitValue();packetOut.write7BitValue(idFlow);
				stage = content.read7BitValue();packetOut.write7BitValue(stage);
			} else
				++stage;

			packetOut.write7BitValue(content.read7BitValue());
			
			if(!(flag&MESSAGE_WITH_BEFOREPART)) {

				if(flag&MESSAGE_HEADER) {
					UInt8 len = content.read8(); packetOut.write8(len);
					while(len!=0) {
						packetOut.writeRaw(content.current(),len);
						content.next(len);
						len = content.read8(); packetOut.write8(len);
					}
				}

				UInt8 flagType = content.read8(); packetOut.write8(flagType);
				if(flagType==0x09) {
					UInt32 time = content.read32(); packetOut.write32(time);
					//TRACE("Timestamp/Flag video : %u/%2x",time,*content.current());
				} else if(flagType==0x08) {
					UInt32 time = content.read32(); packetOut.write32(time);
					//TRACE("Timestamp/Flag audio : %u/%2x",time,*content.current());
				} else if(flagType==0x04) {
					packetOut.write32(content.read32());
					UInt16 a = content.read16(); packetOut.write16(a);
					UInt32 b = content.read32(); packetOut.write32(b);
					UInt32 c = content.read32(); packetOut.write32(c);
					//TRACE("Bound %u : %u %u %u",idFlow,a,b,c);
				}

				if(flagType==0x0b && stage==0x01 && ((marker==0x4e && idFlow==0x03) || (marker==0x8e && idFlow==0x05))) {
					/// Replace "middleId" by "peerId"	

					UInt8 middlePeerIdWanted[ID_SIZE];
					content.readRaw(middlePeerIdWanted,ID_SIZE);

					++nbPeerSent;

					Sessions::Iterator it;
					for(it=_sessions.begin();it!=_sessions.end();++it) {
						Middle* pMiddle = (Middle*)it->second;
						if(pMiddle->middlePeer() == middlePeerIdWanted) {
							memcpy(middlePeerIdWanted,pMiddle->peer.id,ID_SIZE);
							break;
						}
					}
					packetOut.writeRaw(middlePeerIdWanted,ID_SIZE);	

				} else if(flagType == 0x01) {

					packetOut.writeRaw(content.current(),68);content.next(68);
					
					Entities<Group>::Iterator it;
					for(it = _invoker.groups.begin();it!=_invoker.groups.end();++it) {

						Group& group = *it->second;						
			
						Group::Iterator itP;
						for(itP=group.begin();itP!=group.end();++itP) {
							if((**itP)==_target.id) {
								UInt8 result1[AES_KEY_SIZE];
								UInt8 result2[AES_KEY_SIZE];
								HMAC(EVP_sha256(),_target.sharedSecret,KEY_SIZE,&_target.initiatorNonce[0],_target.initiatorNonce.size(),result1,NULL);
								HMAC(EVP_sha256(),group.id,ID_SIZE,result1,AES_KEY_SIZE,result2,NULL);
								packetOut.writeRaw(result2,AES_KEY_SIZE);content.next(AES_KEY_SIZE);
//.........这里部分代码省略.........
开发者ID:cvarrick,项目名称:Cumulus,代码行数:101,代码来源:Middle.cpp

示例10: packetHandler

void Middle::packetHandler(PacketReader& packet) {
	if(!_pMiddleAesEncrypt) {
		DEBUG("500ms sleeping to wait target handshaking");
		Thread::sleep(500); // to wait the target handshake response
		manage();
	}

	// Middle to target
	PacketWriter& request = requester();

	UInt8 marker = packet.read8();
	request << marker;

	request << packet.read16();

	if((marker|0xF0) == 0xFD)
		request.write16(packet.read16()); // time echo

	int pos = request.position();

	UInt8 type = packet.available()>0 ? packet.read8() : 0xFF;
	while(type!=0xFF) {
		UInt16 size = packet.read16();
		PacketReader content(packet.current(),size);

		PacketWriter out(request.begin()+request.position(),request.available()); // 3 for future type and size
		out.clear(3);

		if(type==0x10) {

			out.write8(content.read8());
			UInt32 idFlow = content.read7BitValue();out.write7BitValue(idFlow);
			UInt32 stage = content.read7BitValue();out.write7BitValue(stage);

			if(idFlow==0x02 && stage==0x01) {
				if(!_isPeer) {
				
					/// Replace NetConnection infos

					out.writeRaw(content.current(),14);content.next(14);

					// first string
					string tmp;
					content.readString16(tmp);out.writeString16(tmp);

					AMFWriter writer(out);
					writer.amf0Preference=true;
					AMFReader reader(content);
					writer.writeNumber(reader.readNumber()); // double
					
					AMFSimpleObject obj;
					reader.readSimpleObject(obj);
					
					/// Replace tcUrl
					if(obj.has("tcUrl"))
						obj.setString("tcUrl",_queryUrl);
					
					writer.writeSimpleObject(obj);

				} else {

					out.writeRaw(content.current(),3);content.next(3);
					UInt16 netGroupHeader = content.read16();out.write16(netGroupHeader);
					if(netGroupHeader==0x4752) {
						out.writeRaw(content.current(),71);content.next(71);
						
						Entities<Group>::Iterator it;
						for(it = _invoker.groups.begin();it!=_invoker.groups.end();++it) {
	
							Group& group = *it->second;						
			
							Group::Iterator itP;
							for(itP=group.begin();itP!=group.end();++itP) {
								if((**itP)==_target.id) {
									UInt8 result1[AES_KEY_SIZE];
									UInt8 result2[AES_KEY_SIZE];
									HMAC(EVP_sha256(),_sharedSecret,KEY_SIZE,&_targetNonce[0],_targetNonce.size(),result1,NULL);
									HMAC(EVP_sha256(),group.id,ID_SIZE,result1,AES_KEY_SIZE,result2,NULL);
									out.writeRaw(result2,AES_KEY_SIZE);content.next(AES_KEY_SIZE);
									out.writeRaw(content.current(),4);content.next(4);
									out.writeRaw(_target.peerId,ID_SIZE);content.next(ID_SIZE);
									break;
								}
							}
							if(itP!=group.end())
								break;

						}
						if(it==_invoker.groups.end())
							ERROR("Handshake NetGroup packet between peers without corresponding Group");
						
					}
				}
			}

		}  else if(type == 0x4C) {
			 kill();
		}  else if(type == 0x51) {
			//printf("%s\n",Util::FormatHex(content.current(),content.available()).c_str());
		}
//.........这里部分代码省略.........
开发者ID:cvarrick,项目名称:Cumulus,代码行数:101,代码来源:Middle.cpp

示例11: handshakeHandler

UInt8 RTMFPHandshake::handshakeHandler(UInt8 id,PacketReader& request,PacketWriter& response) {

	switch(id){
		case 0x30: {
			
			request.next(1);
			UInt8 epdLen = request.read8()-1;

			UInt8 type = request.read8();

			string epd;
			request.readRaw(epdLen,epd);

			string tag;
			request.readRaw(16,tag);
			response.writeString8(tag);
			
			if(type == 0x0f) {

				const UInt8* peerId = (const UInt8*)epd.c_str();
				
				RTMFPSession* pSessionWanted = _sessions.find<RTMFPSession>(peerId);
	
				if(pSessionWanted) {
					if(pSessionWanted->failed())
						return 0x00; // TODO no way in RTMFP to tell "died!"
					/// Udp hole punching
					UInt32 times = attempt(tag);

					RTMFPSession* pSession(NULL);
					if(times > 0 || peer.address.host() == pSessionWanted->peer.address.host())
						pSession = _sessions.find<RTMFPSession>(peer.address);
					
					pSessionWanted->p2pHandshake(tag,peer.address,times,pSession);

					RTMFP::WriteAddress(response,pSessionWanted->peer.address, RTMFP::ADDRESS_PUBLIC);
					DEBUG("P2P address initiator exchange, ",pSessionWanted->peer.address.toString());
					for(const SocketAddress& address : pSessionWanted->peer.localAddresses) {
						RTMFP::WriteAddress(response,address, RTMFP::ADDRESS_LOCAL);
						DEBUG("P2P address initiator exchange, ",address.toString());
					}

					// add the turn address (RelayServer) if possible and required
					if (pSession && times>0) {
						UInt8 timesBeforeTurn(0);
						if(pSession->peer.parameters().getNumber("timesBeforeTurn",timesBeforeTurn) && timesBeforeTurn>=times) {
							UInt16 port = invoker.relayer.relay(pSession->peer.address,pSessionWanted->peer.address,20); // 20 sec de timeout is enough for RTMFP!
							if(port>0) {
								Exception ex;
								SocketAddress address;
								string addr;
								SocketAddress::Split(pSession->peer.serverAddress,addr);
								bool success(false);
								EXCEPTION_TO_LOG(success=address.set(ex,addr, port),"RTMFP turn impossible")
								if (success)
									RTMFP::WriteAddress(response,address, RTMFP::ADDRESS_REDIRECTION);
							} // else ERROR already display by RelayServer class
						}
					}
					return 0x71;
				}


				DEBUG("UDP Hole punching, session ", Util::FormatHex(peerId, ID_SIZE, LOG_BUFFER), " wanted not found")
				set<SocketAddress> addresses;
				peer.onRendezVousUnknown(peerId,addresses);
				set<SocketAddress>::const_iterator it;
				for(it=addresses.begin();it!=addresses.end();++it) {
					if(it->host().isWildcard())
						continue;
					if(peer.address == *it)
						WARN("A client tries to connect to himself (same ",peer.address.toString()," address)");
					RTMFP::WriteAddress(response,*it,RTMFP::ADDRESS_REDIRECTION);
					DEBUG("P2P address initiator exchange, ",it->toString());
				}
				return addresses.empty() ? 0 : 0x71;
			}
开发者ID:taililong,项目名称:MonaServer,代码行数:77,代码来源:RTMFPHandshake.cpp


注:本文中的PacketReader::next方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。