int clSocketBase::SelectReadMS(long milliSeconds) throw(clSocketException) { if(milliSeconds == -1) { return kSuccess; } if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } int seconds = milliSeconds / 1000; // convert the number into seconds int ms = milliSeconds % 1000; // the remainder is less than a second struct timeval tv = { seconds, ms * 1000 }; fd_set readfds; FD_ZERO(&readfds); FD_SET(m_socket, &readfds); int rc = select(m_socket + 1, &readfds, NULL, NULL, &tv); if(rc == 0) { // timeout return kTimeout; } else if(rc < 0) { // an error occurred throw clSocketException("SelectRead failed: " + error()); } else { // we got something to read return kSuccess; } }
int clSocketBase::SelectWriteMS(long milliSeconds) { if(milliSeconds == -1) { return kSuccess; } if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } struct timeval tv; tv.tv_sec = milliSeconds / 1000; tv.tv_usec = (milliSeconds % 1000) * 1000; fd_set write_set; FD_ZERO(&write_set); FD_SET(m_socket, &write_set); errno = 0; int rc = select(m_socket + 1, NULL, &write_set, NULL, &tv); if(rc == 0) { // timeout return kTimeout; } else if(rc < 0) { // an error occurred throw clSocketException("SelectWriteMS failed: " + error()); } else { // we got something to read return kSuccess; } }
int clSocketBase::SelectWrite(long seconds) throw(clSocketException) { if(seconds == -1) { return kSuccess; } if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } struct timeval tv = { seconds, 0 }; fd_set write_set; FD_ZERO(&write_set); FD_SET(m_socket, &write_set); errno = 0; int rc = select(m_socket + 1, NULL, &write_set, NULL, &tv); if(rc == 0) { // timeout return kTimeout; } else if(rc < 0) { // an error occurred throw clSocketException("SelectRead failed: " + error()); } else { // we got something to read return kSuccess; } }
int clSocketBase::ReadMessage(wxString& message, int timeout) throw (clSocketException) { size_t message_len(0); size_t bytesRead(0); int rc = Read( (char*)&message_len, sizeof(message_len), bytesRead, timeout); if ( rc != kSuccess ) { // timeout return rc; } bytesRead = 0; char *buff = new char[message_len+1]; memset(buff, 0, message_len+1); rc = Read(buff, message_len, bytesRead, timeout); if ( rc != kSuccess ) { wxDELETEA( buff ); return rc; } if ( bytesRead == 0 ) { // session was closed wxDELETEA( buff ); throw clSocketException("connection closed by peer"); } else if ( bytesRead != message_len ) { wxDELETEA( buff ); throw clSocketException("Wrong message length received"); } buff[message_len] = '\0'; message = buff; return kSuccess; }
void clSocketBase::Send(const wxMemoryBuffer& msg) throw(clSocketException) { if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } char* pdata = (char*)msg.GetData(); int bytesLeft = msg.GetDataLen(); while(bytesLeft) { if(SelectWriteMS(1000) == kTimeout) continue; int bytesSent = ::send(m_socket, (const char*)pdata, bytesLeft, 0); if(bytesSent <= 0) throw clSocketException("Send error: " + error()); pdata += bytesSent; bytesLeft -= bytesSent; } }
void CodeLiteLLDBApp::AcceptNewConnection() throw(clSocketException) { m_replySocket.reset(NULL); wxPrintf("codelite-lldb: waiting for new connection\n"); try { while(true) { m_replySocket = m_acceptSocket.WaitForNewConnection(1); if(m_replySocket) { break; } } // Remote connection, send the 'handshake' packet if(m_port != wxNOT_FOUND) { wxPrintf("codelite-lldb: sending handshake packet\n"); LLDBRemoteHandshakePacket handshake; handshake.SetHost(::wxGetHostName()); m_replySocket->WriteMessage(handshake.ToJSON().format()); } // handle the connection to the thread m_networkThread = new LLDBNetworkServerThread(this, m_replySocket->GetSocket()); m_networkThread->Start(); } catch(clSocketException& e) { wxPrintf("codelite-lldb: an error occurred while waiting for connection. %s\n", e.what().c_str()); Cleanup(); // exit throw clSocketException("Failed to accept new connection"); } }
// Read API int clSocketBase::Read(wxMemoryBuffer& content, long timeout) throw(clSocketException) { content.Clear(); char buffer[4096]; timeout = (timeout * 1000); // convert to MS while(true && timeout) { int rc = SelectReadMS(10); timeout -= 10; if(rc == kSuccess) { memset(buffer, 0x0, sizeof(buffer)); int bytesRead = recv(m_socket, buffer, sizeof(buffer), 0); if(bytesRead < 0) { // Error throw clSocketException("Read failed: " + error()); } else if(bytesRead == 0) { // connection closed return kError; } else { content.AppendData(buffer, bytesRead); continue; } } else { if(content.IsEmpty()) continue; // keep waiting until time ends else return kSuccess; // we already read our content } } return kTimeout; }
void clSocketBase::Send(const std::string& msg) throw (clSocketException) { if ( m_socket == INVALID_SOCKET ) { throw clSocketException("Invalid socket!"); } ::send(m_socket, msg.c_str(), msg.length(), 0); }
void clSocketBase::Send(const std::string& msg) throw(clSocketException) { if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } wxMemoryBuffer mb; mb.AppendData(msg.c_str(), msg.length()); Send(mb); }
// Send API void clSocketBase::Send(const wxString& msg, const wxMBConv& conv) throw(clSocketException) { if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } wxCharBuffer cb = msg.mb_str(conv).data(); wxMemoryBuffer mb; mb.AppendData(cb.data(), cb.length()); Send(mb); }
void clSocketBase::WriteMessage(const wxString& message) { if(m_socket == INVALID_SOCKET) { throw clSocketException("Invalid socket!"); } // Write the message length std::string c_str = message.mb_str(wxConvUTF8).data(); int len = c_str.length(); // send the length in string form to avoid binary / arch differences between remote and local machine char msglen[11]; memset(msglen, 0, sizeof(msglen)); sprintf(msglen, "%010d", len); // send it without the NULL byte if(::write(m_socket, msglen, sizeof(msglen) - 1) < 0) { throw clSocketException("Send error: " + error(errno)); } // now send the actual data Send(c_str); }
int clSocketBase::Read(char* buffer, size_t bufferSize, size_t& bytesRead, long timeout) { if(SelectRead(timeout) == kTimeout) { return kTimeout; } memset(buffer, 0, bufferSize); const int res = recv(m_socket, buffer, bufferSize, 0); if(res < 0) { const int err = GetLastError(); if(eWouldBlock == err) { return kTimeout; } throw clSocketException("Read failed: " + error(err)); } else if(0 == res) { throw clSocketException("Read failed: " + error()); } bytesRead = static_cast<size_t>(res); return kSuccess; }
void clSocketBase::WriteMessage(const wxString& message) throw (clSocketException) { if ( m_socket == INVALID_SOCKET ) { throw clSocketException("Invalid socket!"); } // Write the message length std::string c_str = message.mb_str(wxConvUTF8).data(); size_t len = c_str.length(); ::send(m_socket, (const char*)&len, sizeof(len), 0); // now send the actual data Send(c_str); }
int clSocketBase::ReadMessage(wxString& message, int timeout) throw(clSocketException) { // send the length in string form to avoid binary / arch differences between remote and local machine char msglen[11]; memset(msglen, 0, sizeof(msglen)); size_t message_len(0); size_t bytesRead(0); int rc = Read((char*)msglen, sizeof(msglen) - 1, bytesRead, timeout); if(rc != kSuccess) { // timeout return rc; } // convert the string to int message_len = ::atoi(msglen); bytesRead = 0; char* buff = new char[message_len + 1]; memset(buff, 0, message_len + 1); // read the entire amount we need int bytesLeft = message_len; int totalRead = 0; while(bytesLeft > 0) { rc = Read(buff + totalRead, bytesLeft, bytesRead, timeout); if(rc != kSuccess) { wxDELETEA(buff); return rc; } else if(rc == 0) { // session was closed wxDELETEA(buff); throw clSocketException("connection closed by peer"); } else { bytesLeft -= bytesRead; totalRead += bytesRead; bytesRead = 0; } } buff[message_len] = '\0'; message = buff; return kSuccess; }