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 EXTERN_SERVER_RECVSTATUS
49 int
50         Drd64_Server_RecvStatus_Init(
51                 int i_alloc_units )
52 {
53         int             i_cnt;
54         void    *pv_temp;
55
56         gpp_recvstat    = (Drd64_Server_RecvStatus **)malloc(
57                                         sizeof( Drd64_Server_RecvStatus *) * FD_SETSIZE );
58         if( NULL == gpp_recvstat )      {
59                 return 0x01;
60         }
61
62         gi_recvstat_max = 0;
63
64         gp_recvalloc = (Drd64_Server_RecvStatus *)malloc(
65                                                 sizeof( Drd64_Server_RecvStatus )
66                                                         * i_alloc_units );
67         if( NULL == gp_recvalloc )      {
68                 free( gpp_recvstat );
69                 gpp_recvstat    = NULL;
70                 return 0x02;
71         }
72
73         memset( gpp_recvstat, 0x00, 
74                                         sizeof( Drd64_Server_RecvStatus *) * FD_SETSIZE );
75         
76         memset( gp_recvalloc, 0x00, 
77                                                 sizeof( Drd64_Server_RecvStatus )
78                                                         * i_alloc_units );
79         for( i_cnt = 0; i_cnt < i_alloc_units; i_cnt++ )        {
80                 (gp_recvalloc + i_cnt)->i_fds_id        = -1;
81                 pv_temp = malloc( DRD64_MAX_PACKET_LENGTH );
82                 if( NULL == pv_temp )   { break; }
83                 
84                 (gp_recvalloc + i_cnt)->pv_buf  = pv_temp;
85         }
86
87         if( NULL == pv_temp     )       {
88                 for( ; i_cnt >= 0; i_cnt-- )    {
89                         free( (gp_recvalloc + i_cnt)->pv_buf );
90                         (gp_recvalloc + i_cnt)->pv_buf  = NULL;
91                 }
92                 free( gp_recvalloc );
93                 gi_recvalloc_max        = 0;
94                 gi_recvalloc_now        = 0;
95                 gp_recvalloc            = NULL;
96         
97                 free( gpp_recvstat );
98                 gpp_recvstat            = NULL;
99                 
100                 return 0x01;
101         }
102
103         gi_recvalloc_max        = i_alloc_units;
104         gi_recvalloc_now        = 0;
105
106         return 0x00;
107 }
108
109
110 EXTERN_SERVER_RECVSTATUS
111 int
112         Drd64_Server_RecvStatus_Term(
113                 void )
114 {
115         int             i_cnt;
116
117         for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )     {
118                         free( (gp_recvalloc + i_cnt)->pv_buf );
119                         (gp_recvalloc + i_cnt)->pv_buf  = NULL;
120         }
121
122         free( gp_recvalloc );
123         gi_recvalloc_max        = 0;
124         gi_recvalloc_now        = 0;
125         gp_recvalloc            = NULL;
126
127         free( gpp_recvstat );
128         gpp_recvstat            = NULL;
129
130         return;
131 }
132
133
134 EXTERN_SERVER_RECVSTATUS
135 Drd64_Server_RecvStatus *
136         Drd64_Server_RecvStatus_AllocRecvStatus(
137                 int     i_fds,
138                 int     i_alloc_units )
139 {
140         int             i_cnt;
141         Drd64_Server_RecvStatus         *p_recv;
142         Drd64_Server_RecvStatus         *p_temp;
143
144         p_recv  = NULL;
145
146         if( gi_recvalloc_now >= gi_recvalloc_max )      {
147                 for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )             {
148                         if( -1 == (gp_recvalloc + i_cnt)->i_fds_id )    {
149                                 p_recv  = (gp_recvalloc + i_cnt);
150                                 break;
151                         }
152                 }
153
154                 if( NULL == p_recv )    {
155                         p_temp  = realloc( gp_recvalloc,
156                                         gi_recvalloc_max + i_alloc_units );
157                         if( NULL == p_temp )    { return NULL; }
158                 
159                         for( i_cnt = gi_recvalloc_max;
160                                         i_cnt < i_alloc_units + gi_recvalloc_max;
161                                         i_cnt++ )       {
162                                 (gp_recvalloc + i_cnt)->i_fds_id        = -1;
163                         }
164
165                         gp_recvalloc    = p_temp;
166                         gi_recvalloc_max        += i_alloc_units;
167                         p_recv  = gp_recvalloc + gi_recvalloc_now++;
168                 }
169         }
170         else    {
171                 p_recv  = gp_recvalloc + gi_recvalloc_now++;
172         }
173
174         p_recv->i_fds_id        = i_fds;
175         *(gpp_recvstat + i_fds) = p_recv;
176         if( i_fds + 1 > gi_recvstat_max )
177                 { gi_recvstat_max       = i_fds + 1; }
178
179         return p_recv;
180 }
181
182
183 EXTERN_SERVER_RECVSTATUS
184 void
185         Drd64_Server_RecvStatus_FreeRecvStatus(
186                 int i_fds )
187 {
188         int             i_cnt;
189         Drd64_Server_RecvStatus *p_recv_now;
190         Drd64_Server_RecvStatus *p_recv_temp;
191
192         p_recv_now      = *(gpp_recvstat + i_fds);
193
194         if( NULL != p_recv_now )        {
195                 *(gpp_recvstat + i_fds) = NULL;
196
197                 if( i_fds + 1 == gi_recvstat_max )      {
198                         for( i_cnt = i_fds+1; i_cnt > 0; i_cnt-- )              {
199                                 p_recv_temp     = *(gpp_recvstat + i_cnt);
200                                 if( NULL != p_recv_temp )       {
201                                         gi_recvstat_max = i_cnt + 1;
202                                         break;
203                                 }
204                         }
205
206                         if( 0 == i_cnt ) { gi_recvstat_max      = 0; }
207                 }
208
209                 p_recv_now->i_fds_id    = -1;
210         }
211         
212         return;
213 }
214
215
216 EXTERN_SERVER_RECVSTATUS
217 Drd64_Server_RecvStatus *
218         Drd64_Server_RecvStatus_GetRecvStatus(
219                 int             i_fds )
220 {
221         return *(gpp_recvstat + i_fds);
222 }
223
224
225 EXTERN_SERVER_RECVSTATUS
226 int
227         Drd64_Server_RecvStatus_GetPacketStatus(
228                 int             i_fds )
229 {
230         Drd64_Server_RecvStatus         *p_recv;
231         p_recv  = *(gpp_recvstat + i_fds);
232         
233         if( NULL == p_recv )    { return -1; }
234
235         return p_recv->b_recv_status;
236 }
237
238
239 EXTERN_SERVER_RECVSTATUS
240 int
241         Drd64_Server_RecvStatus_GetCmdStatus(
242                 int             i_fds )
243 {
244         Drd64_Server_RecvStatus         *p_recv;
245         p_recv  = *(gpp_recvstat + i_fds);
246         
247         if( NULL == p_recv )    { return -1; }
248
249         return p_recv->i_command_status;
250 }
251
252
253 EXTERN_SERVER_RECVSTATUS
254 int
255         Drd64_Server_RecvStatus_SetCmdStatus(
256                 int             i_fds,
257                 int             i_command_status )
258 {
259         Drd64_Server_RecvStatus         *p_recv;
260         p_recv  = *(gpp_recvstat + i_fds);
261         
262         if( NULL == p_recv )    { return -1; }
263
264         p_recv->i_command_status        = i_command_status;
265
266         return p_recv->i_command_status;
267 }
268
269
270 EXTERN_SERVER_RECVSTATUS
271 int
272         Drd64_Server_RecvStatus_GetRecvStatMax(
273                 void )
274 {
275         return gi_recvstat_max;
276 }
277
278 /* EOF of drd64_.c ----------------------------------- */