This commit is contained in:
2025-09-24 10:53:28 +08:00
commit f8e4df77fb
856 changed files with 140098 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
#ifndef __UT_ROBOT_LEASE_SERVER_HPP__
#define __UT_ROBOT_LEASE_SERVER_HPP__
#include <unitree/robot/server/server_base.hpp>
namespace unitree
{
namespace robot
{
class LeaseCache
{
public:
LeaseCache();
~LeaseCache();
void Set(int64_t id, const std::string& mName, int64_t lastModified = 0);
void Renewal(int64_t lastModified = 0);
void Clear();
int64_t GetLastModified() const;
int64_t GetId() const;
const std::string& GetName() const;
private:
int64_t mLastModified;
int64_t mId;
std::string mName;
};
class LeaseServer : public ServerBase
{
public:
explicit LeaseServer(const std::string& name, int64_t term);
~LeaseServer();
void Init();
bool CheckRequestLeaseDenied(int64_t leaseId);
private:
void ServerRequestHandler(const RequestPtr& request);
int32_t Apply(const std::string& parameter, std::string& data);
int32_t Renewal(int64_t leaseId);
int64_t GenerateId(const std::string& name);
private:
int64_t mTerm;
LeaseCache mCache;
common::Mutex mMutex;
ServerStubPtr mServerStubPtr;
};
using LeaseServerPtr = std::shared_ptr<LeaseServer>;
}
}
#endif//__UT_ROBOT_LEASE_SERVER_HPP__

View File

@@ -0,0 +1,77 @@
#ifndef __UT_ROBOT_SDK_SERVER_HPP__
#define __UT_ROBOT_SDK_SERVER_HPP__
#include <unitree/robot/server/server_base.hpp>
#include <unitree/robot/server/lease_server.hpp>
#define UT_ROBOT_SERVER_REG_API_HANDLER_NO_LEASE(apiId, handler) \
UT_ROBOT_SERVER_REG_API_HANDLER(apiId, handler, false)
#define UT_ROBOT_SERVER_REG_API_BINARY_HANDLER_NO_LEASE(apiId, handler) \
UT_ROBOT_SERVER_REG_API_BINARY_HANDLER(apiId, handler, false)
#define UT_ROBOT_SERVER_REG_API_HANDLER(apiId, handler, checkLease) \
RegistHandler(apiId, std::bind(handler, this, std::placeholders::_1, std::placeholders::_2), checkLease)
#define UT_ROBOT_SERVER_REG_API_BINARY_HANDLER(apiId, handler, checkLease) \
RegistBinaryHandler(apiId, std::bind(handler, this, std::placeholders::_1, std::placeholders::_2), checkLease)
namespace unitree
{
namespace robot
{
using RequestHandler = std::function<int32_t(const std::string& parameter, std::string& data)>;
using BinaryRequestHandler = std::function<int32_t(const std::vector<uint8_t>& parameter, std::vector<uint8_t>& data)>;
class Server : public ServerBase
{
public:
explicit Server(const std::string& name);
virtual ~Server();
virtual void Init() = 0;
void StartLease(int64_t leaseTerm);
void StartLease(float leaseTerm);
const std::string& GetName();
const std::string& GetApiVersion() const;
int32_t GetCurrentApiId() const;
protected:
void SetApiVersion(const std::string& version);
void ServerRequestHandler(const RequestPtr& request);
void RegistHandler(int32_t apiId, const RequestHandler& handler, bool checkLease = false);
void RegistBinaryHandler(int32_t apiId, const BinaryRequestHandler& binaryHandler, bool checkLease = false);
bool IsBinary(int32_t apiId);
RequestHandler GetHandler(int32_t apiId, bool& ignoreLease) const;
BinaryRequestHandler GetBinaryHandler(int32_t apiId, bool& ignoreLease) const;
bool CheckLeaseDenied(int64_t leaseId);
private:
bool mEnableLease;
std::string mName;
std::string mApiVersion;
std::unordered_map<int32_t,std::pair<RequestHandler,bool>> mApiHandlerMap;
std::unordered_map<int32_t,std::pair<BinaryRequestHandler,bool>> mApiBinaryHandlerMap;
std::set<int32_t> mApiBinarySet;
LeaseServerPtr mLeaseServerPtr;
private:
static thread_local int32_t mCurrentApiId;
};
using ServerPtr = std::shared_ptr<Server>;
}
}
#endif//__UT_ROBOT_SDK_SERVER_HPP__

View File

@@ -0,0 +1,35 @@
#ifndef __UT_ROBOT_SDK_SERVER_BASE_HPP__
#define __UT_ROBOT_SDK_SERVER_BASE_HPP__
#include <unitree/robot/server/server_stub.hpp>
namespace unitree
{
namespace robot
{
class ServerBase
{
public:
explicit ServerBase(const std::string& name);
virtual ~ServerBase();
virtual void Init() = 0;
virtual void Start(bool enableProiQueue = false);
const std::string& GetName() const;
protected:
virtual void ServerRequestHandler(const RequestPtr& request) = 0;
void SendResponse(const Response& response);
protected:
std::string mName;
ServerStubPtr mServerStubPtr;
};
using ServerBasePtr = std::shared_ptr<ServerBase>;
}
}
#endif//__UT_ROBOT_SDK_SERVER_BASE_HPP__

View File

@@ -0,0 +1,46 @@
#ifndef __UT_ROBOT_SDK_SERVER_STUB_HPP__
#define __UT_ROBOT_SDK_SERVER_STUB_HPP__
#include <unitree/robot/internal/internal.hpp>
#include <unitree/robot/channel/channel_labor.hpp>
#include <unitree/common/thread/thread.hpp>
#include <unitree/common/block_queue.hpp>
namespace unitree
{
namespace robot
{
using ServerRequestHandler = std::function<void(const RequestPtr& requestPtr)>;
class ServerStub
{
public:
explicit ServerStub();
~ServerStub();
void Init(const std::string& name, const ServerRequestHandler& handler, bool enableProiQueue);
bool Send(const Response& response, int64_t timeout = 0);
private:
void Enqueue(const void* message);
int32_t QueueThreadFunction();
int32_t ProiQueueThreadFunction();
private:
bool mEnableProiQueue;
bool mRunning;
ServerRequestHandler mRequestHandler;
ChannelLaborPtr<Response,Request> mChannelLaborPtr;
common::BlockQueuePtr<RequestPtr> mQueuePtr;
common::BlockQueuePtr<RequestPtr> mProiQueuePtr;
common::ThreadPtr mQueueThreadPtr;
common::ThreadPtr mProiQueueThreadPtr;
};
using ServerStubPtr = std::shared_ptr<ServerStub>;
}
}
#endif//__UT_ROBOT_SDK_SERVER_STUB_HPP__