int SockReadLine(SOCKET socket,char *buf,WORD bufsiz,WORD timeout) { int res; while(ReadyToRead(socket,timeout) == SUCCESS) { res = recv(socket,buf++,1,0); if(res == SOCKET_ERROR) break; if(*(buf - 1) == 0x0 || *(buf - 1) == 0xd ) { *buf = 0x0; break; } } return res; }
int main() { Sleep(5000); for (; logout==0;) { ReadyToRead(); ReadText(); action(); typing(); logout = 1; } return 0; }
int main (int argc, char ** argv ) { /// first time inits. pollfds.events = POLLIN; serverAddrInfoGotten = false; /* // Test the replacer char * b = new char[500]; strcpy(b, "Hello world!\n\t\nThinker toys."); unsigned char one41 = 141; char * buf = new char[4]; buf[0] = (unsigned char) 141; buf[1] = '\0'; strcat(b, buf); strcat(b, "Hellow world"); strcat(b, buf); b[14] = 141; int len = strlen(b); std::cout<<"\n"<<b; ReplaceStringKeys(b, len); std::cout<<"\n"; return 0; */ int udpPort = SERVER_PORT_3333; int tcpPort = 4444; int counter = 0; int MAX_COUNTER_SIZE=20; std::cout<<"\nArgc: "<<argc; bool isPi = false; /// Parse inputs. if (argc > 1) { // Parse ports char * portStr = argv[1]; udpPort = atoi(portStr); std::cout<<"\nUsing udp port: "<<udpPort; if (udpPort == 514) isPi = true; } /// Open ports bool ok = BindUdpSocket(udpPort); if (!ok) { std::cout<<"\nUnable to bind UDP socket to port "<<udpPort<<" D:"; return -1; } /// After opening port, try send to pi if specified /* if (argc > 1 && strstr(argv[1], "testpi")) { TestThePi(); }*/ /// Edited /// Set on receiving first message of the batch. Compared to when determining when to start the long sleep. long firstReceiveTime = TimeMs(); /// bool waitingForLastMessage = false; int receivedMessages = 0; /// If true, will try and batch all messages to send. bool batchMessages = true; // std::cout<<"\nStart of loop."; Sleep(500); /// Format with time-stamp std::string dateStr; while(true) { // std::cout<<"\nReady to read?"; // Read timeval t = {0, 1000}; // Check time since last message. if (waitingForLastMessage) { long timeDiff = TimeMs() - firstReceiveTime; if (isPi && batchMessages && timeDiff > 240000) { if (totalReceivedBytes > 0) SendBatchToServer(); } /// If sufficient time has passed since first message (4 minutes), start the long sleep (9 minutes). // std::cout<<"\nTimeMs(): "<<TimeMs()<<" firstReceiveTime: "<<firstReceiveTime<<" diff: "<<TimeMs() - firstReceiveTime; if (timeDiff > 240000) { waitingForLastMessage = false; /// Send acc later std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; // std::cout<<"\nBeginning long sleep for 540 seconds...\n"; Sleep(540000); receivedMessages = 0; } } /// If final message.. send it? if (!ReadyToRead(sockfd)) { if (isPi) ;//Sleep(80); else Sleep(2000); continue; } if (isPi) dateStr = currentDateTime(); // std::cout<<"\nBytes to read"; int flags = 0; // std::cout<<"\nSize of sock Addr: "<<sizeofSockaddr<<" vs "<<sizeof(sockaddr); int bytesRead = recvfrom(sockfd, inputBuffer, INPUT_BUFFER_SIZE-1, flags, &fromAddress, (socklen_t*) &sizeofSockaddr); inputBuffer[bytesRead] = '\0'; // Add null-char so it is printable. // std::cout<<"\nReceived: "<<inputBuffer; if (bytesRead == SOCKET_ERROR) { int error = GetLastError(); std::cout<<"\nError reading socket from given address. ErrorCode: "<<error; continue; } lastReceiveTime = TimeMs(); // std::cout<<"|"; //<<inputBuffer // Flag as waiting for last message. waitingForLastMessage = true; /// First message, initialize stuff. if (receivedMessages == 0) { firstReceiveTime = TimeMs(); if (!isPi) // Clear log file if on server and first message. ClearLogFile(); } ++receivedMessages; if (isPi) { // std::cout<<"\nThis is the PI,"; /// Move the body of the text. int dateStrLen = dateStr.length(); int fullString = bytesRead + dateStrLen; /// Send to the server straight away, first test. if (batchMessages) { // memmove(inputBuffer + dateStrLen, inputBuffer, bytesRead); // memcpy(inputBuffer, dateStr.c_str(), dateStrLen); /// std::cout<<"\nFull string to send: "<<inputBuffer; /// Append into total input. memcpy(totalInputBuffer + totalReceivedBytes, dateStr.c_str(), dateStrLen); totalReceivedBytes += dateStrLen; memcpy(totalInputBuffer + totalReceivedBytes, inputBuffer, fullString); totalReceivedBytes += bytesRead; // memcpy(total) // std::cout<<"\nBatched"; // memcpy(totalInputBuffer + totalReceivedBytes, inputBuffer, fullString); // totalReceivedBytes += fullString; /// Separate messages with \n /// Magic separator 141 between each line. totalInputBuffer[totalReceivedBytes - 1] = 141; // ++totalReceivedBytes; if (totalReceivedBytes > SEND_THRESHOLD) SendBatchToServer(); } else { memmove(inputBuffer + dateStrLen, inputBuffer, bytesRead); memcpy(inputBuffer, dateStr.c_str(), dateStrLen); /// std::cout<<"\nFull string to send: "<<inputBuffer; SendToServer(inputBuffer, fullString); std::cout<<" sending to server"; } } /// SERVER else { /// Replace #000 numbers with chars int newLength = ReplaceStringKeys(inputBuffer, bytesRead); std::fstream file; /// append for now file.open("/opt/gclc/gclc.log", std::ios_base::out | std::ios_base::app); if (file.is_open()) { file.write((char*) inputBuffer, newLength); file.close(); } else { std::cout<<"unable to open file D:"; } std::cout<<"|"; // receivedMessages } // Sleep(50); } /// Free up resources (if we ever exit...) freeaddrinfo(servInfo); return 0; }
DWORD SockReadAll(SOCKET socket,char* buf,long bufsiz,WORD timeout) { int curRead,totRead = 0; DWORD nonBlock = 0; WORD int_count = 0; char *tmpBuf,*bData; int dwResult; memset(buf,0x0,bufsiz); if(ioctlsocket(socket,FIONBIO,&nonBlock) ) { deb( "Error ioctl on socket.\n"); return ERR; } bData = (char*) a(1024); tmpBuf = (char*) a(INTERNAL_TEMP_BUF); if(tmpBuf == NULL || bData == NULL) { deb("malloc() error\n"); return ERR; } while(1) { dwResult = ReadyToRead(socket,timeout); if( dwResult == SUCCESS) { curRead = recv(socket,bData,1024,0); if(curRead == SOCKET_ERROR || curRead == 0) { f(bData); f(tmpBuf); if(int_count) { return SUCCESS; } else { return curRead; } } /*if(curRead <= 0 || curRead == SOCKET_ERROR) { break; }*/ if((totRead + curRead)> bufsiz) { break; } memcpy((void*) (tmpBuf + totRead), bData,curRead); totRead += curRead; int_count++; } else if(dwResult == SOCKET_ERROR) { f(bData); f(tmpBuf); if(int_count) { return totRead; } else { return curRead; } } } if(totRead > bufsiz) totRead = bufsiz; memcpy( buf, tmpBuf,bufsiz); f(bData); f(tmpBuf); return totRead; }