00001
00050
00051
00052
00053
00054
00055
00056
00057 #include <sent/stddefs.h>
00058 #include <sent/adin.h>
00059 #include <sent/tcpip.h>
00060
00061 static int adinnet_sd = -1;
00062 static int adinnet_asd = -1;
00063 static boolean last_is_segmented = FALSE;
00064
00065 #ifdef FORK_ADINNET
00066 static pid_t child;
00067 #endif
00068
00077 boolean
00078 adin_tcpip_standby(int freq, void *port_str)
00079 {
00080 int port;
00081
00082 port = atoi((char *)port_str);
00083
00084 if ((adinnet_sd = ready_as_server(port)) < 0) {
00085 j_printerr("adin_tcpip_standby: cannot ready for server\n");
00086 return FALSE;
00087 }
00088
00089 last_is_segmented = FALSE;
00090
00091 return TRUE;
00092 }
00093
00099 boolean
00100 adin_tcpip_begin()
00101 {
00102 if (last_is_segmented) {
00103
00104 } else {
00105 #ifdef FORK_ADINNET
00106
00107
00108
00109 for (;;) {
00110
00111 j_printerr("waiting connection...\n");
00112 adinnet_asd = accept_from(adinnet_sd);
00113
00114 child = fork();
00115 if (child < 0) {
00116 j_printerr("adin_tcpip_standby: fork failed\n");
00117 return FALSE;
00118 }
00119
00120 if (child == 0) {
00121 break;
00122 } else {
00123 j_printerr("forked process [%d] handles this request\n", child);
00124 }
00125 }
00126 #else
00127 j_printerr("waiting connection...\n");
00128 adinnet_asd = accept_from(adinnet_sd);
00129 #endif
00130 }
00131 return TRUE;
00132 }
00133
00145 boolean
00146 adin_tcpip_end()
00147 {
00148 if (!last_is_segmented) {
00149
00150 close_socket(adinnet_asd);
00151 #ifdef FORK_ADINNET
00152
00153 j_error("connection end\n");
00154 #else
00155
00156 j_printerr("connection end\n");
00157 #endif
00158 }
00159 return TRUE;
00160 }
00161
00176 int
00177 adin_tcpip_read(SP16 *buf, int sampnum)
00178 {
00179 int cnt, ret;
00180 fd_set rfds;
00181 struct timeval tv;
00182 int status;
00183
00184
00185 FD_ZERO(&rfds);
00186 FD_SET(adinnet_asd, &rfds);
00187 tv.tv_sec = 0;
00188 tv.tv_usec = 1;
00189 status = select(adinnet_asd+1, &rfds, NULL, NULL, &tv);
00190 if (status < 0) {
00191 j_printerr("adin_tcpip_read: cannot poll\n");
00192 return -2;
00193 }
00194 if (status > 0) {
00195
00196 ret = rd(adinnet_asd, (char *)buf, &cnt, sampnum * sizeof(SP16));
00197 if (ret == 0) {
00198
00199 last_is_segmented = TRUE;
00200 return -1;
00201 }
00202 if (ret < 0) {
00203
00204 last_is_segmented = FALSE;
00205 return -1;
00206 }
00207 } else {
00208 cnt = 0;
00209 }
00210 cnt /= sizeof(SP16);
00211 #ifdef WORDS_BIGENDIAN
00212 swap_sample_bytes(buf, cnt);
00213 #endif
00214 return cnt;
00215 }
00216
00222 boolean
00223 adin_tcpip_send_pause()
00224 {
00225 int count;
00226 char com;
00227
00228 com = '0';
00229 count = wt(adinnet_asd, &com, 1);
00230 if (count < 0) j_printerr("adin_tcpip_send_pause: cannot send command to client\n");
00231 return TRUE;
00232 }
00233
00239 boolean
00240 adin_tcpip_send_resume()
00241 {
00242 int count;
00243 char com;
00244
00245 com = '1';
00246 count = wt(adinnet_asd, &com, 1);
00247 if (count < 0) j_printerr("adin_tcpip_send_resume: cannot send command to client\n");
00248 return TRUE;
00249 }
00250
00256 boolean
00257 adin_tcpip_send_terminate()
00258 {
00259 int count;
00260 char com;
00261
00262 com = '2';
00263 count = wt(adinnet_asd, &com, 1);
00264 if (count < 0) j_printerr("adin_tcpip_send_terminate: cannot send command to client\n");
00265 return TRUE;
00266 }
00267