-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
92 lines (88 loc) · 1.73 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
//
// list.c
//
//
//
//websocket 链表
#define PRINT
#include<malloc.h>
#include"list.h"
#define LIST_LEN 1000
//创建链表,生成第一个节点。
socket_list* list_create()
{
int i;
list_head = malloc(sizeof(socket_list)*LIST_LEN);
list_tail = list_head;
socket_list* head=list_head;
for(i=0;i<LIST_LEN;i++)
{
list_tail=(char*)list_tail+sizeof(socket_list);
head->sock_fd=-2;
head->pnext=list_tail;
head=list_tail;
}
list_tail=(char*)list_tail-sizeof(socket_list);
return (socket_list*)list_head;
}
//添加链接的websocket描述字
void list_insert(socket_list* head,int fd)
{
while(head->pnext->sock_fd != -2)
head=head->pnext;
head->pnext->sock_fd=fd;
#ifdef PRINT
printf("insert %d\n",head->pnext->sock_fd);
#endif
}
//从任务链表取得任务
int list_get_task(socket_list* head)
{
static int fd;
if( (fd=head->pnext->sock_fd) != -2)
{
list_tail->pnext=head->pnext;
head->pnext=head->pnext->pnext;
list_tail=list_tail->pnext;
list_tail->sock_fd=-2;
#ifdef PRINT
printf("get %d\n",fd);
#endif
return fd;//成功取得任务
}
return 0;
}
//删除断开链接的socket
void list_remove(socket_list* head,int fd)
{
while(head->pnext->sock_fd != fd)
head=head->pnext;
list_tail->pnext=head->pnext;
head->pnext=head->pnext->pnext;
list_tail=list_tail->pnext;
list_tail->sock_fd=-2;
}
//清除链表
void list_clear(socket_list* head)
{
free(list_head);
}
//发送信息到链表上的websocket
void list_send(socket_list* head,char* buf,int length)
{
#ifdef PRINT
printf("sendto ");
#endif
while(head->pnext->sock_fd != -2)
{
send(head->pnext->sock_fd,buf,length,0);
#ifdef PRINT
printf("%d ",head->pnext->sock_fd);
#endif
head=head->pnext;
}
#ifdef PRINT
printf("\n");
#endif
}