4.7 消息发布功能实现 (⼀)- 客户端与服务器发布消息传输
- 与消息订阅⼀样,在客户端需要设计⼀个接⼝⽤于发布消息, 具体设计如下:
- step 1 : 在 client.h 中添加消息发布接⼝
- extern void publish(char *topic,const char *content);
- step 2 : 在 client.c 中添加实现接⼝
- void publish(char *topic,const char *content)
- {
- #ifdef DEBUG
- printf("[DEBUG] publish.\n");
- #endif
- }
- 在传输发布消息时, 也是通过环形队列发送给服务器, 具体设计如下:
void publish(char *topic,const char *content)
{
packet_t packet;
strcpy(packet.topic,topic);
strcpy(packet.content,content);
packet.pid = getpid();
packet.mode = PUBLISH;
shmfifo_put(g_shmfifo,&packet);
}
- 服务器接收到消息后, 会进⾏分发处理,最终会通过发布消息函数来进⾏处理,具体接收设计如下:
- step 1 : 在 server.h 中添加发布消息的处理函数
- void do_publish(char *topic,pid_t pid,char *content)
- step 2 : 在 server.c 中添加发布消息接⼝
- void do_publish(char *topic,pid_t pid,char *content)
- {
- #ifdef DEBUG
- printf("[DEBUG] do publish.\n");
- #endif
- }
4.8 消息发布功能实现(