redis pubsub.c

channel是一个精确的名字,pattern则是glob匹配(通配符匹配)。

这个文件主要包含了以下接口:

int listMatchPubsubPattern(void *a, void *b) ;  // a和b指向pubsubPattern结构:

/*

  typedef struct pubsubPattern {

    redisClient* client;

    robj* pattern;

  };

*/


// 订阅频道,往client->pubsub_channels这个set中添加,往server.pubsub_channels对应client列表中添加client

int pubsubSubscribeChannel(redisClient *c, robj *channel);

int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify);


//  订阅pattern,往client->pubusb_patterns这个list中添加,往server.pubusb_patterns中添加pubsubPattern结构
int pubsubSubscribePattern(redisClient *c, robj *pattern);

int pubsubUnsubscribePattern(redisClient *c, robj *pattern, int notify);


int pubsubUnsubscribeAllChannels(redisClient *c, int notify);
int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);


// 发布消息时先对匹配channel的clients发送,然后对匹配pattern的clients发送
int pubsubPublishMessage(robj *channel, robj *message);

void freePubsubPattern(void *p);  // 需要decrRefCount(p->pattern)


// 相应的命令在这里,subscribe后面跟几个参数,逐一订阅

void subscribeCommand(redisClient *c);


// 不跟参数,unsubscribe all,否则后面跟几个参数,逐一取消订阅
void unsubscribeCommand(redisClient *c);


// psubscribe后面跟几个参数,逐一订阅
void psubscribeCommand(redisClient *c);


// 不跟参数,unsubscribe all,否则后面跟几个参数,逐一取消订阅

void punsubscribeCommand(redisClient *c);


// 第一个参数是channel,第二个参数是message
void publishCommand(redisClient *c);


从上面看来,channels和pattern都需要在client和server中保存,不过channels用set保存,patterns用list保存。

你可能感兴趣的:(redis,源码,pubsub)