void TCPPort::Run() { char buffer[1024]; while (!CheckStopped()) { assert(listener_fd >= 0); if (connection_fd < 0) { /* accept new connection */ fd_set rfds; FD_ZERO(&rfds); FD_SET(listener_fd, &rfds); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 250000; int ret = select(listener_fd + 1, &rfds, NULL, NULL, &timeout); if (ret > 0) connection_fd = accept(listener_fd, NULL, NULL); else if (ret < 0) { close(listener_fd); listener_fd = -1; break; } } else { /* read from existing client connection */ fd_set rfds; FD_ZERO(&rfds); FD_SET(connection_fd, &rfds); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 250000; int ret = select(connection_fd + 1, &rfds, NULL, NULL, &timeout); if (ret > 0) { ssize_t nbytes = recv(connection_fd, buffer, sizeof(buffer), 0); if (nbytes <= 0) { close(connection_fd); connection_fd = -1; continue; } for (ssize_t i = 0; i < nbytes; ++i) ProcessChar(buffer[i]); } else if (ret < 0) { close(connection_fd); connection_fd = -1; } } } }
void SocketThread::Run() { while (!CheckStopped()) { assert(socket.IsDefined()); int ret = socket.WaitReadable(500); if ((ret > 0 && !handler.OnFileEvent(socket.Get(), 0)) || ret < 0) break; } }
void AndroidIOIOUartPort::Run() { SetRxTimeout(500); while (!CheckStopped()) { int ch = helper->read(Java::GetEnv(), UartID); if (ch >= 0) { char ch2 = ch; handler.DataReceived(&ch2, sizeof(ch2)); } } }
void AndroidBluetoothPort::Run() { assert(helper != NULL); SetRxTimeout(500); JNIEnv *const env = Java::GetEnv(); while (!CheckStopped()) { int ch = helper->read(env); if (ch >= 0) ProcessChar(ch); } }
void AndroidBluetoothPort::Run() { assert(helper != NULL); SetRxTimeout(500); JNIEnv *const env = Java::GetEnv(); while (!CheckStopped()) { int ch = helper->read(env); if (ch >= 0) { char ch2 = ch; handler.DataReceived(&ch2, sizeof(ch2)); } } }
void SocketPort::Run() { char buffer[1024]; while (!CheckStopped()) { assert(socket.IsDefined()); int ret = socket.WaitReadable(250); if (ret > 0) { ssize_t nbytes = socket.Read(buffer, sizeof(buffer)); if (nbytes <= 0) { break; } handler.DataReceived(buffer, nbytes); } else if (ret < 0) { break; } } }
void SerialPort::Run() { assert(Thread::IsInside()); DWORD dwBytesTransferred; BYTE inbuf[1024]; // JMW added purging of port on open to prevent overflow Flush(); #ifndef _WIN32_WCE OverlappedEvent osStatus, osReader; if (!osStatus.Defined() || !osReader.Defined()) // error creating event; abort return; #endif // Specify a set of events to be monitored for the port. if (is_widcomm) SetRxTimeout(180); else { ::SetCommMask(hPort, EV_RXCHAR); SetRxTimeout(0); } while (!CheckStopped()) { #ifndef _WIN32_WCE WaitResult result = WaitDataPending(osStatus, INFINITE); switch (result) { case WaitResult::READY: break; case WaitResult::TIMEOUT: continue; case WaitResult::FAILED: case WaitResult::CANCELLED: ::Sleep(100); continue; } int nbytes = GetDataPending(); if (nbytes <= 0) { ::Sleep(100); continue; } // Start reading data if ((size_t)nbytes > sizeof(inbuf)) nbytes = sizeof(inbuf); if (!::ReadFile(hPort, inbuf, nbytes, &dwBytesTransferred, osReader.GetPointer())) { if (::GetLastError() != ERROR_IO_PENDING) { // Error in ReadFile() occured ::Sleep(100); continue; } if (osReader.Wait() != OverlappedEvent::FINISHED) { ::CancelIo(hPort); ::SetCommMask(hPort, 0); osReader.Wait(); continue; } if (!::GetOverlappedResult(hPort, osReader.GetPointer(), &dwBytesTransferred, FALSE)) continue; } #else if (is_widcomm) { /* WaitCommEvent() doesn't work with the Widcomm Bluetooth driver, it blocks for 11 seconds, regardless whether data is received. This workaround polls for input manually. Observed on an iPaq hx4700 with WM6. */ } else { // Wait for an event to occur for the port. DWORD dwCommModemStatus; if (!::WaitCommEvent(hPort, &dwCommModemStatus, 0)) { // error reading from port Sleep(100); continue; } if ((dwCommModemStatus & EV_RXCHAR) == 0) /* no data available */ continue; } // Read the data from the serial port. if (!ReadFile(hPort, inbuf, 1024, &dwBytesTransferred, NULL) || dwBytesTransferred == 0) { Sleep(100); continue; } #endif DataReceived(inbuf, dwBytesTransferred); } Flush(); }
void SerialPort::Run() { assert(Thread::IsInside()); DWORD dwBytesTransferred; BYTE inbuf[1024]; // JMW added purging of port on open to prevent overflow Flush(); OverlappedEvent osStatus, osReader; if (!osStatus.Defined() || !osReader.Defined()) // error creating event; abort return; // Specify a set of events to be monitored for the port. ::SetCommMask(hPort, EV_RXCHAR); SetRxTimeout(0); while (!CheckStopped()) { WaitResult result = WaitDataPending(osStatus, INFINITE); switch (result) { case WaitResult::READY: break; case WaitResult::TIMEOUT: continue; case WaitResult::FAILED: case WaitResult::CANCELLED: ::Sleep(100); continue; } int nbytes = GetDataPending(); if (nbytes <= 0) { ::Sleep(100); continue; } // Start reading data if ((size_t)nbytes > sizeof(inbuf)) nbytes = sizeof(inbuf); if (!::ReadFile(hPort, inbuf, nbytes, &dwBytesTransferred, osReader.GetPointer())) { if (::GetLastError() != ERROR_IO_PENDING) { // Error in ReadFile() occured ::Sleep(100); continue; } if (osReader.Wait() != OverlappedEvent::FINISHED) { ::CancelIo(hPort); ::SetCommMask(hPort, 0); osReader.Wait(); continue; } if (!::GetOverlappedResult(hPort, osReader.GetPointer(), &dwBytesTransferred, FALSE)) continue; } DataReceived(inbuf, dwBytesTransferred); } Flush(); }