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


C++ Packet::Get方法代码示例

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


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

示例1: LogError

bool
Connection::JoinParent()
{
	if (!Connect())
	{
	   LogError(wxString().Format(wxT(" Handshake failed in con id %d"),id_));
		KillSocket(-1);
		return FALSE;
	}

	/*
	Tell our parent hello and it has just born a new child.
	*/
	Packet packet = GenPacket_HelloNext();
	if (!Send(packet))
	{
	   LogError(wxString().Format(wxT(" Send failed in con id %d when trying to say hello to parent"),id_));
		KillSocket(-1);
		return FALSE;
	}

	/*
	Receive all the hashes we are responsible for.
	*/
	Packet* rcv;
	if (!Receive(rcv) || rcv == 0)
	{
	   LogError(wxString().Format(wxT(" Receive failed when getting responsible hashes from parent on con id %d"), id_));
		KillSocket(-1);
		return FALSE;
	}

	KillSocket(-1);

	// add all the hashes we are responsible for to our hash table
	for (int x = 0; x < rcv->Count(Packet::HEADER_HASH);x++)
	{
		wxString hash = rcv->Get(Packet::HEADER_HASH,x);
		wxString node = rcv->Get(Packet::HEADER_NODE,x);
		wxString info = rcv->Get(Packet::HEADER_INFO,x);
		Network::Instance()->hashTable->Add(HashValue(node,hash,info));
	}

	delete rcv;

	return TRUE;
}
开发者ID:digitalpeer,项目名称:turnstile,代码行数:47,代码来源:connection.cpp

示例2: answer

LookupResponse
Connection::DirectQuery(wxString key)
{
	LookupResponse answer(LookupResponse::FAIL);

 	if (!Connect())
	{
	   LogError(wxString().Format(wxT("Connect() failed when doing a direct query in con id %d"),id_));
		KillSocket(-1);
		return answer;
	}

	/*
	Send QUERY.
	*/
	Packet snd = GenPacket_Query(key);
	if (!Send(snd))
	{
	   LogError(wxString().Format(wxT("Send failed when trying to direct query on con id %d"),id_));
		KillSocket(-1);
		return answer;
	}

	/*
	Receive some sort of query response.
	*/
	Packet* rcv;
	if (!Receive(rcv) || rcv == 0)
	{
	   LogError(wxString().Format(wxT("Receive failed when trying to direct query on con id %d"), id_));
		KillSocket(-1);
		return answer;
	}

	KillSocket(-1);

	switch(rcv->Type())
	{
	case Packet::TYPE_RESPONSE_FOUND:
	{
		answer = LookupResponse(LookupResponse::FOUND,Node(currentNid_,currentAddr_));

		for (int x = 0; x < rcv->Count(Packet::HEADER_NODE);x++)
		{
			wxString node = rcv->Get(Packet::HEADER_NODE,x);
			wxString info = rcv->Get(Packet::HEADER_INFO,x);
			answer.AddResponse(HashValue(node,key,info));
		}

		break;
	}
	case Packet::TYPE_RESPONSE_JUMP:
	{
	   answer = LookupResponse(LookupResponse::JUMP,Node(wxT("UNDEFINED"),rcv->Get(Packet::HEADER_JUMP)));
		break;
	}
	case Packet::TYPE_RESPONSE_SUCCESSOR:
	{
		answer = LookupResponse(LookupResponse::SUCCESSOR,Node(currentNid_,currentAddr_));
		break;
	}
	default:
	{
	   LogError(wxString().Format(wxT("Received an invalid lookup response from node on con id %d"), id_));
		break;
	}
	}

	delete rcv;
	return answer;
}
开发者ID:digitalpeer,项目名称:turnstile,代码行数:71,代码来源:connection.cpp

示例3: tokens

