mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
multicast support
This commit is contained in:
60
service-src/service_multicast.c
Normal file
60
service-src/service_multicast.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "skynet.h"
|
||||
#include "skynet_multicast.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct skynet_multicast_group *
|
||||
multicast_create() {
|
||||
return skynet_multicast_newgroup();
|
||||
}
|
||||
|
||||
void
|
||||
multicast_release(struct skynet_multicast_group *g) {
|
||||
skynet_multicast_deletegroup(g);
|
||||
}
|
||||
|
||||
static int
|
||||
_maincb(struct skynet_context * context, void * ud, int session, const char * addr, const void * msg, size_t sz) {
|
||||
struct skynet_multicast_group *g = ud;
|
||||
uint32_t source = strtoul(addr+1, NULL, 16);
|
||||
if (source == 0) {
|
||||
char cmd = '\0';
|
||||
uint32_t handle = 0;
|
||||
sscanf(msg,"%c %x",&cmd,&handle);
|
||||
if (handle == 0) {
|
||||
skynet_error(context, "Invalid handle %s",msg);
|
||||
return 0;
|
||||
}
|
||||
switch (cmd) {
|
||||
case 'E':
|
||||
skynet_multicast_entergroup(g, handle);
|
||||
break;
|
||||
case 'L':
|
||||
skynet_multicast_leavegroup(g, handle);
|
||||
break;
|
||||
case 'C':
|
||||
skynet_command(context, "EXIT", NULL);
|
||||
break;
|
||||
default:
|
||||
skynet_error(context, "Invalid command %s",msg);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
struct skynet_multicast_message * mc = skynet_multicast_create(msg, sz, source);
|
||||
skynet_multicast_castgroup(context, g, mc);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
multicast_init(struct skynet_multicast_group *g, struct skynet_context *ctx, const char * args) {
|
||||
skynet_callback(ctx, g, _maincb);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user