mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 22:25:04 -04:00
Added rsshared ptr helper object (only use in test for memory management)
Added first nxs_test modified pserviceserver to allow better dependency injection git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7279 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
a929f80596
commit
ef8f48ae73
17 changed files with 340 additions and 86 deletions
|
@ -117,6 +117,8 @@ public:
|
|||
|
||||
public:
|
||||
|
||||
typedef std::map<RsNxsGrp*, RsGxsGrpMetaData*> GrpStoreMap;
|
||||
|
||||
RsGeneralDataService(){}
|
||||
virtual ~RsGeneralDataService(){return;}
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
#include "rsgxsnetutils.h"
|
||||
#include "pqi/p3cfgmgr.h"
|
||||
#include "rsgixs.h"
|
||||
#include "util/rssharedptr.h"
|
||||
|
||||
/// keep track of transaction number
|
||||
typedef std::map<uint32_t, NxsTransaction*> TransactionIdMap;
|
||||
|
@ -64,6 +65,8 @@ class RsGxsNetService : public RsNetworkExchangeService, public p3ThreadedServic
|
|||
{
|
||||
public:
|
||||
|
||||
typedef RsSharedPtr<RsGxsNetService> pointer;
|
||||
|
||||
static const uint32_t FRAGMENT_SIZE;
|
||||
/*!
|
||||
* only one observer is allowed
|
||||
|
|
|
@ -33,7 +33,7 @@ const int pqiservicezone = 60478;
|
|||
* #define SERVICE_DEBUG 1
|
||||
****/
|
||||
|
||||
void pqiService::setServiceServer(p3ServiceServer *server)
|
||||
void pqiService::setServiceServer(p3ServiceServerIface *server)
|
||||
{
|
||||
mServiceServer = server;
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
// DataType is defined in the serialiser directory.
|
||||
|
||||
class RsRawItem;
|
||||
class p3ServiceServer;
|
||||
class p3ServiceServerIface;
|
||||
|
||||
|
||||
class pqiService
|
||||
|
@ -68,7 +68,7 @@ class pqiService
|
|||
virtual ~pqiService() { return; }
|
||||
|
||||
public:
|
||||
void setServiceServer(p3ServiceServer *server);
|
||||
void setServiceServer(p3ServiceServerIface *server);
|
||||
//
|
||||
virtual bool recv(RsRawItem *) = 0;
|
||||
virtual bool send(RsRawItem *item);
|
||||
|
@ -78,7 +78,7 @@ virtual RsServiceInfo getServiceInfo() = 0;
|
|||
virtual int tick() { return 0; }
|
||||
|
||||
private:
|
||||
p3ServiceServer *mServiceServer; // const, no need for mutex.
|
||||
p3ServiceServerIface *mServiceServer; // const, no need for mutex.
|
||||
};
|
||||
|
||||
#include <map>
|
||||
|
@ -89,7 +89,21 @@ virtual int tick() { return 0; }
|
|||
* to the base level pqiPublisher instead.
|
||||
*/
|
||||
|
||||
class p3ServiceServer
|
||||
// use interface to allow DI
|
||||
class p3ServiceServerIface
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~p3ServiceServerIface() {}
|
||||
|
||||
|
||||
virtual bool recvItem(RsRawItem *) = 0;
|
||||
virtual bool sendItem(RsRawItem *) = 0;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class p3ServiceServer : public p3ServiceServerIface
|
||||
{
|
||||
public:
|
||||
p3ServiceServer(pqiPublisher *pub, p3ServiceControl *ctrl);
|
||||
|
@ -100,6 +114,7 @@ bool recvItem(RsRawItem *);
|
|||
bool sendItem(RsRawItem *);
|
||||
|
||||
int tick();
|
||||
public:
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -35,10 +35,16 @@
|
|||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <util/rsrandom.h>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
template<uint32_t ID_SIZE_IN_BYTES,bool UPPER_CASE,uint32_t UNIQUE_IDENTIFIER> class t_RsGenericIdType
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::list<t_RsGenericIdType<ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER> > std_list;
|
||||
typedef std::vector<t_RsGenericIdType<ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER> > std_vector;
|
||||
|
||||
t_RsGenericIdType()
|
||||
{
|
||||
memset(bytes,0,ID_SIZE_IN_BYTES) ; // by default, ids are set to null()
|
||||
|
|
|
@ -1243,6 +1243,19 @@ std::ostream& RsNxsSyncMsgItem::print(std::ostream &out, uint16_t indent)
|
|||
return out;
|
||||
}
|
||||
|
||||
RsNxsGrp* RsNxsGrp::clone() const {
|
||||
RsNxsGrp* grp = new RsNxsGrp(PacketService());
|
||||
*grp = *this;
|
||||
|
||||
if(this->metaData)
|
||||
{
|
||||
grp->metaData = new RsGxsGrpMetaData();
|
||||
*(grp->metaData) = *(this->metaData);
|
||||
}
|
||||
|
||||
return grp;
|
||||
}
|
||||
|
||||
std::ostream& RsNxsGrp::print(std::ostream &out, uint16_t indent){
|
||||
|
||||
printRsItemBase(out, "RsNxsGrp", indent);
|
||||
|
|
|
@ -200,6 +200,8 @@ public:
|
|||
//std::cout << "\nGrp refcount-- : " << --refcount << std::endl;
|
||||
}
|
||||
|
||||
RsNxsGrp* clone() const;
|
||||
|
||||
virtual void clear();
|
||||
virtual std::ostream &print(std::ostream &out, uint16_t indent);
|
||||
|
||||
|
|
|
@ -156,7 +156,7 @@ uint8_t RsItem::PacketType()
|
|||
}
|
||||
|
||||
|
||||
uint8_t RsItem::PacketSubType()
|
||||
uint8_t RsItem::PacketSubType() const
|
||||
{
|
||||
return (type & 0xFF);
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ RsItem::RsItem(uint8_t ver, uint16_t service, uint8_t subtype)
|
|||
return;
|
||||
}
|
||||
|
||||
uint16_t RsItem::PacketService()
|
||||
uint16_t RsItem::PacketService() const
|
||||
{
|
||||
return (type >> 8) & 0xFFFF;
|
||||
}
|
||||
|
|
|
@ -96,11 +96,11 @@ class RsItem: public RsMemoryManagement::SmallObject
|
|||
uint8_t PacketVersion();
|
||||
uint8_t PacketClass();
|
||||
uint8_t PacketType();
|
||||
uint8_t PacketSubType();
|
||||
uint8_t PacketSubType() const;
|
||||
|
||||
/* For Service Packets */
|
||||
RsItem(uint8_t ver, uint16_t service, uint8_t subtype);
|
||||
uint16_t PacketService(); /* combined Packet class/type (mid 16bits) */
|
||||
uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */
|
||||
|
||||
inline uint8_t priority_level() const { return _priority_level ;}
|
||||
inline void setPriorityLevel(uint8_t l) { _priority_level = l ;}
|
||||
|
|
85
libretroshare/src/util/rssharedptr.h
Normal file
85
libretroshare/src/util/rssharedptr.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* rssharedptr.h
|
||||
*
|
||||
* Created on: 16 Apr 2014
|
||||
* Author: crispy
|
||||
*/
|
||||
|
||||
#ifndef RSSHAREDPTR_H_
|
||||
#define RSSHAREDPTR_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/*!
|
||||
* Not thread safe!!
|
||||
*/
|
||||
template<class T>
|
||||
class RsSharedPtr
|
||||
{
|
||||
public:
|
||||
|
||||
RsSharedPtr() : mShared(NULL), mCount(NULL) {}
|
||||
|
||||
RsSharedPtr(T* shared)
|
||||
: mShared(shared), mCount(new int(0))
|
||||
{
|
||||
mCount++;
|
||||
}
|
||||
|
||||
RsSharedPtr(const RsSharedPtr<T>& rsp)
|
||||
{
|
||||
mShared = rsp.mShared;
|
||||
mCount = rsp.mCount;
|
||||
mCount++;
|
||||
}
|
||||
|
||||
void operator=(const RsSharedPtr<T>& rsp)
|
||||
{
|
||||
DecrementAndDeleteIfLast();
|
||||
mShared = rsp.mShared;
|
||||
RepointAndIncrement(rsp.mCount);
|
||||
}
|
||||
|
||||
T* release() { mCount--; T* temp = mShared; mShared = NULL; return temp; }
|
||||
T* get() { return mShared; }
|
||||
|
||||
T& operator*(){ return *mShared; }
|
||||
T* operator->(){ return mShared; }
|
||||
|
||||
~RsSharedPtr()
|
||||
{
|
||||
DecrementAndDeleteIfLast();
|
||||
}
|
||||
private:
|
||||
|
||||
void DecrementAndDeleteIfLast()
|
||||
{
|
||||
mCount--;
|
||||
if(mCount == 0 && mShared != NULL)
|
||||
{
|
||||
delete mShared;
|
||||
delete mCount;
|
||||
}
|
||||
|
||||
mShared = NULL;
|
||||
mCount = NULL;
|
||||
}
|
||||
|
||||
void RepointAndIncrement(int* count)
|
||||
{
|
||||
mCount = count;
|
||||
mCount++;
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int* mCount;
|
||||
T* mShared;
|
||||
|
||||
};
|
||||
|
||||
template<class T>
|
||||
RsSharedPtr<T> rs_make_shared(T* ptr){ return RsSharedPtr<T>(ptr); }
|
||||
|
||||
#endif /* RSSHAREDPTR_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue