OSDN Git Service

*** empty log message ***
[drdeamon64/drdeamon64.git] / deamon / drd64_server_recvstatus.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #include"drd64_server.h"
38 #define DRD64_SRC_SERVER_RECVSTATUS
39 #include"drd64_server_recvstatus.h"
40
41 Drd64_Server_RecvStatus         **gpp_recvstat;
42 Drd64_Server_RecvStatus         *gp_recvalloc;
43 int                                                     gi_recvalloc_max;
44 int                                                     gi_recvalloc_now;
45 int                                                     gi_recvstat_max;
46
47
48 int
49         Drd64_Server_RecvStatus_FreeRecvStatus_All(
50                 void )
51 {
52         int             i_cnt;
53
54         for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )     {
55                         free( (gp_recvalloc + i_cnt)->pv_buf );
56                         (gp_recvalloc + i_cnt)->pv_buf  = NULL;
57         }
58
59         free( gp_recvalloc );
60         gi_recvalloc_max        = 0;
61         gi_recvalloc_now        = 0;
62         gp_recvalloc            = NULL;
63
64         free( gpp_recvstat );
65         gpp_recvstat            = NULL;
66
67         return;
68 }
69
70
71 Drd64_Server_RecvStatus *
72         Drd64_Server_RecvStatus_AllocRecvStatus_Unit(
73                 int             i_alloc_units )
74 {
75         int             i_cnt;
76         int             i_init_start_id;
77         int             i_init_end_id;
78         void    *pv_temp;
79         Drd64_Server_RecvStatus         *p_recvs;
80
81         p_recvs = NULL;
82
83         if( 0 == gi_recvalloc_max )     {
84                 p_recvs = (Drd64_Server_RecvStatus *)malloc(
85                                                 sizeof( Drd64_Server_RecvStatus )
86                                                         * i_alloc_units );
87
88                 i_init_start_id         = 0;
89                 i_init_end_id           = i_alloc_units;
90         }
91         else    {
92                 p_recvs = realloc( gp_recvalloc,
93                                         gi_recvalloc_max + i_alloc_units );
94
95                 i_init_start_id         = gi_recvalloc_max;
96                 i_init_end_id           = gi_recvalloc_max + i_alloc_units;
97         }
98         
99         if( NULL == p_recvs )   {
100                 return NULL;
101         }
102
103
104         for( i_cnt = i_init_start_id; i_cnt < i_init_end_id; i_cnt++ )  {
105                 memset( (p_recvs + i_cnt), 0x00, 
106                                         sizeof( Drd64_Server_RecvStatus ) );
107                 (p_recvs + i_cnt)->i_fds_id             = -1;
108
109                 pv_temp = malloc( DRD64_MAX_PACKET_LENGTH );
110                 if( NULL == pv_temp )   { break; }
111                 (p_recvs + i_cnt)->pv_buf               = pv_temp;
112                 gi_recvalloc_max++;
113         }
114
115         if( gi_recvalloc_max == i_init_start_id )       {
116                 return NULL;
117         }
118
119         gp_recvalloc    = p_recvs;
120         return p_recvs;
121 }
122
123
124 EXTERN_SERVER_RECVSTATUS
125 int
126         Drd64_Server_RecvStatus_Init(
127                 int i_alloc_units )
128 {
129         Drd64_Server_RecvStatus *p_recvs;
130
131         /* Alloc RecvStatus Struct Management Area --- */
132         gpp_recvstat
133                 = (Drd64_Server_RecvStatus **)malloc(
134                                         sizeof( Drd64_Server_RecvStatus *)
135                                                 * FD_SETSIZE * DRD64_PACKET_MAX_MULTIPLE);
136         if( NULL == gpp_recvstat )      { return 0x01; }
137
138         memset( gpp_recvstat, 0x00, 
139                                         sizeof( Drd64_Server_RecvStatus *)
140                                                 * FD_SETSIZE * DRD64_PACKET_MAX_MULTIPLE );
141
142         gi_recvstat_max = 0;
143         
144         /* Alloc RecvStatus Area ( with Packet Data buffer ) ---*/
145         p_recvs = Drd64_Server_RecvStatus_AllocRecvStatus_Unit( i_alloc_units );
146         if( NULL == p_recvs )   {
147                 Drd64_Server_RecvStatus_FreeRecvStatus_All();
148                 return 0x02;
149         }
150
151         gp_recvalloc    = p_recvs;
152         gi_recvalloc_now        = 0;
153
154         return 0x00;
155 }
156
157
158 EXTERN_SERVER_RECVSTATUS
159 int
160         Drd64_Server_RecvStatus_Term(
161                 void )
162 {
163         Drd64_Server_RecvStatus_FreeRecvStatus_All();
164
165         return;
166 }
167
168
169 EXTERN_SERVER_RECVSTATUS
170 Drd64_Server_RecvStatus *
171         Drd64_Server_RecvStatus_AllocRecvStatus(
172                 int     i_fds,
173                 int     i_alloc_units )
174 {
175         int             i_cnt;
176         Drd64_Server_RecvStatus         *p_recv;
177         Drd64_Server_RecvStatus         *p_temp;
178
179         p_recv  = NULL;
180
181         if( gi_recvalloc_now >= gi_recvalloc_max )      {
182                 for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )             {
183                         if( -1 == (gp_recvalloc + i_cnt)->i_fds_id )    {
184                                 p_recv  = (gp_recvalloc + i_cnt);
185                                 break;
186                         }
187                 }
188
189                 if( NULL == p_recv )    {
190                         p_temp
191                                 = Drd64_Server_RecvStatus_AllocRecvStatus_Unit(
192                                                 i_alloc_units );
193                         if( NULL == p_temp )    { return NULL; }
194
195                         gp_recvalloc    = p_temp;
196                         p_recv  = gp_recvalloc + gi_recvalloc_now++;
197                 }
198         }
199         else    {
200                 p_recv  = gp_recvalloc + gi_recvalloc_now++;
201         }
202
203         p_recv->i_fds_id        = i_fds;
204         *(gpp_recvstat + i_fds) = p_recv;
205         if( i_fds + 1 > gi_recvstat_max )
206                 { gi_recvstat_max       = i_fds + 1; }
207
208         return p_recv;
209 }
210
211
212 EXTERN_SERVER_RECVSTATUS
213 void
214         Drd64_Server_RecvStatus_FreeRecvStatus(
215                 int i_fds )
216 {
217         int             i_cnt;
218         Drd64_Server_RecvStatus *p_recv_now;
219         Drd64_Server_RecvStatus *p_recv_temp;
220
221         p_recv_now      = *(gpp_recvstat + i_fds);
222
223         if( NULL != p_recv_now )        {
224                 *(gpp_recvstat + i_fds) = NULL;
225
226                 if( i_fds + 1 == gi_recvstat_max )      {
227                         for( i_cnt = i_fds+1; i_cnt > 0; i_cnt-- )              {
228                                 p_recv_temp     = *(gpp_recvstat + i_cnt);
229                                 if( NULL != p_recv_temp )       {
230                                         gi_recvstat_max = i_cnt + 1;
231                                         break;
232                                 }
233                         }
234
235                         if( 0 == i_cnt ) { gi_recvstat_max      = 0; }
236                 }
237
238                 p_recv_now->i_fds_id    = -1;
239         }
240         
241         return;
242 }
243
244
245 EXTERN_SERVER_RECVSTATUS
246 Drd64_Server_RecvStatus *
247         Drd64_Server_RecvStatus_GetRecvStatus(
248                 int             i_fds )
249 {
250         return *(gpp_recvstat + i_fds);
251 }
252
253
254 EXTERN_SERVER_RECVSTATUS
255 int
256         Drd64_Server_RecvStatus_GetPacketStatus(
257                 int             i_fds )
258 {
259         Drd64_Server_RecvStatus         *p_recv;
260         p_recv  = *(gpp_recvstat + i_fds);
261         
262         if( NULL == p_recv )    { return -1; }
263
264         return p_recv->b_recv_status;
265 }
266
267
268 EXTERN_SERVER_RECVSTATUS
269 int
270         Drd64_Server_RecvStatus_GetCmdStatus(
271                 int             i_fds )
272 {
273         Drd64_Server_RecvStatus         *p_recv;
274         p_recv  = *(gpp_recvstat + i_fds);
275         
276         if( NULL == p_recv )    { return -1; }
277
278         return p_recv->i_command_status;
279 }
280
281
282 EXTERN_SERVER_RECVSTATUS
283 int
284         Drd64_Server_RecvStatus_SetCmdStatus(
285                 int             i_fds,
286                 int             i_command_status )
287 {
288         Drd64_Server_RecvStatus         *p_recv;
289         p_recv  = *(gpp_recvstat + i_fds);
290         
291         if( NULL == p_recv )    { return -1; }
292
293         p_recv->i_command_status        = i_command_status;
294
295         return p_recv->i_command_status;
296 }
297
298
299 EXTERN_SERVER_RECVSTATUS
300 int
301         Drd64_Server_RecvStatus_GetRecvStatMax(
302                 void )
303 {
304         return gi_recvstat_max;
305 }
306
307 /* EOF of drd64_.c ----------------------------------- */