begin db, pthread mutex added
This commit is contained in:
parent
aeec0156a2
commit
e45e41c0c5
2 changed files with 73 additions and 0 deletions
44
db.h
Normal file
44
db.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <pair>
|
||||||
|
#include <set>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
#include "netbase.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
class CAddrInfo {
|
||||||
|
private:
|
||||||
|
CIP ip;
|
||||||
|
uint64_t services;
|
||||||
|
uint32_t lastTry;
|
||||||
|
uint32_t ourLastTry;
|
||||||
|
double reliabity;
|
||||||
|
double weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CAddrDb {
|
||||||
|
private:
|
||||||
|
CCriticalSection cs;
|
||||||
|
int nId; // number of address id's
|
||||||
|
map<int, CAddrInfo> idToInfo; // map address id to address info
|
||||||
|
map<CIP, int> ipToId; // map ip to id
|
||||||
|
deque<int> ourId; // sequence of tracked nodes, in order we have tried connecting to them
|
||||||
|
set<int> unkId; // set of nodes not yet tried
|
||||||
|
set<int> goodId; // set of good nodes
|
||||||
|
map<CIP, pair<uint32_t, uint32_t> > banned; // nodes that are banned, with their from/to ban time
|
||||||
|
|
||||||
|
public:
|
||||||
|
void Add(const CAddress &addr);
|
||||||
|
void Add(const vector<CAddress> &vAddr);
|
||||||
|
void Good(const CIP &addr);
|
||||||
|
void Bad(const CIP &addr, int fail);
|
||||||
|
CIP Get();
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
int GetIPv4Address(struct in_addr *addr, int max);
|
||||||
|
}
|
29
util.h
29
util.h
|
@ -1,6 +1,8 @@
|
||||||
#ifndef _UTIL_H_
|
#ifndef _UTIL_H_
|
||||||
#define _UTIL_H_ 1
|
#define _UTIL_H_ 1
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#define loop for (;;)
|
#define loop for (;;)
|
||||||
#define BEGIN(a) ((char*)&(a))
|
#define BEGIN(a) ((char*)&(a))
|
||||||
#define END(a) ((char*)&((&(a))[1]))
|
#define END(a) ((char*)&((&(a))[1]))
|
||||||
|
@ -34,4 +36,31 @@ inline int myclosesocket(SOCKET& hSocket)
|
||||||
}
|
}
|
||||||
#define closesocket(s) myclosesocket(s)
|
#define closesocket(s) myclosesocket(s)
|
||||||
|
|
||||||
|
|
||||||
|
// Wrapper to automatically initialize mutex
|
||||||
|
class CCriticalSection
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
public:
|
||||||
|
explicit CCriticalSection() { pthread_mutex_init(&mutex, NULL); }
|
||||||
|
~CCriticalSection() { pthread_mutex_destroy(&mutex); }
|
||||||
|
void Enter() { pthread_mutex_lock(&mutex); }
|
||||||
|
void Leave() { pthread_mutex_unlock(&mutex); }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Automatically leave critical section when leaving block, needed for exception safety
|
||||||
|
class CCriticalBlock
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
CCriticalSection* pcs;
|
||||||
|
public:
|
||||||
|
CCriticalBlock(CCriticalSection& cs) : pcs(&cs) { pcs->Enter(); }
|
||||||
|
operator bool() const { return true; }
|
||||||
|
~CCriticalBlock() { pcs->Leave(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
#define CRITICAL_BLOCK(cs) \
|
||||||
|
if (CCriticalBlock criticalblock = CCriticalBlock(cs))
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue