2 // masashi shimakura 20140223
10 #include <sys/select.h>
16 // #define SOCKET_READBUG
20 int read_check_th(struct th_str * th_struct)
24 th_struct->t1 = pthread_self();
26 for(; th_struct->th_out < th_struct->th_timeout; ){
35 safe_pthread_mutex_lock(&th_struct->read_mutex);
36 if(1 == th_struct->th_ret){
37 pthread_mutex_unlock(&th_struct->read_mutex);
40 pthread_mutex_unlock(&th_struct->read_mutex);
42 if(th_struct->th_out >= th_struct->th_timeout){
44 safe_pthread_mutex_lock(&th_struct->read_mutex);
45 th_struct->th_ret = -1;
46 pthread_mutex_unlock(&th_struct->read_mutex);
48 close(th_struct->th_sk);
59 int read_get_th(struct th_str * th_struct)
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;
67 //printf("========%d==%d======== %s \n", stat, th_struct->th_count, th_struct->th_cha);
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;
75 th_struct->th_ret = 1;
77 pthread_mutex_unlock(&th_struct->read_mutex);
85 int socket_readasth(int sk, char * readdata, int max, int interval, int mode, int * size)
90 // read() 用thread構造体の宣言
91 // th_str は、libms.h 内で定義
92 struct th_str * th_struct;
102 // tv.tv_sec = interval;
104 tsv.tv_sec = interval;
107 memset(&th_s, 0x00, sizeof(th_s));
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;
117 memset(cha, 0x00, 64);
126 // retval = select(sk + 1, &rfds, NULL, NULL, &tv);
127 retval = pselect(sk + 1, &rfds, NULL, NULL, &tsv, NULL);
130 #ifdef SOCKET_READBUG
131 printf("SOCKET_READBUG: socket_readasth(1): NG select <%d> ERRNO: %d %s\n",
132 retval, errno, strerror(errno));
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));
147 #ifdef SOCKET_READBUG
148 printf("SOCKET_READBUG: socket_readasth(3): OK select. break <%d> ERRNO: %d %s\n",
149 retval, errno, strerror(errno));
152 pthread_mutex_init(&th_struct->read_mutex, NULL);
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);
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);
163 pthread_join( th_struct->t1, NULL);
164 pthread_join( th_struct->t2, NULL);
166 pthread_mutex_destroy(&th_struct->read_mutex);
168 stat = th_struct->th_stat;
169 self_memcpy(cha, th_struct->th_cha, 64);
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);
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]);
183 #ifdef SOCKET_READBUG
184 printf("SOCKET_READBUG: socke_readasth(7): BREAK stat==0\n");
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");
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");
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");
210 strncat(readdata, cha, stat);
211 memset(cha, 0x00, 33);
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");
220 strncat(readdata, cha, stat);
221 memset(cha, 0x00, 33);
226 #ifdef SOCKET_READBUG
227 printf("SOCKET_READBUG: socket_readasth(12): BREAK else [mode 1]\n");
229 strncat(readdata, cha, stat);
230 memset(cha, 0x00, 33);
233 #ifdef SOCKET_READBUG
234 printf("SOCKET_READBUG: socket_readasth(13): read size err [mode 1].\n");
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");
245 strncat(readdata, cha, stat);
246 memset(cha, 0x00, 33);
251 strncat(readdata, cha, stat);
252 memset(cha, 0x00, 33);
255 #ifdef SOCKET_READBUG
256 printf("SOCKET_READBUG: socket_readasth(15): read size err [mode 0].\n");
263 strncat(readdata, cha, stat);
264 memset(cha, 0x00, 33);
267 #ifdef SOCKET_READBUG
268 printf("SOCKET_READBUG: socket_readasth(16): read size err [mode else].\n");
279 * size = th_struct->th_count;