📄 kadmain.cpp
字号:
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "kademlia/Kademlia.h"
#include "kademlia/Error.h"
#include "kademlia/Search.h"
#include "kademlia/KadFile.h"
#include "routing/Contact.h"
#include "net/KademliaUDPListener.h"
#include "KademliaMain.h"
#include "KadMain.h"
#include "opcodes.h"
#include <deque>
#include <vector>
#include <map>
using namespace Kademlia;
using namespace std;
#define FILE_HASH_LEN 24 // Tiger Tree Hash
KADAPI double GetKademliaVersion()
{
return KADEMLIA_LIBRARY_VERSION;
}
static CKademliaMain* pKademlia = NULL;
static CPrefs* startupKadPref = NULL;
static deque<CKadFile> publish_file_list;
static map<CUInt128, FileList> publish_keyword_list;
static CCriticalSection m_critical;
static void PublishNextFile();
static void PublishNextKeyword();
static FileResultHandler FindFileResultHandler = NULL;
static KeywordResultHandler FindKeywordResultHandler = NULL;
void CALLBACK KademliaSearchAddCallback (CSearch* search);
void CALLBACK KademliaSearchRemCallback (CSearch* search);
void CALLBACK KademliaSearchRefCallback (CSearch* search);
void CALLBACK KademliaContactAddCallback (CContact* contact);
void CALLBACK KademliaContactRemCallback (CContact* contact);
void CALLBACK KademliaContactRefCallback (CContact* contact);
void CALLBACK KademliaContactUpdateCallback (CContact* contact);
void CALLBACK KademliaIndexedAddCallback (CEntry* contact);
void CALLBACK KademliaIndexedRemCallback (CEntry* contact);
void CALLBACK KademliaIndexedRefCallback (CEntry* contact);
void CALLBACK KademliaResultFileCallback (uint32 searchID, CUInt128 contactID, uint8 type, uint32 ip, uint16 tcp, uint16 udp, uint32 serverip, uint16 serverport);
//void CALLBACK KademliaResultKeywordCallback (uint32 searchID, CUInt128 fileID, LPCSTR name, uint32 size, LPCSTR type, uint16 numProperties, ...);
void CALLBACK KademliaResultKeywordCallback (uint32 searchID, CUInt128 fileID, LPCSTR name, uint32 size, LPCSTR type, uint32 ip, uint16 tcp, uint32 tth_1, uint32 tth_2);
void CALLBACK KademliaRequestTCPCallback (CContact* contact);
void CALLBACK KademliaUpdateStatusCallback (Status* status);
void CALLBACK KademliaOverheadSendCallback (uint32 size);
void CALLBACK KademliaOverheadRecvCallback (uint32 size);
void CALLBACK myDebugAndLogHandler(LPCSTR lpMsg);
void CALLBACK myLogHandler(LPCSTR lpMsg);
void CALLBACK myErrHandler(CKademliaError *error);
///////////////////////////////////////////////////////////////////////////////
// those funcs are called from a different thread
// to stay synchronized with the main app each function+data is forced through the
// app message queue
void CALLBACK KademliaSearchAddCallback(CSearch* search)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_SEARCHADD, 0, (LPARAM)search);
}
void CALLBACK KademliaSearchRemCallback(CSearch* search)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_SEARCHREM, 0, (LPARAM)search);
}
void CALLBACK KademliaSearchRefCallback(CSearch* search)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_SEARCHREF, 0, (LPARAM)search);
}
void CALLBACK KademliaContactAddCallback(CContact* contact)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_CONTACTADD, 0, (LPARAM)contact);
}
void CALLBACK KademliaContactRemCallback(CContact* contact)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_CONTACTREM, 0, (LPARAM)contact);
}
void CALLBACK KademliaContactRefCallback(CContact* contact)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_CONTACTREF, 0, (LPARAM)contact);
}
//void CALLBACK KademliaResultFileCallback(uint32 searchID, CUInt128 contactID, uint8 type, uint32 ip, uint16 tcp, uint16 udp, uint32 serverip, uint16 serverport)
void CALLBACK KademliaResultFileCallback(CUInt128 fileID, CUInt128 contactID, uint8 type, uint32 ip, uint16 tcp, uint16 udp, uint32 serverip, uint16 serverport, uint32 tth1, uint32 tth2)
{
// TODO: only type == 1 is handled now
if(type != 1){
ASSERT(0);
return;
}
try
{
uint8 buf[24];
memcpy(buf, fileID.getData(), 16);
memcpy(buf+16, &tth1, 4);
memcpy(buf+20, &tth2, 4);
if(FindFileResultHandler != NULL){
(*FindFileResultHandler)(buf, ip, tcp, udp);
}
// if ( !theApp.emuledlg->IsRunning() )
// {
// return;
// }
}
catch(...){ return; }
/* KADFILERESULT kadfr;
kadfr.searchID = searchID;
kadfr.pcontactID = &contactID;
kadfr.type = type;
kadfr.ip = ip;
kadfr.tcp = tcp;
kadfr.udp = udp;
kadfr.serverip = serverip;
kadfr.serverport = serverport;
*/
// theApp.emuledlg->SendMessage(WM_KAD_RESULTFILE, 0, (LPARAM)&kadfr);
}
void CALLBACK KademliaResultKeywordCallback(uint32 searchID, CUInt128 fileID, LPCSTR name, uint32 size, LPCSTR type, uint32 ip, uint16 tcp, uint32 tth_1, uint32 tth_2)
{
try
{
byte buf[FILE_HASH_LEN];
memcpy(buf, fileID.getData(), 16);
memcpy(buf + 16, &tth_1, 4);
memcpy(buf + 20, &tth_2, 4);
if(FindKeywordResultHandler != NULL){
(*FindKeywordResultHandler)(ip, tcp, buf , name, size);
}
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
/*
va_list args;
va_start(args, numProperties);
KADKEYWORDRESULT kadkwr;
kadkwr.searchID = searchID;
kadkwr.pfileID = &fileID;
kadkwr.name = name;
kadkwr.size = size;
kadkwr.type = type;
kadkwr.numProperties = numProperties;
kadkwr.args = args;
va_end(args);
*/
// theApp.emuledlg->SendMessage(WM_KAD_RESULTKEYWORD, 0, (LPARAM)&kadkwr);
}
static bool processingTcpRequest = false;
static void __cdecl ThreadProcessTcpRequest(void* params)
{
processingTcpRequest = true;
SOCKET s = INVALID_SOCKET;
timeval timeout = { 15, 0 };
unsigned long argp = 1;
CContact* contact = (CContact*)params;
try{
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == INVALID_SOCKET) goto cleanup;
ioctlsocket(s, FIONBIO, &argp);
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(contact->getIPAddress());
addr.sin_port = htons(contact->getTCPPort());
connect(s, (struct sockaddr*)&addr, sizeof(addr));
fd_set writefds;
fd_set exceptfds;
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(s, &writefds);
FD_SET(s, &exceptfds);
select(0, NULL, &writefds, &exceptfds, &timeout);
}catch(...){
TRACE("Exception in Kademlia ThreadProcessTcpRequest\n");
_ASSERT(0);
}
cleanup:
if(s != INVALID_SOCKET){
closesocket(s);
}
processingTcpRequest = false;
delete params;
_endthread();
}
void CALLBACK KademliaRequestTCPCallback(CContact* contact)
{
try
{
if(!processingTcpRequest && contact->getTCPPort()){
_beginthread(ThreadProcessTcpRequest, 0, contact);
}else{
delete contact;
}
}
catch(...){ return; }
}
void CALLBACK KademliaUpdateStatusCallback(Status* status)
{
if(status->m_totalStoreSrc < KADEMLIATOTALSTORESRC && status->m_connected)
PublishNextFile();
if(status->m_totalStoreKey < KADEMLIATOTALSTOREKEY && status->m_connected)
PublishNextKeyword();
pKademlia->setStatus(status);
/*
try
{
if ( !theApp.emuledlg->IsRunning() )
{
delete status;
return;
}
}
catch(...){ return; }
theApp.emuledlg->SendMessage(WM_KAD_UPDATESTATUS, 0, (LPARAM)status);
*/}
void CALLBACK KademliaOverheadSendCallback(uint32 size)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_OVERHEADSEND, 0, (LPARAM)size);
}
void CALLBACK KademliaOverheadRecvCallback(uint32 size)
{
try
{
// if ( !theApp.emuledlg->IsRunning() )
{
return;
}
}
catch(...){ return; }
// theApp.emuledlg->SendMessage(WM_KAD_OVERHEADRECV, 0, (LPARAM)size);
}
void CALLBACK myErrHandler(CKademliaError *error)
{
#ifdef DEBUG
CString msg;
msg.Format("\r\n\r\nError 0x%08X : %s\r\n\r\n", error->m_ErrorCode, error->m_ErrorDescription);
// cerr << msg.GetBuffer(0);
OutputDebugString(msg);
#endif
// if(theApp && theApp.emuledlg->IsRunning())
// CemuleApp::AddLogLine(false, msg);
}
void CALLBACK myDebugAndLogHandler(LPCSTR lpMsg)
{
// cout << lpMsg << endl;
// if(theApp && theApp.emuledlg->IsRunning())
// CemuleApp::AddDebugLogLine(false, "%s", (CString)lpMsg);
#ifdef DEBUG
OutputDebugString(lpMsg);
OutputDebugString("\r\n");
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -