int RASocket::recv_line(std::string& out_line) { char buf[4096]; ACE_Data_Block db(sizeof (buf), ACE_Message_Block::MB_DATA, buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block(&db, ACE_Message_Block::DONT_DELETE, 0); if (recv_line(message_block) == -1) { sLog->outRemote("Recv error %s", ACE_OS::strerror(errno)); return -1; } out_line = message_block.rd_ptr(); return 0; }
int RASocket::recv_line(std::string& out_line) { char buf[4096]; ACE_Data_Block db(sizeof (buf), ACE_Message_Block::MB_DATA, buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block(&db, ACE_Message_Block::DONT_DELETE, 0); if (recv_line(message_block) == -1) { TC_LOG_DEBUG(LOG_FILTER_REMOTECOMMAND, "Recv error %s", ACE_OS::strerror(errno)); return -1; } out_line = message_block.rd_ptr(); return 0; }
bool BufferedSocket::send(const char* buf, size_t len) { if (buf == NULL || len == 0) return true; ACE_Data_Block db( len, ACE_Message_Block::MB_DATA, (const char*)buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block( &db, ACE_Message_Block::DONT_DELETE, 0); message_block.wr_ptr(len); if (this->msg_queue()->is_empty()) { // Try to send it directly. ssize_t n = this->noblk_send(message_block); if (n < 0) return false; else if (n == len) return true; // adjust how much bytes we sent message_block.rd_ptr((size_t)n); // fall down } // enqueue the message, note: clone is needed cause we cant enqueue stuff on the stack ACE_Message_Block* mb = message_block.clone(); if (this->msg_queue()->enqueue_tail(mb, (ACE_Time_Value*) &ACE_Time_Value::zero) == -1) { mb->release(); return false; } // tell reactor to call handle_output() when we can send more data if (this->reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK) == -1) return false; return true; }
bool RealmSocket::send(const char *buf, size_t len) { if (buf == NULL || len == 0) return true; ACE_Data_Block db( len, ACE_Message_Block::MB_DATA, (const char*)buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block( &db, ACE_Message_Block::DONT_DELETE, 0); message_block.wr_ptr(len); if (msg_queue()->is_empty()) { // Try to send it directly. ssize_t n = noblk_send(message_block); if (n < 0) return false; size_t un = size_t(n); if (un == len) return true; // fall down message_block.rd_ptr(un); } ACE_Message_Block *mb = message_block.clone(); if (msg_queue()->enqueue_tail(mb, (ACE_Time_Value *) &ACE_Time_Value::zero) == -1) { mb->release(); return false; } if (reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK) == -1) return false; return true; }
int RASocket::subnegotiate() { char buf[1024]; ACE_Data_Block db(sizeof (buf), ACE_Message_Block::MB_DATA, buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block(&db, ACE_Message_Block::DONT_DELETE, 0); const size_t recv_size = message_block.space(); // Wait a maximum of 1000ms for negotiation packet - not all telnet clients may send it ACE_Time_Value waitTime = ACE_Time_Value(1); const ssize_t n = peer().recv(message_block.wr_ptr(), recv_size, &waitTime); if (n <= 0) return int(n); if (n >= 1024) { sLog->outRemote("RASocket::subnegotiate: allocated buffer 1024 bytes was too small for negotiation packet, size: %u", n); return -1; } buf[n] = '\0'; #ifdef _DEBUG for (uint8 i = 0; i < n; ) { uint8 iac = buf[i]; if (iac == 0xFF) // "Interpret as Command" (IAC) { uint8 command = buf[++i]; std::stringstream ss; switch (command) { case 0xFB: // WILL ss << "WILL "; break; case 0xFC: // WON'T ss << "WON'T "; break; case 0xFD: // DO ss << "DO "; break; case 0xFE: // DON'T ss << "DON'T "; break; default: return -1; // not allowed } uint8 param = buf[++i]; ss << uint32(param); sLog->outRemote(ss.str().c_str()); } ++i; } #endif //! Just send back end of subnegotiation packet uint8 const reply[2] = {0xFF, 0xF0}; return peer().send(reply, 2); }
int WorldSocket::handle_input_missing_data(void) { char buf [4096]; ACE_Data_Block db(sizeof(buf), ACE_Message_Block::MB_DATA, buf, 0, 0, ACE_Message_Block::DONT_DELETE, 0); ACE_Message_Block message_block(&db, ACE_Message_Block::DONT_DELETE, 0); const size_t recv_size = message_block.space(); const ssize_t n = peer().recv(message_block.wr_ptr(), recv_size); if (n <= 0) { return (int)n; } message_block.wr_ptr(n); while (message_block.length() > 0) { if (m_Header.space() > 0) { // need to receive the header const size_t to_header = (message_block.length() > m_Header.space() ? m_Header.space() : message_block.length()); m_Header.copy(message_block.rd_ptr(), to_header); message_block.rd_ptr(to_header); if (m_Header.space() > 0) { // Couldn't receive the whole header this time. MANGOS_ASSERT(message_block.length() == 0); errno = EWOULDBLOCK; return -1; } // We just received nice new header if (handle_input_header() == -1) { MANGOS_ASSERT((errno != EWOULDBLOCK) && (errno != EAGAIN)); return -1; } } // Its possible on some error situations that this happens // for example on closing when epoll receives more chunked data and stuff // hope this is not hack ,as proper m_RecvWPct is asserted around if (!m_RecvWPct) { sLog.outError("Forcing close on input m_RecvWPct = NULL"); errno = EINVAL; return -1; } // We have full read header, now check the data payload if (m_RecvPct.space() > 0) { // need more data in the payload const size_t to_data = (message_block.length() > m_RecvPct.space() ? m_RecvPct.space() : message_block.length()); m_RecvPct.copy(message_block.rd_ptr(), to_data); message_block.rd_ptr(to_data); if (m_RecvPct.space() > 0) { // Couldn't receive the whole data this time. MANGOS_ASSERT(message_block.length() == 0); errno = EWOULDBLOCK; return -1; } } // just received fresh new payload if (handle_input_payload() == -1) { MANGOS_ASSERT((errno != EWOULDBLOCK) && (errno != EAGAIN)); return -1; } } return size_t(n) == recv_size ? 1 : 2; }
int TAO_DIOP_Transport::handle_input (TAO_Resume_Handle &rh, ACE_Time_Value *max_wait_time) { // If there are no messages then we can go ahead to read from the // handle for further reading.. // The buffer on the stack which will be used to hold the input // messages char buf [ACE_MAX_DGRAM_SIZE + ACE_CDR::MAX_ALIGNMENT]; #if defined (ACE_INITIALIZE_MEMORY_BEFORE_USE) (void) ACE_OS::memset (buf, '\0', sizeof buf); #endif /* ACE_INITIALIZE_MEMORY_BEFORE_USE */ // Create a data block ACE_Data_Block db (sizeof (buf), ACE_Message_Block::MB_DATA, buf, this->orb_core_->input_cdr_buffer_allocator (), this->orb_core_->locking_strategy (), ACE_Message_Block::DONT_DELETE, this->orb_core_->input_cdr_dblock_allocator ()); // Create a message block ACE_Message_Block message_block (&db, ACE_Message_Block::DONT_DELETE, this->orb_core_->input_cdr_msgblock_allocator ()); // Align the message block ACE_CDR::mb_align (&message_block); // Read the message into the message block that we have created on // the stack. ssize_t n = this->recv (message_block.rd_ptr (), message_block.space (), max_wait_time); // If there is an error return to the reactor.. if (n <= 0) { if (n == -1) { this->tms_->connection_closed (); } return n; } // Set the write pointer in the stack buffer message_block.wr_ptr (n); // Make a node of the message block.. TAO_Queued_Data qd (&message_block); size_t mesg_length = 0; // Parse the incoming message for validity. The check needs to be // performed by the messaging objects. if (this->messaging_object ()->parse_next_message (qd, mesg_length) == -1) return -1; if (qd.missing_data () == TAO_MISSING_DATA_UNDEFINED) { // parse/marshal error return -1; } if (message_block.length () > mesg_length) { // we read too much data return -1; } // NOTE: We are not performing any queueing nor any checking for // missing data. We are assuming that ALL the data would be got in a // single read. // Process the message return this->process_parsed_messages (&qd, rh); }