-
Notifications
You must be signed in to change notification settings - Fork 0
/
snb_session.c
178 lines (159 loc) · 3.42 KB
/
snb_session.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <snb_session.h>
#include <snb_protocal.h>
static struct {
snb_session_t* head;
snb_session_t* rear;
}session_list;
static int snb_session_new_id()
{
return 0;
}
int snb_session_pipe_push_command(snb_command_pipe_t* pipe, snb_command_t* cmd)
{
if (cmd == NULL)
return -1;
SNB_LOCK_MUTEX(pipe);
if(pipe->cmd_list_head == NULL)
{
pipe->cmd_list_head = cmd;
pipe->cmd_list_tail = cmd;
}
else
{
pipe->cmd_list_tail->next = cmd;
pipe->cmd_list_tail = pipe->cmd_list_tail->next;
}
SNB_UNLOCK_MUTEX(pipe);
}
snb_command_t* snb_session_pipe_pop_command(snb_command_pipe_t* pipe)
{
snb_command_t* cmd = NULL;
SNB_LOCK_MUTEX(pipe);
if(pipe->cmd_list_head == NULL)
{
cmd = NULL;
}
else
{
cmd = pipe->cmd_list_head;
pipe->cmd_list_head = pipe->cmd_list_head->next;
}
SNB_UNLOCK_MUTEX(pipe);
return cmd;
}
int snb_session_push_command(snb_session_t* session,
snb_command_t* cmd, pipe_type_t io)
{
if ((cmd == NULL) || (session == NULL))
return -1;
cmd->parent = session;
if(io == pipe_in)
snb_session_pipe_push_command(&session->pipe_in, cmd);
else
snb_session_pipe_push_command(&session->pipe_out, cmd);
return 0;
}
snb_command_t* snb_session_pop_command(snb_session_t* session, pipe_type_t io)
{
snb_command_t* cmd = NULL;
if(io == pipe_in)
cmd = snb_session_pipe_pop_command(&session->pipe_in);
else
cmd = snb_session_pipe_pop_command(&session->pipe_out);
return cmd;
}
snb_session_t* snb_add_session(int sock)
{
snb_session_t* session;
if(sock == -1)
return NULL;
session = SNB_MALLOC(sizeof(*session));
if(session == NULL)
return NULL;
bzero(session, sizeof(*session));
session->next = NULL;
session->passwd.usr = NULL;
session->passwd.passwd = NULL;
session->seq_num = 0;
session->sock = sock;
session->id = snb_session_new_id();
if((session->msg_buf_in = snb_alloc_msg_buf(SNB_MSG_MAX_BUFFER)) == NULL)
{
free(session);
return NULL;
}
session->msg_buf_in->state = SNB_MSG_RECV_STATE_HEAD;
if((session->msg_buf_out = snb_alloc_msg_buf(SNB_MSG_MAX_BUFFER)) == NULL)
{
free(session->msg_buf_in);
free(session);
return NULL;
}
session->msg_buf_out->state = SNB_MSG_SEND_STATE_START;
if(session_list.head == NULL && session_list.rear == NULL)
{
session_list.head = session;
session_list.rear = session;
session_list.head->next = NULL;
}
else
{
session_list.rear->next = session;
session_list.rear = session;
}
return session;
}
int snb_remove_session(snb_session_t* session)
{
snb_session_t* tmp_session;
snb_session_t* next_session;
if(session == NULL)
return -1;
tmp_session = session_list.head;
if(session_list.rear == session)
{
if(session_list.head == session)
{
session_list.head = NULL;
session_list.rear = NULL;
return 0;
}
tmp_session = session_list.head;
while(tmp_session->next != session_list.rear)
{
tmp_session = tmp_session->next;
}
tmp_session->next = NULL;
}
else
{
*session = *(session->next);
}
return 0;
}
snb_session_t* snb_find_session(int sock)
{
snb_session_t* session;
session = session_list.head;
while(session != NULL)
{
if(session->sock == sock)
return session;
session = session->next;
}
return NULL;
}
const snb_session_t* snb_iteritor_session_begin()
{
return session_list.head;
}
const snb_session_t* snb_iteritor_session_next(const snb_session_t* it)
{
if(it == NULL)
return NULL;
return it->next;
}
const snb_session_t* snb_iteritor_session_end()
{
return session_list.rear;
}