2019-11-05 08:15:19 +08:00
|
|
|
#pragma once
|
2023-01-01 06:19:29 +08:00
|
|
|
|
2019-11-05 08:15:19 +08:00
|
|
|
#include <string>
|
2023-01-01 08:33:09 +08:00
|
|
|
#include <vector>
|
2019-11-05 08:15:19 +08:00
|
|
|
|
2024-06-07 04:58:15 +08:00
|
|
|
#include "msgq/messaging/messaging.h"
|
|
|
|
#include "msgq/messaging/msgq.h"
|
2023-01-01 06:19:29 +08:00
|
|
|
|
2019-11-05 08:15:19 +08:00
|
|
|
#define MAX_POLLERS 128
|
|
|
|
|
|
|
|
class MSGQContext : public Context {
|
|
|
|
private:
|
|
|
|
void * context = NULL;
|
|
|
|
public:
|
|
|
|
MSGQContext();
|
|
|
|
void * getRawContext() {return context;}
|
|
|
|
~MSGQContext();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MSGQMessage : public Message {
|
|
|
|
private:
|
|
|
|
char * data;
|
|
|
|
size_t size;
|
|
|
|
public:
|
|
|
|
void init(size_t size);
|
|
|
|
void init(char *data, size_t size);
|
|
|
|
void takeOwnership(char *data, size_t size);
|
|
|
|
size_t getSize(){return size;}
|
|
|
|
char * getData(){return data;}
|
|
|
|
void close();
|
|
|
|
~MSGQMessage();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MSGQSubSocket : public SubSocket {
|
|
|
|
private:
|
2019-11-16 07:42:55 +08:00
|
|
|
msgq_queue_t * q = NULL;
|
2019-11-05 08:15:19 +08:00
|
|
|
int timeout;
|
|
|
|
public:
|
2021-01-08 21:54:41 +08:00
|
|
|
int connect(Context *context, std::string endpoint, std::string address, bool conflate=false, bool check_endpoint=true);
|
2019-11-05 08:15:19 +08:00
|
|
|
void setTimeout(int timeout);
|
|
|
|
void * getRawSocket() {return (void*)q;}
|
|
|
|
Message *receive(bool non_blocking=false);
|
|
|
|
~MSGQSubSocket();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MSGQPubSocket : public PubSocket {
|
|
|
|
private:
|
2019-11-16 07:42:55 +08:00
|
|
|
msgq_queue_t * q = NULL;
|
2019-11-05 08:15:19 +08:00
|
|
|
public:
|
2021-01-08 21:54:41 +08:00
|
|
|
int connect(Context *context, std::string endpoint, bool check_endpoint=true);
|
2019-11-05 08:15:19 +08:00
|
|
|
int sendMessage(Message *message);
|
|
|
|
int send(char *data, size_t size);
|
2021-04-19 23:28:06 +08:00
|
|
|
bool all_readers_updated();
|
2019-11-05 08:15:19 +08:00
|
|
|
~MSGQPubSocket();
|
|
|
|
};
|
|
|
|
|
|
|
|
class MSGQPoller : public Poller {
|
|
|
|
private:
|
|
|
|
std::vector<SubSocket*> sockets;
|
|
|
|
msgq_pollitem_t polls[MAX_POLLERS];
|
|
|
|
size_t num_polls = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void registerSocket(SubSocket *socket);
|
|
|
|
std::vector<SubSocket*> poll(int timeout);
|
2023-08-25 02:31:53 +08:00
|
|
|
~MSGQPoller(){}
|
2019-11-05 08:15:19 +08:00
|
|
|
};
|