本文整理汇总了C++中HASH_ID函数的典型用法代码示例。如果您正苦于以下问题:C++ HASH_ID函数的具体用法?C++ HASH_ID怎么用?C++ HASH_ID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HASH_ID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: start_socket
// 启动socket,加入事件循环
// 成功时返回SOCKET_OPEN
static int
start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) {
int id = request->id;
result->id = id;
result->opaque = request->opaque;
result->ud = 0;
result->data = NULL;
struct socket *s = &ss->slot[HASH_ID(id)];
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
result->data = "invalid socket";
return SOCKET_ERROR;
}
if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) {
// 套接字加入事件循环
if (sp_add(ss->event_fd, s->fd, s)) {
force_close(ss, s, result);
result->data = strerror(errno);
return SOCKET_ERROR;
}
// SOCKET_TYPE_PACCEPT -> SOCKET_TYPE_CONNECTED
// SOCKET_TYPE_PLISTEN -> SOCKET_TYPE_LISTEN
s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN;
s->opaque = request->opaque;
result->data = "start";
return SOCKET_OPEN;
} else if (s->type == SOCKET_TYPE_CONNECTED) {
// todo: maybe we should send a message SOCKET_TRANSFER to s->opaque
s->opaque = request->opaque;
result->data = "transfer";
return SOCKET_OPEN;
}
// if s->type == SOCKET_TYPE_HALFCLOSE , SOCKET_CLOSE message will send later
return -1;
}
示例2: socket_server_udp_send
int64_t
socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp_address *addr, const void *buffer, int sz) {
struct socket * s = &ss->slot[HASH_ID(id)];
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
free_buffer(ss, buffer, sz);
return -1;
}
struct request_package request;
request.u.send_udp.send.id = id;
request.u.send_udp.send.sz = sz;
request.u.send_udp.send.buffer = (char *)buffer;
const uint8_t *udp_address = (const uint8_t *)addr;
int addrsz;
switch (udp_address[0]) {
case PROTOCOL_UDP:
addrsz = 1+2+4; // 1 type, 2 port, 4 ipv4
break;
case PROTOCOL_UDPv6:
addrsz = 1+2+16; // 1 type, 2 port, 16 ipv6
break;
default:
free_buffer(ss, buffer, sz);
return -1;
}
memcpy(request.u.send_udp.address, udp_address, addrsz);
send_request(ss, &request, 'A', sizeof(request.u.send_udp.send)+addrsz);
return s->wb_size;
}
示例3: new_fd
// 创建新的socket实例
// id,通过reserve_id分配的alloc_id
// fd, 通过socket创建的套接字
// add, true则加入事件循环
static struct socket *
new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque, bool add) {
struct socket * s = &ss->slot[HASH_ID(id)];
// 正常情况下,socket已在reserve_id中赋值为保留类型
assert(s->type == SOCKET_TYPE_RESERVE);
if (add) {
// 加入事件循环
if (sp_add(ss->event_fd, fd, s)) {
// 如果加入失败,socket重置为初始类型
s->type = SOCKET_TYPE_INVALID;
return NULL;
}
}
// 初始化变量
s->id = id;
s->fd = fd;
s->protocol = protocol;
s->p.size = MIN_READ_BUFFER;
s->opaque = opaque;
s->wb_size = 0;
check_wb_list(&s->high);
check_wb_list(&s->low);
return s;
}
示例4: socket_req_start
static int socket_req_start(struct _start_req *req, struct socket_message *msg) {
struct socket *sock;
msg->id = req->id;
msg->ud = req->ud;
sock = &S.slot[HASH_ID(req->id)];
if (sock->type == SOCKET_TYPE_INVALID || sock->id != req->id) {
msg->data = "socket invalid id";
return SOCKET_ERR;
}
if (sock->type == SOCKET_TYPE_PACCEPT || sock->type == SOCKET_TYPE_PLISTEN) {
if (event_add(S.event_fd, sock->fd, sock)) {
sock->type = SOCKET_TYPE_INVALID;
msg->data = strerror(errno);
return SOCKET_ERR;
}
sock->type = (sock->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_OPENED : SOCKET_TYPE_LISTEN;
sock->ud = req->ud;
msg->data = "start";
return SOCKET_OPEN;
} else if (sock->type == SOCKET_TYPE_OPENED) {
sock->ud = req->ud;
msg->data = "transfer";
return SOCKET_OPEN;
}
return -1;
}
示例5: reserve_id
// 递增分配id进行hash,并返回匹配的socket
static int
reserve_id(struct socket_server *ss) {
int i;
for (i=0;i<MAX_SOCKET;i++) {
// 递增
int id = ATOM_INC(&(ss->alloc_id));
if (id < 0) {
// 回绕
id = ATOM_AND(&(ss->alloc_id), 0x7fffffff);
}
// 哈希匹配
struct socket *s = &ss->slot[HASH_ID(id)];
if (s->type == SOCKET_TYPE_INVALID) {
// 如果是初始类型,修改为保留类型
if (ATOM_CAS(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) {
s->id = id;
s->fd = -1;
return id;
} else {
// 否则,重试
// retry
--i;
}
}
}
return -1;
}
示例6: socket_udpsend
long socket_udpsend(int id, const struct socket_udp_address *address, const void *data, int size) {
const uint8_t *udp_address;
int addrsize;
struct socket_req req;
struct socket * sock = &S.slot[HASH_ID(id)];
if (sock->id != id || sock->type == SOCKET_TYPE_INVALID) {
freebuffer((void *)data, size);
return -1;
}
memset(&req, 0, sizeof req);
req.req = SOCKET_REQ_SENDUDP;
req.u.send.id = id;
req.u.send.data = (char *)data;
req.u.send.size = size;
udp_address = (const uint8_t *)address;
switch (udp_address[0]) {
case PROTOCOL_UDP:
addrsize = 1 + 2 + 4;
break;
case PROTOCOL_UDPv6:
addrsize = 1 + 2 + 16;
break;
default:
freebuffer((void *)data, size);
return -1;
}
memcpy(req.u.sendudp.address, udp_address, addrsize);
socket_send_req(&req);
return sock->wb_size;
}
示例7: close_socket
static int
close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) {
int id = request->id;
struct socket * s = &ss->slot[HASH_ID(id)];
if (s->type == SOCKET_TYPE_INVALID || s->id != id) {
result->id = id;
result->opaque = request->opaque;
result->ud = 0;
result->data = NULL;
return SOCKET_CLOSE;
}
if (!send_buffer_empty(s)) {
int type = send_buffer(ss,s,result);
if (type != -1)
return type;
}
if (send_buffer_empty(s)) {
force_close(ss,s,result);
result->id = id;
result->opaque = request->opaque;
return SOCKET_CLOSE;
}
s->type = SOCKET_TYPE_HALFCLOSE;
return -1;
}
示例8: start_socket
static int
start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) {
int id = request->id;
result->id = id;
result->opaque = request->opaque;
result->ud = 0;
result->data = NULL;
struct socket *s = &ss->slot[HASH_ID(id)];
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
return SOCKET_ERROR;
}
if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) {
if (sp_add(ss->event_fd, s->fd, s)) {
s->type = SOCKET_TYPE_INVALID;
return SOCKET_ERROR;
}
s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN;
s->opaque = request->opaque;
result->data = "start";
return SOCKET_OPEN;
} else if (s->type == SOCKET_TYPE_CONNECTED) {
s->opaque = request->opaque;
result->data = "transfer";
return SOCKET_OPEN;
}
return -1;
}
示例9: socket_req_opt
static int socket_req_opt(struct _opt_req *req, struct socket_message *msg) {
struct socket *sock;
sock = &S.slot[HASH_ID(req->id)];
if (sock->type == SOCKET_TYPE_INVALID || sock->id != req->id) {
return -1;
}
setsockopt(sock->fd, IPPROTO_TCP, req->what, (const char *)&req->value, sizeof(req->value));
return -1;
}
示例10: setopt_socket
static void
setopt_socket(struct socket_server *ss, struct request_setopt *request) {
int id = request->id;
struct socket *s = &ss->slot[HASH_ID(id)];
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
return;
}
int v = request->value;
setsockopt(s->fd, IPPROTO_TCP, request->what, &v, sizeof(v));
}
示例11: socket_server_send_lowpriority
void
socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) {
struct socket * s = &ss->slot[HASH_ID(id)];
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
return;
}
struct request_package request;
request.u.send.id = id;
request.u.send.sz = sz;
request.u.send.buffer = (char *)buffer;
send_request(ss, &request, 'P', sizeof(request.u.send));
}
示例12: socket_req_listen
static int socket_req_listen(struct _listen_req *req, struct socket_message *msg) {
struct socket *sock = socket_new(req->fd, req->id, PROTOCOL_TCP, req->ud, 0);
if (sock == 0) {
goto _failed;
}
sock->type = SOCKET_TYPE_PLISTEN;
return -1;
_failed:
close(req->fd);
msg->ud = req->ud;
msg->id = req->id;
msg->data = "socket limit";
S.slot[HASH_ID(req->id)].type = SOCKET_TYPE_INVALID;
return SOCKET_ERR;
}
示例13: socket_server_send
// return -1 when error
// send_socket HIGH
int64_t
socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) {
struct socket * s = &ss->slot[HASH_ID(id)];
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
free_buffer(ss, buffer, sz);
return -1;
}
struct request_package request;
request.u.send.id = id;
request.u.send.sz = sz;
request.u.send.buffer = (char *)buffer;
send_request(ss, &request, 'D', sizeof(request.u.send));
return s->wb_size;
}
示例14: socket_send
long socket_send(int id, const void *data, int size, int priority) {
struct socket_req req;
struct socket * sock = &S.slot[HASH_ID(id)];
if (sock->id != id || sock->type == SOCKET_TYPE_INVALID) {
freebuffer((void *)data, size);
return -1;
}
memset(&req, 0, sizeof req);
req.req = SOCKET_REQ_SEND;
req.u.send.id = id;
req.u.send.data = (char *)data;
req.u.send.size = size;
req.u.send.priority = priority;
socket_send_req(&req);
return sock->wb_size;
}
示例15: add_udp_socket
static void
add_udp_socket(struct socket_server *ss, struct request_udp *udp) {
int id = udp->id;
int protocol;
if (udp->family == AF_INET6) {
protocol = PROTOCOL_UDPv6;
} else {
protocol = PROTOCOL_UDP;
}
struct socket *ns = new_fd(ss, id, udp->fd, protocol, udp->opaque, true);
if (ns == NULL) {
close(udp->fd);
ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID;
return;
}
ns->type = SOCKET_TYPE_CONNECTED;
memset(ns->p.udp_address, 0, sizeof(ns->p.udp_address));
}