// this function does not send events
bool
Connection::ReceiveHandShake()
{
	// TODO check the banned list!

	/*
	Get the SYN or JOIN packet.
	*/
	Packet* rcv;
	if (!Receive(rcv) || rcv == 0)
	{
	   LogError(wxString().Format(wxT("Receive failed on incomming con id %d"), id_));
		return FALSE;
	}

	switch(rcv->Type())
	{
	// node is a network node and wants to handshake
	case Packet::TYPE_SYN:
	{

#ifdef ENABLE_ENCRYPTION
		rsaPub_ = rcv->Get(Packet::HEADER_PUBLIC_KEY);

		if (rsaPub_ == wxT(""))
		{
		   LogError(wxString().Format(wxT("Got empty RSA public key on incomming con id %d"), id_));
			return false;
		}
#endif

		delete rcv;

#ifdef ENABLE_ENCRYPTION
		/*
		Init encryption.
		*/
		symetric_->Init(crypto::RandomString().c_str()/*,(unsigned char*)"blah"*/);
#endif
		/*
		Send the ACK packet.
		*/
#ifdef ENABLE_ENCRYPTION
		Packet snd = GenPacket_Ack(wxString(symetric_->Key().c_str()),wxString(socket_->GetPeerHost().c_str()));
#else
		Packet snd = GenPacket_Ack(wxString(),wxString(socket_->GetPeerHost().c_str()));
#endif
		if (!Send(snd))
		{
		   LogError(wxString().Format(wxT("ACK send failed on incomming con id %d"),id_));
			return FALSE;
		}

		if (!Receive(rcv) || rcv == 0)
		{
		   LogError(wxString().Format(wxT("Receive confirm packet failed on incomming con id %d"), id_));
			return FALSE;
		}

		if (rcv->Type() != Packet::TYPE_CONFIRM)
		{
		   LogError(wxString().Format(wxT("Did not get confirm packet on incomming con id %d, got packet type %d instead"), id_,rcv->Type()));
			return FALSE;
		}

		// TODO get the correct hostname if none is supplied
		currentAddr_ = rcv->Get(Packet::HEADER_SRC_ADDR);

		currentNid_ = rcv->Get(Packet::HEADER_SRC_NID);
		wxString wrkGrpKey = rcv->Get(Packet::HEADER_WRKGRPKEY);
		Network::Instance()->SetGateway(rcv->Get(Packet::HEADER_DEST_HOST));

		if (Prefs::Instance()->Get(Prefs::WRKGRPKEY) != wxT("")
			&&
			wrkGrpKey != Prefs::Instance()->Get(Prefs::WRKGRPKEY))
		{
		   LogError(wxString().Format(wxT("Node tried to connect with invalid workgroup %s key on incomming con id %d packet: %s"), wrkGrpKey.c_str(),id_,rcv->Raw()));
			delete rcv;
			return FALSE;
		}


		if (currentAddr_ == wxT("") || currentNid_ == wxT(""))
		{
		   LogError(wxString().Format(wxT("(3) Blank address or nid from node when trying to handshake on con id %d"),id_));
			delete rcv;
			return FALSE;
		}

		delete rcv;

		/*
		Now that handshaking is out of the way, lets figure out what this node wants.
		*/
		if (!Receive(rcv) || rcv == 0)
		{
		   LogError(wxString().Format(wxT("Receive failed on incomming con id %d"), id_));
			return FALSE;
		}
//.........这里部分代码省略.........
开发者ID:digitalpeer,项目名称:turnstile,代码行数:101,代码来源:connection.cpp

示例4: HandleImEvents

void Connection::HandleImEvents(ImEventQueue* queue)
{
	ImEvent* event;

	while(true/*!(Network::Instance()->KillAll())*/)
	{
		// see if there are any events to send
		event = queue->PopSendEvent();
		if (event != (ImEvent*)0)
		{
			switch (event->type)
			{
			case ImEvent::EVENT_IM:
			{
				Packet snd = GenPacket_Im(event->data);
				if (!Send(snd))
				{
				   LogError(wxString().Format(wxT("Sending im %s on con id %d"),event->data.c_str(),id_));
					PostConEvent(ImEvent::EVENT_LOST);
					delete event;
					return;
				}

				PostConEvent(ImEvent::EVENT_SENT);

				break;
			}
			case ImEvent::EVENT_CLOSE:
			{
				Packet snd = GenPacket_ImClose();
				if (!Send(snd))
				{
				   LogDebug(wxString().Format(wxT("Sending close im event on con id %d"),id_));
					PostConEvent(ImEvent::EVENT_LOST);
					delete event ;
					return;
				}

				break;
			}
			case ImEvent::EVENT_DISCONNECT:
			{
				PostConEvent(ImEvent::EVENT_LOST);
				delete event;
				return;

				break;
			}
			}

			delete event;
		}

		// see if there are any packets to read
		if (socket_->ReadReady())
		{
			Packet* rcv;
			if (Receive(rcv/*,FALSE*/))
			{
				if (rcv != (Packet*)0)
				{
					switch (rcv->Type())
					{
					case Packet::TYPE_IM_CLOSED:
					{
						PostConEvent(ImEvent::EVENT_CLOSE);
						break;
					}
					case Packet::TYPE_IM:
					{
						event = new ImEvent(ImEvent::EVENT_IM,rcv->Get(Packet::HEADER_IM));
						PostConEvent(ImEvent::EVENT_IM,event);
						break;
					}
					}
				}
			}
			else
			{
				PostConEvent(ImEvent::EVENT_LOST);
				return;
			}
		}

		wxThread::Sleep(1);
	}

	PostConEvent(ImEvent::EVENT_LOST);
}
开发者ID:digitalpeer,项目名称:turnstile,代码行数:89,代码来源:connection.cpp

示例5: data

void
Connection::Put(ShareFile* file)
{
	char buffer[FILE_SEG_BUFFER_SIZE+1];
	// the total bytes transfered in this session
	unsigned int totalBytesTransfered = 0;

	LogMsg(wxString().Format(wxT("Uploading file %s ..."),file->Path().c_str()));

	Network::Instance()->AddUpload(file);

	wxFile data(file->Path(), wxFile::read);

	if (!data.IsOpened())
	{
	   LogError(wxT("Failed to open file for reading to upload."));
		file->Status(ShareFile::STATUS_FAILED);
		data.Close();
		return;
	}

	file->Status(ShareFile::STATUS_INPROGRESS);

	memset(buffer, 0, sizeof(*buffer));

	sw_->Start(0);

	while (true)
	{

		if (Network::Instance()->KillAll() ||
			file->Status() == ShareFile::STATUS_CANCELLED)
		{
		   LogDebug(wxT("File transfer thread got signal to exit"));
			file->Status(ShareFile::STATUS_PAUSED);
			data.Close();
			return;
		}

		unsigned int start = 0;
		unsigned int end = 0;

		/*
		Remember, this session does not mean we are transfering a file.  The
		remote host can request any file segment as long as it is from the
		same file requested in any order he wants.  So if he drops the
		connection then he's done asking.
		*/
		Packet* rcv;
		if (!Receive(rcv) || rcv == 0)
		{
			file->Status(ShareFile::STATUS_FINISHED);
			data.Close();
			delete rcv;
			return;
		}

		switch(rcv->Type())
		{
		case Packet::TYPE_CONT:
		{
			wxString startTxt = rcv->Get(Packet::HEADER_FILESEG_START);
			wxString endTxt = rcv->Get(Packet::HEADER_FILESEG_END);
			start = atoi(startTxt.c_str());
			end = atoi(endTxt.c_str());

			if (start < 0 || start >= end || end > file->Size()
				||
			    startTxt == wxT("") || endTxt == wxT("")
				|| end-start > FILE_SEG_BUFFER_SIZE)
			{
			   LogError(wxString().Format(wxT("Invalid start %d or end %d range requested on con id %d"),start,end,id_));
				file->Status(ShareFile::STATUS_FAILED);
				data.Close();
				delete rcv;
				return;
			}
			break;
		}
		default:
		   LogError(wxString().Format(wxT("Did not receive CONT packet when expected on con id %d"), id_));
		}

		if (data.Seek(start, wxFromStart) != start)
		{
			file->Status(ShareFile::STATUS_FAILED);
			data.Close();
			LogError(wxString().Format(wxT("Could not seek to requested file byte: %d"),start));
			return;
		}

		delete rcv;

		unsigned int count = data.Read(buffer,end-start);

		if (count != end-start)
		{
		   LogError(wxString().Format(wxT("Could not read requested file segment from local file on con id %d"),id_));
			file->Status(ShareFile::STATUS_FAILED);
			return;
//.........这里部分代码省略.........
开发者ID:digitalpeer,项目名称:turnstile,代码行数:101,代码来源:connection.cpp

示例6: data

void
FileTransferThread::Get(ShareFile& file)
{
	// save the info file (recovery file) every this count of packets
	const int SAVE_INFO_FILE_EVERY = 10;
	// counter for when to save info file
	int saveInfoFile = 0;
	// the total bytes read in THIS session
	unsigned int totalBytesTransfered = 0;

	LogMsg(wxString().Format("Downloading file %s ...",file.Path().c_str()));

	if (!Connect())
	{
		LogError(wxString().Format("Handshaking failed when starting file transfer in con id %d",id_));
		file.Status(ShareFile::STATUS_FAILED);
		return;
	}

	/*
	Send the request for the file segment.
	*/
	Packet snd = GenPacket_Request(file.Name(),file.MD5());
	if (!Send(snd))
	{
		LogError(wxString().Format("Failed when requesting file on con id %d",id_));
		file.Status(ShareFile::STATUS_FAILED);
		return;
	}

	wxFile data(file.PartPath(), wxFile::write);

	if (!data.IsOpened())
	{
		LogMsg(wxString().Format("Failed to open download file %s for writing",file.Path().c_str()));
		file.Status(ShareFile::STATUS_FAILED);
		data.Close();
		return;
	}

	if (!file.LoadInfoFile())
	{
		file.Status(ShareFile::STATUS_FAILED);
		data.Close();
		LogError("Corrupt info file.");
		return;
	}

	// seek to where we left off
	if (data.Seek(file.CompletedSize(), wxFromStart) != file.CompletedSize())
	{
		file.Status(ShareFile::STATUS_FAILED);
		data.Close();
		LogError("Info file and part file do not match.");
		return;
	}

	file.Status(ShareFile::STATUS_INPROGRESS);

	sw_->Start(0);
	bool first = true;

	while (true)
	{
		if (Network::Instance()->KillAll() ||
			file.Status() == ShareFile::STATUS_CANCELLED ||
			file.Status() == ShareFile::STATUS_PAUSED)
		{
			//LogDebug("File transfer thread got signal to exit");
			file.Status(ShareFile::STATUS_PAUSED);
			data.Close();
			return;
		}
		else if (file.Size() == file.CompletedSize())
		{
			LogMsg(wxString().Format("Download of file %s completed.",file.Path().c_str()));
			file.Status(ShareFile::STATUS_FINISHED);

			file.PartToReal();

			if (crypto::Md5Hash(file.Path()) != file.MD5())
			{
				LogError("MD5's don't match!  File is corrupt.");
				file.MD5(file.MD5() + " (Downloaded file does not match MD5)");
				file.Status(ShareFile::STATUS_FAILED);
			}

			data.Close();
			return;
		}
		else if (file.Size() < file.CompletedSize())
		{
			LogError("Got too many bytes in file!");
			file.Status(ShareFile::STATUS_FAILED);
			data.Close();
			return;
		}

		unsigned int end;

//.........这里部分代码省略.........
开发者ID:digitalpeer,项目名称:turnstile,代码行数:101,代码来源:filetransferthread.cpp


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