void interactive(int s) { /* Open an interactive terminal session with the daemon. Useful for debugging, not much else. */ char buf[FILEPATH_MAX+1], *nl; int c, read; while( printf("> "), fgets(buf, FILEPATH_MAX+1, stdin) != NULL ) { nl = strchr(buf, '\n'); if( nl == NULL ) { printf("Didn't send, input too long\n"); while( (c = getchar()) != '\n' && c != EOF ) /* discard */; continue; } *nl = '\0'; if( strcmp(buf, "q") == 0 ) break; soc_w(s, buf); do { if( (read = soc_r(s, buf, FILEPATH_MAX)) > 0 ) { printf("recv> `%s'\n", buf); } else { if( read < 0 ) fprintf(stderr, "Quitting for read error\n"); else fprintf(stderr, "Server closed connection\n"); exit(EXIT_FAILURE); } /* check if there's more */ } while( readwait(s, 0.2) ); } }
uint16_t REDFLY::socketRead(uint8_t *socket, uint16_t *len, uint8_t *ip, uint16_t *port, uint8_t *dst, uint16_t dst_size) { uint8_t stop=0; uint16_t rd=0; static uint8_t last_socket=INVALID_SOCKET; static uint16_t last_len=0; static uint8_t buf[8], pos=0; static uint16_t udp_port=0; static uint8_t udp_ip[4]; if(read_state == 2) //we are currently reading data { if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket { return 0; } if(dst_size == 0) //only get len and socket { *socket = last_socket; *len = last_len; return 0; } } do { switch(read_state) { case 0: //restart searching read_state = 1; last_socket = INVALID_SOCKET; last_len = 0; pos = 0; udp_port = 0; udp_ip[0] = 0; udp_ip[1] = 0; udp_ip[2] = 0; udp_ip[3] = 0; case 1: //search while((pos<8) && available()) { buf[pos++] = read(); } if(pos >= 8) { if((buf[0] == 'R') && \ (buf[1] == 'S') && \ (buf[2] == 'I') && \ (buf[3] == '_') && \ (buf[4] == 'R') && \ (buf[5] == 'E') && \ (buf[6] == 'A') && \ (buf[7] == 'D')) //RSI_READ { last_socket = readwait(); last_len = readwait(); last_len |= readwait()<<8; if((last_socket == INVALID_SOCKET) || (last_len == 0)) { read_state = 0; last_socket = INVALID_SOCKET; last_len = 0; break; } read_state = 2; //get IP and port on UDP connection if(socketState(last_socket) == SOCKET_UDP) { udp_ip[0] = readwait(); udp_ip[1] = readwait(); udp_ip[2] = readwait(); udp_ip[3] = readwait(); udp_port = readwait(); udp_port |= readwait()<<8; } if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket { return 0; } if(dst_size == 0) { stop = 1; //break loop } } else if((buf[0] == 'S') && \ (buf[1] == 'I') && \ (buf[2] == '_') && \ (buf[3] == 'C') && \ (buf[4] == 'L') && \ (buf[5] == 'O') && \ (buf[6] == 'S') && \ (buf[7] == 'E')) //SI_CLOSE { last_socket = readwait(); readwait(); readwait(); //trailing \r\n last_len = 0; rd = 0xFFFF; read_state = 0; stop = 1; //break loop for(uint8_t i=0; i<MAX_SOCKETS; i++) { if(socket_state[i].handle == last_socket) { socket_state[i].handle = INVALID_SOCKET; socket_state[i].state = SOCKET_CLOSED; break; } } } else { //move one byte pos = 7; buf[0] = buf[1]; buf[1] = buf[2]; buf[2] = buf[3]; buf[3] = buf[4]; buf[4] = buf[5]; buf[5] = buf[6]; buf[6] = buf[7]; } } break; case 2: //receive *dst = readwait(); dst_size--; rd++; if(--last_len == 0) { readwait(); readwait(); //trailing \r\n read_state = 0; stop = 1; //break loop } else if(dst_size == 0) { stop = 1; //break loop } break; } }while(available() && (stop == 0)); *socket = last_socket; *len = last_len; if(ip && udp_ip[0]) { ip[0] = udp_ip[0]; ip[1] = udp_ip[1]; ip[2] = udp_ip[2]; ip[3] = udp_ip[3]; } if(port && udp_port) { *port = udp_port; } return rd; }