int webclientReadResponse(int s, char* buf, int size) { int len, bodylen = 0; bool chunked = false; // read through the headers - figure out the content length scheme while ((len = tcpReadLine(s, webclientBuf, WEBCLIENT_BUFFER_SIZE))) { if (!strncasecmp(webclientBuf, "Content-Length", 14)) // check for content length bodylen = atoi(&webclientBuf[16]); else if (!strncasecmp(webclientBuf, "Transfer-Encoding: chunked", 26)) // check to see if we're chunked chunked = true; else if (strncmp(webclientBuf, "\r\n", 2) == 0) // final CRLF means end of headers break; } if (len <= 0) return 0; int content_read = 0; // read the actual response data into the caller's buffer, if there's any to grab if (chunked) { // first see if it's chunked int chunklen = 1; while (chunklen != 0 && content_read < size) { len = tcpReadLine(s, webclientBuf, WEBCLIENT_BUFFER_SIZE); if (siscanf(webclientBuf, "%x", &chunklen) != 1) // the first part of the chunk should indicate the chunk's length (hex) break; if (chunklen == 0) // an empty chunk indicates the end of the transfer break; if (chunklen > (size - content_read)) // make sure we have enough room to read this chunk, based on what we've already read chunklen = size - content_read; content_read += tcpRead(s, buf, chunklen); tcpReadLine(s, webclientBuf, WEBCLIENT_BUFFER_SIZE); // slurp out the remaining newlines } } // otherwise see if we got a content length else if (bodylen > 0) { while ((len = tcpRead(s, buf, size - content_read))) { content_read += len; buf += len; if (content_read >= bodylen) break; } } // lastly, just try to read until we get cut off else { while (content_read < size) { len = tcpRead(s, buf, size - content_read); if (len <= 0) break; content_read += len; buf += len; } } return content_read; }
/** * Read into a single octet from TCP. * @param devptr TCP device table entry * @return character read from TCP */ devcall tcpGetc(device *devptr) { uchar ch; int result = 0; result = tcpRead(devptr, &ch, 1); if (result != 1) { return result; } return ch; }
int init_wifi(int bot_port,char* bot_address) { int bot_sockfd = establishConnection(bot_address,bot_port); char *bot_buffer = (char *)malloc(BUFFER_SIZE*sizeof(char)); /* Initialization per bot */ do { tcpRead(bot_sockfd,bot_buffer); printf("got: %s \n",bot_buffer); }while(bot_buffer[0]!='M'); printf("Bot ready\n"); return bot_sockfd; }
/** Read a single line from a TCP socket, as terminated by CR LF (0x0D 0x0A). Make sure you have an open socket before trying to read from it. The line endings are not included in the data returned. @param socket The socket to read from. @param data Where to store the incoming data. @param length The maximum number of bytes to read. @return The number of bytes of data successfully read. @see tcpRead() for a similar example */ int tcpReadLine(int socket, char* data, int length) { int readLength; int lineLength = -1; data--; do { // Upon entering, data points to char prior to buffer, length is -1 data++; // here data points to where byte will be written lineLength++; // linelength now reflects true number of bytes readLength = tcpRead(socket, data, 1); // here, if readlength == 1, data has a new char in next position, linelength is one off, // if readlength == 0, data had no new char and linelength is right } while ((readLength == 1) && (lineLength < length - 1) && (*data != '\n')); if (readLength == 1) // here, length is corrected if there was a character lineLength++; return lineLength; }
//处理读事件 void TcpHandler::dealReadEvent(int & success) { //cout << "m_pLastNode addr is: " << m_pLastNode <<endl; if(m_pLastNode) { //cout << "m_pLastNode msglen is: "<< m_pLastNode->m_nMsgLen <<endl; //cout << "m_pLastNode m_nOffSet is: " << m_pLastNode->m_nOffSet <<endl; } else { //cout << "m_pLastNode addr is null!!!" <<endl; } evbuffer * inputBuf = bufferevent_get_input(m_pBufferevent); size_t inputLen = evbuffer_get_length(inputBuf); //cout <<"total len is : "<< inputLen <<endl; while(inputLen > 0) { //tcphandler第一次接收消息或者该node接收完消息,需要开辟新的node接受消息 if(!m_pLastNode || m_pLastNode->m_nMsgLen <= m_pLastNode->m_nOffSet) { //判断消息长度是否满足包头大小,不满足跳出 if(inputLen < PACKETHEADLEN) { break; } char data[PACKETHEADLEN] = {0}; bufferevent_read(m_pBufferevent, data, PACKETHEADLEN); struct PacketHead packetHead; memcpy(&packetHead, data, PACKETHEADLEN); cout << "packetId is : " <<packetHead.packetID << endl; cout << "packetLen is : " << packetHead.packetLen << endl; if(packetHead.packetID > 1024 || packetHead.packetID < 0) { success = 0; return; } insertNode(packetHead.packetID, packetHead.packetLen); inputLen -= PACKETHEADLEN; //cout << "after remove head the length is : " << inputLen <<endl; } //考虑可能去包头后剩余的为0 if(inputLen <= 0) { break; } //读取去除包头后剩余消息 tcpRead(inputLen); } }
//............................................................................ int main(int argc, char *argv[]) { int optChar; char comPort[FNAME_SIZE]; char inpFileName[FNAME_SIZE]; char outFileName[FNAME_SIZE]; char savFileName[FNAME_SIZE]; char matFileName[FNAME_SIZE]; TargetLink link = NO_LINK; // default configuration options strcpy(inpFileName, "qs.bin"); strcpy(comPort, "COM1"); int tcpPort = 6601; int baudRate = 38400; uint8_t quiet = 0; uint8_t tstampSize = 4; uint8_t objPtrSize = 4; uint8_t funPtrSize = 4; uint8_t sigSize = 1; uint8_t evtSize = 2; uint8_t queueCtrSize = 1; uint8_t poolCtrSize = 2; uint8_t poolBlkSize = 2; uint8_t tevtCtrSize = 2; outFileName[0] = '\0'; // Output file not specified savFileName[0] = '\0'; // Save file not specified matFileName[0] = '\0'; // Matlab file not specified time_t now = time(NULL); printf("QSPY host application %s\n" "Copyright (c) Quantum Leaps, LLC.\n" "%s\n", QSPY_VER, ctime(&now)); static char const help[] = "Syntax is: qspy [options] * = default\n\n" "OPTION DEFAULT COMMENT\n" "----------------------------------------------------------------\n" "-h help (this message)\n" "-q quiet mode (no stdout output)\n" "-o<File_name> produce output to a file\n" "-s<File_name> save the binary data to a file\n" "-m<File_name> produce a Matlab file\n" "-c<COM_port> * COM1 com port input\n" "-b<Baud_rate> 38400 baud rate selection\n" "-f<File_name> qs.bin file input\n" "-t TCP/IP input\n" "-p<TCP_Port_number> 6601 TCP/IP server port\n" "-T<tstamp_size> 4 QS timestamp size (bytes)\n" "-O<pointer_size> 4 object pointer size (bytes)\n" "-F<pointer_size> 4 function pointer size (bytes)\n" "-S<signal_size> 1 signal size (bytes)\n" "-E<event_size> 2 event size size (bytes)\n" "-Q<queue_counter_size> 1 queue counter size (bytes)\n" "-P<pool_counter_size> 2 pool counter size (bytes)\n" "-B<pool_blocksize_size> 2 pool blocksize size (bytes)\n" "-C<QTimeEvt_counter_size> 2 QTimeEvt counter size\n"; while ((optChar = getopt(argc, argv, "hqo:s:m:c:b:tp:f:T:O:F:S:E:Q:P:B:C:")) != -1) { switch (optChar) { case 'q': { // quiet mode quiet = 1; break; } case 'o': { // file output strncpy(outFileName, optarg, sizeof(outFileName)); printf("-o %s\n", outFileName); break; } case 's': { // save binary data to a file strncpy(savFileName, optarg, sizeof(savFileName)); printf("-s %s\n", savFileName); break; } case 'm': { // Matlab file output strncpy(matFileName, optarg, sizeof(matFileName)); printf("-m %s\n", matFileName); break; } case 'c': { // COM port if ((link != NO_LINK) && (link != SERIAL_LINK)) { printf("The -c option is incompatible with -p/-f\n"); return -1; // failure } strncpy(comPort, optarg, sizeof(comPort)); printf("-c %s\n", comPort); link = SERIAL_LINK; break; } case 'b': { // baud rate if ((link != NO_LINK) && (link != SERIAL_LINK)) { printf("The -b option is incompatible with -p/-f\n"); return -1; // failure } if (sscanf(optarg, "%d", &baudRate) != 1) { printf("incorrect baud rate: %s\n", optarg); return -1; // failure } printf("-b %d\n", baudRate); link = SERIAL_LINK; break; } case 'f': { // File input if (link != NO_LINK) { printf("The -f option is incompatible with -c/-b/-p\n"); return -1; // failure } strncpy(inpFileName, optarg, sizeof(inpFileName)); printf("-f %s\n", inpFileName); link = FILE_LINK; break; } case 't': { // TCP/IP input if ((link != NO_LINK) && (link != TCP_LINK)) { printf("The -t option is incompatible with -c/-b/-f\n"); return -1; } printf("-t\n"); link = TCP_LINK; break; } case 'p': { // TCP/IP port if ((link != NO_LINK) && (link != TCP_LINK)) { printf("The -p option is incompatible with -c/-b/-f\n"); return -1; } tcpPort = (int)strtoul(optarg, NULL, 10); printf("-p %d\n", tcpPort); link = TCP_LINK; break; } case 'T': { // timestamp size tstampSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'F': { // function pointer size funPtrSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'O': { // object pointer size objPtrSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'S': { // signal size sigSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'E': { // event size evtSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'Q': { // Queue counter size queueCtrSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'P': { // Memory-pool counter size poolCtrSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'B': { // Memory-pool blocksize size poolBlkSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'C': { // Time event counter size tevtCtrSize = (uint8_t)strtoul(optarg, 0, 10); break; } case 'h': // help default: { // unknown option printf(help); return -1; // error return } } } if (argc != optind) { printf(help); return -1; } // configure the Quantum Spy FILE *outFile = (outFileName[0] != '\0' ? fopen(outFileName, "w") : (FILE *)0); FILE *matFile = (matFileName[0] != '\0' ? fopen(matFileName, "w") : (FILE*)0); FILE *savFile = (savFileName[0] != '\0' ? fopen(savFileName, "wb") // open for writing binary : (FILE*)0); qsConfig(quiet, objPtrSize, funPtrSize, tstampSize, sigSize, evtSize, queueCtrSize, poolCtrSize, poolBlkSize, tevtCtrSize, outFile, matFile); static unsigned char buf[BUF_SIZE]; int n; switch (link) { case NO_LINK: // intentionally fall through case SERIAL_LINK: { // input trace data from the serial port if (!comOpen(comPort, baudRate)) { return -1; } else { printf("\nSerial port %s opened, hit any key to quit...\n\n", comPort); } while ((n = comRead(buf, sizeof(buf))) != -1) { if (n > 0) { qsParse(buf, n); if (savFile != (FILE *)0) { fwrite(buf, 1, n, savFile); } } } comClose(); break; } case FILE_LINK: { // input trace data from a file FILE *f = fopen(inpFileName, "rb"); // open for reading binary if (f == (FILE *)0) { printf("file %s not found\n", inpFileName); return -1; } do { n = fread(buf, 1, sizeof(buf), f); qsParse(buf, n); } while (n == sizeof(buf)); fclose(f); break; } case TCP_LINK: { // input trace data from the TCP port if (!tcpOpen(tcpPort)) { return -1; } else { printf("\nTCP/IP port %d opened, " "hit any key to quit...\n" "(the target must be stopped)\n", tcpPort); } while ((n = tcpRead(buf, sizeof(buf))) != -1) { if (n > 0) { qsParse(buf, n); if (savFile != (FILE *)0) { fwrite(buf, 1, n, savFile); } } } tcpClose(); break; } } if (savFile != (FILE *)0) { fclose(savFile); } if (matFile != (FILE *)0) { fclose(matFile); } printf("\nDone.\n"); return 0; // success }