OSDN Git Service

addid library source 20140221
[zither/ds-zither.git] / libms / socket_readasth.c
1 // socket_readasth.c
2 // masashi shimakura 20140223
3
4 #include<stdio.h>
5 #include<fcntl.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <string.h>
9
10 #include <sys/select.h>
11
12 #include<pthread.h>
13
14 #include "libms.h"
15
16 // #define SOCKET_READBUG
17
18
19 // t1
20 int read_check_th(struct th_str * th_struct)
21 {
22 int co;
23 co = 0;
24 th_struct->t1 = pthread_self();
25
26 for(; th_struct->th_out < th_struct->th_timeout; ){
27
28    usleep(1000);
29    co++;
30    if( co >= 1000 ){
31       th_struct->th_out++;
32       co = 0;
33       }
34
35    safe_pthread_mutex_lock(&th_struct->read_mutex);
36    if(1 == th_struct->th_ret){
37       pthread_mutex_unlock(&th_struct->read_mutex);
38       break;
39       }
40    pthread_mutex_unlock(&th_struct->read_mutex);
41
42    if(th_struct->th_out >= th_struct->th_timeout){
43
44       safe_pthread_mutex_lock(&th_struct->read_mutex);
45       th_struct->th_ret = -1;
46       pthread_mutex_unlock(&th_struct->read_mutex);
47
48       close(th_struct->th_sk);
49       break;
50       }
51    }
52
53 return 0;
54 }
55
56
57
58 // t2
59 int read_get_th(struct th_str * th_struct)
60 {
61 int stat;
62
63 stat = read(th_struct->th_sk, th_struct->th_cha, 32);
64 th_struct->th_stat = stat;
65 th_struct->th_count = th_struct->th_count + stat; 
66
67 //printf("========%d==%d======== %s \n", stat, th_struct->th_count, th_struct->th_cha);
68
69 // CLOSEされてない場合はret = 1
70 safe_pthread_mutex_lock(&th_struct->read_mutex);
71 if(th_struct->th_ret == -1){
72    th_struct->th_stat = -2;
73    }
74 else{
75    th_struct->th_ret = 1;
76    }
77 pthread_mutex_unlock(&th_struct->read_mutex);
78
79 return 0;
80 }
81
82
83
84
85 int socket_readasth(int sk, char * readdata, int max, int interval, int mode, int * size)
86 {
87 int ret, stat;
88 char cha[64 + 3];
89
90 // read() 用thread構造体の宣言
91 // th_str は、libms.h 内で定義
92 struct th_str * th_struct;
93 struct th_str th_s;
94 int tret;
95
96 struct timespec tsv;
97 // struct timeval tv;
98
99 int retval;
100 fd_set rfds;
101
102 // tv.tv_sec = interval;
103 // tv.tv_usec = 0;
104 tsv.tv_sec = interval;
105 tsv.tv_nsec = 0;
106
107 memset(&th_s, 0x00, sizeof(th_s));
108 th_struct = &th_s;
109
110 tret = 0;
111 th_struct->th_ret = 0;
112 th_struct->th_out = 0;
113 th_struct->th_count = 0;
114 th_struct->th_sk = sk;
115 th_struct->th_timeout = interval;
116
117 memset(cha, 0x00, 64);
118 ret = 0;
119 stat = 0;
120
121
122 while(1){
123    FD_ZERO(&rfds);
124    FD_SET(sk, &rfds);
125
126 // retval = select(sk + 1, &rfds, NULL, NULL, &tv);
127    retval = pselect(sk + 1, &rfds, NULL, NULL, &tsv, NULL);
128
129    if(retval < 0){
130       #ifdef SOCKET_READBUG
131       printf("SOCKET_READBUG: socket_readasth(1): NG select <%d> ERRNO: %d %s\n",
132         retval, errno, strerror(errno));
133       #endif
134       ret = -1;
135       break;
136       }
137    else if(retval == 0){
138       #ifdef SOCKET_READBUG
139       printf("SOCKET_READBUG: socket_readasth(2): NG select. time out break <%d> ERRNO: %d %s\n",
140         retval, errno, strerror(errno));
141       #endif
142       ret = -2;
143       break;
144       }
145    else{
146
147       #ifdef SOCKET_READBUG
148       printf("SOCKET_READBUG: socket_readasth(3): OK select.  break <%d> ERRNO: %d %s\n",
149         retval, errno, strerror(errno));
150       #endif
151
152       pthread_mutex_init(&th_struct->read_mutex, NULL);
153
154       memset(th_struct->th_cha, 0x00, 64);
155       th_struct->th_stat = 1024;
156       if((tret = pthread_create(&th_struct->t1, NULL, (void *)read_check_th, (long int *)th_struct)) != 0){
157          fprintf(stderr,"PTHERAD_ERR: socket_readasth(): pthread_create(read_check_th): %d %d\n",tret, errno);
158          }
159       if((tret = pthread_create(&th_struct->t2, NULL, (void *)read_get_th, (long int *)th_struct)) != 0){
160          fprintf(stderr,"PTHREAD_ERR: socket_readasth(): pthread_create(read_get_th): %d %d\n",tret, errno);
161          }
162
163       pthread_join( th_struct->t1, NULL);
164       pthread_join( th_struct->t2, NULL);
165
166       pthread_mutex_destroy(&th_struct->read_mutex);
167
168       stat = th_struct->th_stat;
169       self_memcpy(cha, th_struct->th_cha, 64);
170
171       #ifdef SOCKET_READBUG
172       printf("SOCKET_READBUG: socket_readasth(5): ===== addr = %d =====read_get_th()===th_struct->th_ret=%d ===th_struct->th_stat=%d data = <%s>\n",
173        (int)&th_struct, th_struct->th_ret, th_struct->th_stat, cha);
174       #endif
175
176
177       #ifdef SOCKET_READBUG
178       printf("SOCKE_READBUG: socket_readasth(6): stat = %d string = %s front_char = %02x %02x %02x %02x\n", stat, cha, cha[0], cha[1], cha[2], cha[3]);
179       printf("rear_char = %02x %02x %02x\n",cha[stat -2], cha[stat -1], cha[stat]);
180       #endif
181
182       if(stat < 0){
183          #ifdef SOCKET_READBUG
184          printf("SOCKET_READBUG: socke_readasth(7): BREAK stat==0\n");
185          #endif
186          ret = stat;
187          break;
188          }
189
190       if(stat == 0 && cha[0] == 0x00 && cha[1] == 0x00){
191          #ifdef SOCKET_READBUG
192          printf("SOCKET_READBUG: socket_readasth(8): BREAK stat==0 cha0=00 cha1=00\n");
193          #endif
194          break;
195          }
196
197       if(stat == 1 && cha[0] == 0x0a && cha[1] == 0x00){
198          #ifdef SOCKET_READBUG
199          printf("SOCKET_READBUG: socket_readasth(9): BREAK stat==1 cha0=0a cha1=00\n");
200          #endif
201          break;
202          }
203
204       if(stat >= 2){
205          if(mode == 1){
206             if(cha[stat - 1] == 0x0a){    // if if 優先あり
207                #ifdef SOCKET_READBUG
208                printf("SOCKET_READBUG: socket_readasth(10): BREAK -1=0d [mode 1]\n");
209                #endif
210                strncat(readdata, cha, stat);
211                memset(cha, 0x00, 33);
212                ret = ret + stat;
213                break;
214                }
215
216             if(cha[stat - 2] == 0x0d && cha[stat - 1] == 0x0a){  // if if 優先順位あり
217                #ifdef SOCKET_READBUG
218                printf("SOCKET_READBUG: socket_readasth(11): BREAK -2=0d -1=0a [mode 1]\n");
219                #endif
220                strncat(readdata, cha, stat);
221                memset(cha, 0x00, 33);
222                ret = ret + stat;
223                break;
224                }
225             else{
226                #ifdef SOCKET_READBUG
227                printf("SOCKET_READBUG: socket_readasth(12): BREAK else [mode 1]\n");
228                #endif
229                strncat(readdata, cha, stat);
230                memset(cha, 0x00, 33);
231                ret = ret + stat;
232                if(ret > max - 32){
233                   #ifdef SOCKET_READBUG
234                   printf("SOCKET_READBUG: socket_readasth(13): read size err [mode 1].\n");
235                   #endif
236                   break;
237                   }
238                }
239             }
240          else if(mode == 0){
241             if(cha[stat - 2] == 0x0d && cha[stat - 1] == 0x0a){  // if if 優先順位あり
242                #ifdef SOCKET_READBUG
243                printf("SOCKET_READBUG: socket_readasth(14): BREAK -2=0d -1=0a [mode 0]\n");
244                #endif
245                strncat(readdata, cha, stat);
246                memset(cha, 0x00, 33);
247                ret = ret + stat;
248                break;
249                }
250             else{
251                strncat(readdata, cha, stat);
252                memset(cha, 0x00, 33);
253                ret = ret + stat;
254                if(ret > max - 32){
255                   #ifdef SOCKET_READBUG
256                   printf("SOCKET_READBUG: socket_readasth(15): read size err [mode 0].\n");
257                   #endif
258                   break;
259                   }
260                }
261             }
262          else{
263             strncat(readdata, cha, stat);
264             memset(cha, 0x00, 33);
265             ret = ret + stat;
266             if(ret > max - 32){
267                #ifdef SOCKET_READBUG
268                printf("SOCKET_READBUG: socket_readasth(16): read size err [mode else].\n");
269                #endif
270                break;
271                }
272             }
273          }
274       }
275    } // while()
276
277
278
279 * size = th_struct->th_count;
280
281 return(ret);
282 }
283
284
285