distributed_control::~distributed_control() { // detach the instance last_dc = NULL; last_dc_procid = 0; distributed_services->full_barrier(); logstream(LOG_INFO) << "Shutting down distributed control " << std::endl; FREE_CALLBACK_EVENT(EVENT_NETWORK_BYTES); FREE_CALLBACK_EVENT(EVENT_RPC_CALLS); // call all deletion callbacks for (size_t i = 0; i < deletion_callbacks.size(); ++i) { deletion_callbacks[i](); } size_t bytessent = bytes_sent(); for (size_t i = 0;i < senders.size(); ++i) { senders[i]->flush(); } comm->close(); for (size_t i = 0;i < senders.size(); ++i) { delete senders[i]; } senders.clear(); pthread_key_delete(dc_impl::thrlocal_sequentialization_key); pthread_key_delete(dc_impl::thrlocal_send_buffer_key); size_t bytesreceived = bytes_received(); for (size_t i = 0;i < receivers.size(); ++i) { receivers[i]->shutdown(); delete receivers[i]; } receivers.clear(); // shutdown function call handlers for (size_t i = 0;i < fcallqueue.size(); ++i) fcallqueue[i].stop_blocking(); fcallhandlers.join(); logstream(LOG_INFO) << "Bytes Sent: " << bytessent << std::endl; logstream(LOG_INFO) << "Calls Sent: " << calls_sent() << std::endl; logstream(LOG_INFO) << "Network Sent: " << network_bytes_sent() << std::endl; logstream(LOG_INFO) << "Bytes Received: " << bytesreceived << std::endl; logstream(LOG_INFO) << "Calls Received: " << calls_received() << std::endl; delete comm; }
vpr::Uint32 SocketDatagramImplBOOST::sendto(const void* msg, const vpr::Uint32 length, const vpr::InetAddr& to, const vpr::Interval& timeout) { boost::system::error_code ec; vpr::Uint32 bytes_sent(0); bytes_sent = mUdpSocket->send_to(boost::asio::buffer(msg, length), to.mUdpAddr, 0, ec); if (ec) { //TODO handle errors } return bytes_sent; }
distributed_control::~distributed_control() { distributed_services->full_barrier(); if(last_dc_procid==0) logstream(LOG_INFO) << "Shutting down distributed control " << std::endl; FREE_CALLBACK_EVENT(EVENT_NETWORK_BYTES); FREE_CALLBACK_EVENT(EVENT_RPC_CALLS); // call all deletion callbacks for (size_t i = 0; i < deletion_callbacks.size(); ++i) { deletion_callbacks[i](); } size_t bytessent = bytes_sent(); for (size_t i = 0;i < senders.size(); ++i) { senders[i]->flush(); } comm->close(); for (size_t i = 0;i < senders.size(); ++i) { delete senders[i]; } size_t bytesreceived = bytes_received(); for (size_t i = 0;i < receivers.size(); ++i) { receivers[i]->shutdown(); delete receivers[i]; } senders.clear(); receivers.clear(); // shutdown function call handlers for (size_t i = 0;i < fcallqueue.size(); ++i) fcallqueue[i].stop_blocking(); fcallhandlers.join(); if(last_dc_procid==0){ logstream(LOG_INFO) << "Bytes Sent: " << bytessent << std::endl; logstream(LOG_INFO) << "Calls Sent: " << calls_sent() << std::endl; logstream(LOG_INFO) << "Network Sent: " << network_bytes_sent() << std::endl; logstream(LOG_INFO) << "Bytes Received: " << bytesreceived << std::endl; logstream(LOG_INFO) << "Calls Received: " << calls_received() << std::endl; } delete comm; }
vpr::Uint32 SocketDatagramImplBSD::sendto(const void* msg, const vpr::Uint32 length, const vpr::InetAddr& to, const vpr::Interval& timeout) { vpr::Uint32 bytes_sent(0); // If not writable within timeout interval throw exception. if ( ! mHandle->isWriteable(timeout) ) { std::ostringstream msg_stream; msg_stream << "Timeout occured while trying to write to " << mHandle->getName(); throw TimeoutException(msg_stream.str(), VPR_LOCATION); } ssize_t bytes; mBlockingFixed = true; bytes = ::sendto(mHandle->mFdesc, msg, length, 0, reinterpret_cast<const sockaddr*>(&to.mAddr), to.size()); if ( bytes == -1 ) { if ( errno == EAGAIN && ! isBlocking() ) { throw WouldBlockException("Would block while reading.", VPR_LOCATION); } else { std::stringstream ss; ss << "[vpr::SocketDatagramImplBSD::sendto()] ERROR: Could not send " << " to " << to << " on socket (" << mRemoteAddr << "): " << strerror(errno); throw SocketException(ss.str(), VPR_LOCATION); } } if ( ECONNRESET == errno) { throw ConnectionResetException("Connection reset.", VPR_LOCATION); } else if (EHOSTUNREACH == errno) { throw NoRouteToHostException("No route to host.", VPR_LOCATION); } else if (EHOSTDOWN == errno) { throw UnknownHostException("Unknown host.", VPR_LOCATION); } else if (ENETDOWN == errno) { throw IOException("Network is down.", VPR_LOCATION); } else { bytes_sent = bytes; } return bytes_sent; }
int32 DCCSend::Transfer(void* arg) { BMessenger msgr(reinterpret_cast<DCCSend*>(arg)); BMessage reply, ipdata; BLooper* looper(NULL); if (msgr.IsValid()) msgr.SendMessage(M_DCC_GET_CONNECT_DATA, &reply); BMessenger callmsgr; reply.FindMessenger("caller", &callmsgr); callmsgr.SendMessage(M_GET_IP, &ipdata); BPath path(reply.FindString("name")); BString fileName, status; struct sockaddr_in address; struct in_addr sendaddr; memset(&sendaddr, 0, sizeof(struct in_addr)); int sd, dccSock(-1); fileName.Append(path.Leaf()); fileName.ReplaceAll(" ", "_"); if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR); return 0; } memset(&address, 0, sizeof(struct sockaddr_in)); address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(atoi(reply.FindString("port"))); int sin_size; sin_size = (sizeof(struct sockaddr_in)); UpdateStatus(msgr, S_DCC_LOCK_ACQUIRE B_UTF8_ELLIPSIS); vision_app->AcquireDCCLock(); if (!msgr.IsValid() || bind(sd, (sockaddr*)&address, sin_size) < 0) { UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR); vision_app->ReleaseDCCLock(); close(sd); return 0; } UpdateStatus(msgr, S_DCC_ACK_WAIT); sendaddr.s_addr = inet_addr(ipdata.FindString("ip")); if (msgr.IsValid()) { status = "PRIVMSG "; status << reply.FindString("nick") << " :\1DCC SEND " << fileName << " " << htonl(sendaddr.s_addr) << " " << reply.FindString("port") << " " << reply.FindString("size") << "\1"; BMessage msg(M_SERVER_SEND); msg.AddString("data", status.String()); if (callmsgr.IsValid()) callmsgr.SendMessage(&msg); UpdateStatus(msgr, S_DCC_LISTEN_CALL); if (listen(sd, 1) < 0) { UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR); vision_app->ReleaseDCCLock(); close(sd); return 0; } } struct timeval t; t.tv_sec = 2; t.tv_usec = 0; uint32 try_count(0); while (msgr.Target(&looper) != NULL) { fd_set rset; FD_ZERO(&rset); FD_SET(sd, &rset); if (select(sd + 1, &rset, 0, 0, &t) < 0) { UpdateStatus(msgr, S_DCC_ESTABLISH_ERROR); vision_app->ReleaseDCCLock(); close(sd); return 0; } if (FD_ISSET(sd, &rset)) { dccSock = accept(sd, (sockaddr*)&address, (socklen_t*)&sin_size); UpdateStatus(msgr, S_DCC_ESTABLISH_SUCCEEDED); break; } ++try_count; status = S_DCC_WAIT_FOR_CONNECTION; status << try_count << "."; UpdateStatus(msgr, status.String()); } vision_app->ReleaseDCCLock(); char set[4]; memset(set, 1, sizeof(set)); close(sd); BFile file; file.SetTo(reply.FindString("name"), B_READ_ONLY); int32 bytes_sent(0L), seekpos(0L); BMessage resumeData; msgr.SendMessage(M_DCC_GET_RESUME_POS, &resumeData); if (resumeData.HasInt32("pos")) { resumeData.FindInt32("pos", &seekpos); file.Seek(seekpos, SEEK_SET); UpdateBar(msgr, seekpos, 0, 0, true); bytes_sent = seekpos; } status = S_DCC_SEND1; status << path.Leaf() << S_DCC_SEND2 << reply.FindString("nick") << "."; UpdateStatus(msgr, status.String()); int cps(0); if (file.InitCheck() == B_NO_ERROR) { bigtime_t last(system_time()), now; const uint32 DCC_BLOCK_SIZE(atoi(vision_app->GetString("dccBlockSize"))); #ifdef __INTEL__ char buffer[DCC_BLOCK_SIZE]; #else char* buffer = new char[DCC_BLOCK_SIZE]; #endif int period(0); ssize_t count(0); bigtime_t start = system_time(); while ((msgr.Target(&looper) != NULL) && (count = file.Read(buffer, DCC_BLOCK_SIZE - 1)) > 0) { int sent; if ((sent = send(dccSock, buffer, count, 0)) < count) { UpdateStatus(msgr, S_DCC_WRITE_ERROR); break; } uint32 confirm(0), newSize(bytes_sent + count); fd_set rset, eset; FD_ZERO(&rset); FD_ZERO(&eset); FD_SET(dccSock, &rset); t.tv_sec = 0; t.tv_usec = 10; while ((confirm < newSize) && (recv(dccSock, &confirm, sizeof(confirm), 0) > 0)) { confirm = ntohl(confirm); bytes_sent = confirm; } BMessage msg(M_DCC_UPDATE_TRANSFERRED); msg.AddInt32("transferred", bytes_sent); msgr.SendMessage(&msg); now = system_time(); period += sent; bool hit(false); if (now - last > 500000) { cps = (int)ceil((bytes_sent - seekpos) / ((now - start) / 1000000.0)); BMessage updmsg(M_DCC_UPDATE_AVERAGE); updmsg.AddInt32("average", cps); msgr.SendMessage(&updmsg); last = now; period = 0; hit = true; } UpdateBar(msgr, sent, cps, bytes_sent, hit); } #ifndef __INTEL__ delete[] buffer; #endif } if (msgr.IsValid()) { BMessage msg(M_DCC_STOP_BUTTON); msgr.SendMessage(&msg); } if (dccSock > 0) { close(dccSock); } if (file.InitCheck() == B_OK) file.Unset(); return 0; }