/// /// pretend a connection. int YARPOutputSocketDgram::Connect (const YARPUniqueNameID& name, const YARPString& own_name) { ACE_UNUSED_ARG (name); OSDataDgram& d = OSDATA(system_resources); YARP_DBG(THIS_DBG) ((LM_DEBUG, "Pretending a connection to port %d on %s\n", d._remote_addr.get_port_number(), d._remote_addr.get_host_addr())); ACE_Time_Value timeout (YARP_SOCK_TIMEOUT, 0); ACE_SOCK_Stream stream; int r = d._service_socket.connect (stream, d._remote_addr, &timeout); if (r < 0) { ACE_DEBUG ((LM_ERROR, "cannot connect to remote peer %s:%d\n", d._remote_addr.get_host_addr(), d._remote_addr.get_port_number())); return YARP_FAIL; } /// send the header. int port_number = 0; MyMessageHeader hdr; hdr.SetGood (); hdr.SetLength (YARP_MAGIC_NUMBER + 128*name.getRequireAck()); stream.send_n (&hdr, sizeof(hdr), 0); /// fine, now send the name of the connection. NetInt32 name_len = own_name.length(); stream.send_n (&name_len, sizeof(NetInt32), 0); stream.send_n (own_name.c_str(), name_len, 0); /// wait response. hdr.SetBad (); r = stream.recv (&hdr, sizeof(hdr), 0, &timeout); if (r < 0) { stream.close (); ACE_DEBUG ((LM_ERROR, "cannot handshake with remote %s:%d\n", d._remote_addr.get_host_addr(), d._remote_addr.get_port_number())); return YARP_FAIL; } /// stores the remote acceptor address for future use (e.g. closing the connection). d._remote_acceptor_store = d._remote_addr; port_number = hdr.GetLength(); if (port_number == -1) { /// there might be a real -1 port number -> 65535. stream.close (); ACE_DEBUG ((LM_ERROR, "*** error, got garbage back from remote %s:%d\n", d._remote_addr.get_host_addr(), d._remote_addr.get_port_number())); return YARP_FAIL; } /// the connect changes the remote port number to the actual assigned channel. d._remote_addr.set_port_number (port_number); stream.close (); return YARP_OK; }
void JAWS_Synch_IO::transmit_file (const char *filename, const char *header, int header_size, const char *trailer, int trailer_size) { ACE_Filecache_Handle handle (ACE_TEXT_CHAR_TO_TCHAR (filename)); int result = handle.error (); if (result == ACE_Filecache_Handle::ACE_SUCCESS) { #if defined (ACE_JAWS_BASELINE) || defined (ACE_WIN32) ACE_SOCK_Stream stream; stream.set_handle (this->handle_); if ((stream.send_n (header, header_size) == header_size) && (stream.send_n (handle.address (), handle.size ()) == handle.size ()) && (stream.send_n (trailer, trailer_size) == trailer_size)) this->handler_->transmit_file_complete (); else result = -1; #else // Attempting to use writev // Is this faster? iovec iov[3]; int iovcnt = 0; if (header_size > 0) { iov[iovcnt].iov_base = const_cast<char*> (header); iov[iovcnt].iov_len = header_size; iovcnt++; } if (handle.size () > 0) { iov[iovcnt].iov_base = reinterpret_cast<char*> (handle.address ()); iov[iovcnt].iov_len = handle.size (); iovcnt++; } if (trailer_size > 0) { iov[iovcnt].iov_base = const_cast<char*> (trailer); iov[iovcnt].iov_len = trailer_size; iovcnt++; } if (ACE_OS::writev (this->handle_, iov, iovcnt) < 0) result = -1; else this->handler_->transmit_file_complete (); #endif /* ACE_JAWS_BASELINE */ } if (result != ACE_Filecache_Handle::ACE_SUCCESS) this->handler_->transmit_file_error (result); }
void JAWS_Synch_IO::transmit_file (JAWS_IO_Handler *ioh, ACE_HANDLE handle, const char *header, unsigned int header_size, const char *trailer, unsigned int trailer_size) { int result = 0; if (handle != ACE_INVALID_HANDLE) { ACE_SOCK_Stream stream; stream.set_handle (ioh->handle ()); if ((unsigned long) stream.send_n (header, header_size) < header_size) { result = -1; } else { int count; char buf[BUFSIZ]; do { count = ACE_OS::read (handle, buf, sizeof (buf)); if (count <= 0) break; if (stream.send_n (buf, count) < count) { result = -1; } } while (result == 0); if ((unsigned long) stream.send_n (trailer, trailer_size) < trailer_size) { result = -1; } } } if (result == 0) ioh->transmit_file_complete (); else ioh->transmit_file_error (result); }
int FT_EventService::report_factory(CORBA::ORB_ptr orb, FtRtecEventChannelAdmin::EventChannel_ptr ec) { try{ char* addr = ACE_OS::getenv("EventChannelFactoryAddr"); if (addr != 0) { // instaniated by object factory, report my ior back to the factory ACE_INET_Addr factory_addr(addr); ACE_SOCK_Connector connector; ACE_SOCK_Stream stream; ORBSVCS_DEBUG((LM_DEBUG,"connecting to %s\n",addr)); if (connector.connect(stream, factory_addr) == -1) ORBSVCS_ERROR_RETURN((LM_ERROR, "(%P|%t) Invalid Factory Address\n"), -1); ORBSVCS_DEBUG((LM_DEBUG,"Factory connected\n")); CORBA::String_var my_ior_string = orb->object_to_string(ec); int len = ACE_OS::strlen(my_ior_string.in()) ; if (stream.send_n(my_ior_string.in(), len) != len) ORBSVCS_ERROR_RETURN((LM_ERROR, "(%P|%t) IOR Transmission Error\n"), -1); stream.close(); } } catch (...){ return -1; } return 0; }
void JAWS_Synch_IO_No_Cache::send_message (const char *buffer, int length) { ACE_SOCK_Stream stream; stream.set_handle (this->handle_); stream.send_n (buffer, length); }
/////////////////////////////////////////////////////////////////////////// // <summary> // This method writes data from the given buffer to the underlying stream. // It can block or not, depending on the value of the blocking parameter. // </summary> // // <param name = "buffer"> // The buffer that contains the data to be written. // </param> // // <param name = "size"> // The size of the buffer in bytes of the buffer. // </param> // // <param name = "blocking"> // True if the write request should block; false otherwise. // </param> // // <param name = "bytesWritten"> // An out parameter that will contain the number of bytes that have been // written to the stream. // </param> // // <returns> // Returns a MgStreamStatus value indicating the status of the operation. // </returns> MgStreamHelper::MgStreamStatus MgAceStreamHelper::WriteData(void* buffer, size_t size, bool blocking, size_t* bytesWritten) { // Do not attempt writing zero byte to the socket as this could be problematic. if (0 == size) { return MgStreamHelper::mssDone; } ACE_ASSERT( buffer && size > 0 ); MgStreamHelper::MgStreamStatus stat = MgStreamHelper::mssError; // check parameters if ( buffer && size > 0 ) { // init out parameter if ( bytesWritten != NULL ) *bytesWritten = 0; ACE_SOCK_Stream stream; stream.set_handle( m_handle ); ssize_t res = 0; // On Linux, use MSG_NOSIGNAL to request not to send SIGPIPE on // errors on stream oriented sockets when the other end breaks // the connection. The EPIPE error is still returned. // Note that neither trapping the SIGPIPE signal via an // ACE_Event_Handler nor calling ACE_OS::signal(SIGPIPE, SIG_IGN) // seems to work. if ( blocking ) { res = stream.send_n(buffer, size, MG_MSG_NOSIGNAL); } else { res = stream.send(buffer, size, MG_MSG_NOSIGNAL); } // check for failure if ( res >= 0 ) { // update out parameter if ( bytesWritten != NULL ) *bytesWritten = res; if ( res == (ssize_t)size ) { stat = MgStreamHelper::mssDone; } else { stat = blocking ? MgStreamHelper::mssError : MgStreamHelper::mssNotDone; } } } return stat; }
int main(int argc, char* argv[]){ if (argc > 1){ return 1; } cout << argv[0] << endl; ACE_INET_Addr addr(1234, ACE_LOCALHOST); ACE_SOCK_Stream stream; ACE_SOCK_Acceptor acceptor; int success = acceptor.open(addr, 1); ACE_TCHAR addrStr[20]; if (success > 0) { addr.addr_to_string(addrStr, 20); } //* success = acceptor.accept(stream); if (success < 0) { cout << "Cannot accept" << endl; return 1; } //*/ char buf[BUFSIZ]; int n; char *msg = "You are connected"; stream.send_n(msg, strlen(msg)); stream.close(); /* while (stream.recv(buf, BUFSIZ)) { // _write(1, buf, n); cout << buf << endl; } //*/ cout << endl << "Done" << endl; return 0; }
void JAWS_Synch_IO::send_message (JAWS_IO_Handler *ioh, const char *buffer, unsigned int length) { ACE_SOCK_Stream stream; stream.set_handle (ioh->handle ()); stream.send_n (buffer, length); }
// thread function that serves the client for the UnMarshalled Octet // test static ACE_THR_FUNC_RETURN unmarshalledOctetServer (void *arg){ // unbundle the arguments ArgStruct * args = reinterpret_cast<ArgStruct *> (arg); ACE_SOCK_Stream * dataModeStream = args->stream; ACE_CDR::ULong numIterations = args->numIters; delete args; // serve the client for numIterations synchronous invocations do { // READ A MESSAGE FROM THE CLIENT size_t bt; ACE_CDR::ULong msgBufSize=0; // read the size of the buffer to follow if ((dataModeStream->recv_n(&msgBufSize, ACE_CDR::LONG_SIZE, 0, &bt)) == -1) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv_n")), 0); msgBufSize = ACE_NTOHL(msgBufSize); // allocate the buffer for the message payload ACE_CDR::Octet * msgBuf = 0; ACE_NEW_RETURN(msgBuf, ACE_CDR::Octet[msgBufSize], 0); // read the buffer if ((dataModeStream->recv_n(msgBuf, msgBufSize, 0, &bt)) == -1) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv_n")), 0); // clean up the allocated buffer delete[] msgBuf; // SEND A REPLY TO THE CLIENT // send back a 2 byte reply ACE_CDR::Short reply; if ((dataModeStream->send_n(&reply, ACE_CDR::SHORT_SIZE, 0, &bt)) == -1) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("send_n")), 0); } while (--numIterations); // close and destroy the stream dataModeStream->close(); delete dataModeStream; return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { ACE_LOG_MSG->open (argv[0]); parse_args (argc, argv); // Default is to ask the server for ``help.'' static char buf[BUFSIZ] = "help\n"; int n; ACE_SOCK_Stream sc; ACE_SOCK_Connector con; if (con.connect (sc, ACE_INET_Addr (port_number, host_name)) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n%a", "connect", 1), -1); if (remote_reconfigure) // Remotely instruct the server to reconfigure itself. ACE_OS::strcpy (buf, "reconfigure\n"); // Send the command. if (sc.send_n (buf, ACE_OS::strlen (buf) + 1) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n%a", "send", 1), -1); // Next, read the response. while ((n = sc.recv (buf, sizeof buf)) > 0) if (ACE_OS::write (ACE_STDOUT, buf, n) != n) ACE_ERROR_RETURN ((LM_ERROR, "%p\n%a", "write", 1), -1); if (sc.close () == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n%a", "close", 1), -1); return 0; }
static void* send_data(void *) { while(1) { std::cout<<">>Hello World"<<std::endl; Peer->send_n("Hello World", sizeof("Hello World")); ACE_OS::sleep(1); } return 0; }
int YARPOutputSocketDgram::Close (const YARPUniqueNameID& name) { ACE_Time_Value timeout (YARP_SOCK_TIMEOUT, 0); ACE_UNUSED_ARG (name); OSDataDgram& d = OSDATA(system_resources); YARP_DBG(THIS_DBG) ((LM_DEBUG, "Pretending to close a connection to port %d on %s\n", d._remote_addr.get_port_number(), d._remote_addr.get_host_addr())); /// send the header. MyMessageHeader hdr; hdr.SetGood (); hdr.SetLength (YARP_MAGIC_NUMBER + 1); ACE_SOCK_Stream stream; int r = d._service_socket.connect (stream, d._remote_addr, &timeout); if (r < 0) { ACE_DEBUG ((LM_DEBUG, "cannot connect to remote peer %s:%d\n", d._remote_addr.get_host_addr(), d._remote_addr.get_port_number())); ACE_DEBUG ((LM_DEBUG, "close will complete anyway\n")); d._connector_socket.close (); identifier = ACE_INVALID_HANDLE; return YARP_FAIL; } r = stream.send_n (&hdr, sizeof(hdr), 0); /// wait response. /// need a timeout here! hdr.SetBad (); r = stream.recv (&hdr, sizeof(hdr), 0, &timeout); if (r < 0) { stream.close (); d._connector_socket.close (); identifier = ACE_INVALID_HANDLE; ACE_DEBUG ((LM_DEBUG, "cannot handshake with remote %s:%d\n", d._remote_addr.get_host_addr(), d._remote_addr.get_port_number())); return YARP_FAIL; } hdr.GetLength(); d._remote_addr.set ((u_short)0); d._remote_acceptor_store.set ((u_short)0); stream.close (); d._connector_socket.close (); identifier = ACE_INVALID_HANDLE; return YARP_OK; }
int Scavenger_Task::svc(void) { this->the_barrier_->wait (); ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting scavenger thread\n")); ACE_SOCK_Stream stream; { ACE_INET_Addr remote_sap (this->endpoint_); ACE_SOCK_Connector connector; if (connector.connect(stream, remote_sap) == -1) { ACE_ERROR((LM_ERROR, "Cannot connect to <%s>\n", endpoint_)); return -1; } } for (;;) { ACE_Time_Value period (0, this->period_in_usecs_); ACE_OS::sleep (period); { ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1); if (this->stopped_) break; } ACE_hrtime_t start = ACE_OS::gethrtime (); ssize_t n = stream.send_n(&start, sizeof(start)); if (n == 0 || n == -1) break; ACE_hrtime_t end; n = stream.recv(&end, sizeof(end)); if (n == 0 || n == -1) break; if (start != end) { ACE_ERROR((LM_ERROR, "Mismatched response from <%s>\n", endpoint_)); break; } } stream.close(); ACE_DEBUG ((LM_DEBUG, "(%P|%t) Finishing scavenger thread\n")); return 0; }
static ACE_THR_FUNC_RETURN worker (void *) { ACE_OS::sleep (3); const ACE_TCHAR *msg = ACE_TEXT ("Message from Connection worker"); ACE_TCHAR buf [BUFSIZ]; buf[0] = static_cast<ACE_TCHAR> ((ACE_OS::strlen (msg) + 1)); ACE_OS::strcpy (&buf[1], msg); ACE_INET_Addr addr (rendezvous); ACE_DEBUG((LM_DEBUG, "(%t) Spawning %d client threads...\n", cli_thrno)); int grp = ACE_Thread_Manager::instance ()->spawn_n (cli_thrno, &cli_worker, buf); ACE_TEST_ASSERT (grp != -1); ACE_Thread_Manager::instance ()->wait_grp (grp); ACE_DEBUG ((LM_DEBUG, "(%t) Client threads done; shutting down...\n")); ACE_SOCK_Stream stream; ACE_SOCK_Connector connect; if (connect.connect (stream, addr) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p Error while connecting\n"), ACE_TEXT ("connect"))); const ACE_TCHAR *sbuf = ACE_TEXT ("\011shutdown"); ACE_DEBUG ((LM_DEBUG, "shutdown stream handle = %x\n", stream.get_handle ())); if (stream.send_n (sbuf, (ACE_OS::strlen (sbuf) + 1) * sizeof (ACE_TCHAR)) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("send_n"))); ACE_DEBUG ((LM_DEBUG, "Sent message of length = %d\n", ACE_OS::strlen (sbuf))); stream.close (); return 0; }
int Sender::sendMessage(const std::string& str, const ACE_SOCK_Stream& stream, bool quiet) { ssize_t res = stream.send_n(str.c_str(), (int)str.size(), &timeout); if(res != (ssize_t)str.size()) { if(!quiet) cerr << "Sender::sendMessage(): sending timeout!"<<endl; return E_SEND; } res = stream.recv(response, 1, &timeout); if(res <= 0) { if(!quiet) cerr << "Sender::sendMessage(): response timeout!" << endl; return E_RESPONSE; } return SUCCESS; }
// send the test header (only contains number of iterations) int sendHeader(ACE_SOCK_Stream & stream) { // create an ACE CDR output stream and place the header information // into it ACE_OutputCDR hdrCDR; hdrCDR << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER); hdrCDR << ACE_CDR::ULong(Options_Manager::test_iterations+primerIterations); if (!hdrCDR.good_bit()) return (0); // send the header to the server (HEADER IS 8 BYTES LONG) size_t bt; if (stream.send_n(hdrCDR.begin(), 0, &bt) == -1) return 0; return 1; }
int ACE_Remote_Token_Proxy::request_reply (ACE_Token_Request &request, ACE_Synch_Options &) { ACE_TRACE ("ACE_Remote_Token_Proxy::request_reply"); void *buffer; ssize_t length; if ((length = request.encode (buffer)) == -1) ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("encode failed")), -1); ACE_SOCK_Stream *peer = ACE_Token_Connections::instance ()->get_connection (); if (peer == 0) ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("BIG PROBLEMS with get_connection")), -1); // Transmit request via a blocking send. if (peer->send_n (buffer, length) != length) ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("send_n failed")), -1); else { ACE_Token_Reply reply; // Receive reply via blocking read. if (peer->recv (&reply, sizeof reply) != sizeof reply) ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv failed")), -1); if (reply.decode () == -1) ACELIB_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("decode failed")), -1); errno = int (reply.errnum ()); if (errno != 0) return -1; else return 0; } }
int main() { ACE_INET_Addr server_addr; ACE_SOCK_Acceptor acceptor; ACE_SOCK_Stream peer; if (-1 == server_addr.set(22334)) { log("server_addr.set faild\n"); return 1; } if (-1 == acceptor.open(server_addr)) { log("acceptor.open failed\n"); return 1; } while(1) { if (-1 == acceptor.accept(peer)) { log("acceptor.accept failed\n"); return 1; } peer.disable(ACE_NONBLOCK); auto_ptr<char> pathname(get_url_pathname(&peer)); ACE_Mem_Map mapped_file(pathname.get()); if (-1 == (peer.send_n(mapped_file.addr(), mapped_file.size()))) { log("peer.send_n failed\n"); return 1; } peer.close(); } return acceptor.close() == -1 ? 1 : 0; }
static ACE_THR_FUNC_RETURN cli_worker (void *arg) { // Client thread function. ACE_INET_Addr addr (rendezvous); ACE_SOCK_Stream stream; ACE_SOCK_Connector connect; ACE_Time_Value delay (0, req_delay); size_t len = * reinterpret_cast<ACE_TCHAR *> (arg); for (size_t i = 0 ; i < cli_conn_no; i++) { if (connect.connect (stream, addr) < 0) { ACE_ERROR ((LM_ERROR, "(%t) %p\n", "connect")); continue; } for (size_t j = 0; j < cli_req_no; j++) { ACE_DEBUG ((LM_DEBUG, "(%t) conn_worker handle 0x%x, req %d\n", stream.get_handle (), j+1)); if (stream.send_n (arg, (len + 1) * sizeof (ACE_TCHAR)) == -1) { ACE_ERROR ((LM_ERROR, "(%t) %p\n", "send_n")); continue; } ACE_OS::sleep (delay); } stream.close (); } return 0; }
int main(int argc, char *argv[]) { ACE_INET_Addr addr(1500, "127.0.0.1"); //remote address ACE_SOCK_Connector con; // connector for socket client ACE_SOCK_Stream stream; // stream is for socket read/write if (con.connect(stream, addr) == -1) //connect to remote address { ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("connection failed"))); return 1; } const char msg[] = "Hello,ACE!"; stream.send_n(msg, sizeof(msg)); // send_n function send exactly n bytes char buffer[1024] = { 0 }; if (stream.recv(buffer, sizeof(buffer) - 1) == -1) // just call socket recv { ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("recv failed"))); return 1; } ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) recv:%s\n"), buffer)); if (stream.close() == -1) //close the connection { ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("close"))); return 1; } return 0; }
void send_work_to_server(ACE_TCHAR* arg) { ACE_SOCK_Stream stream; ACE_SOCK_Connector connect; ACE_Time_Value delay (0, req_delay); size_t len = * reinterpret_cast<ACE_TCHAR *> (arg); for (size_t i = 0 ; i < cli_conn_no; i++) { if (connect.connect (stream, addr_) < 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("connect"))); continue; } for (size_t j = 0; j < cli_req_no; j++) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sending work to server on handle 0x%x, req %d\n"), stream.get_handle (), j+1)); if (stream.send_n (arg, (len + 1) * sizeof (ACE_TCHAR)) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("send_n"))); continue; } ACE_OS::sleep (delay); } stream.close (); } }
void shut_down() { ACE_SOCK_Stream stream; ACE_SOCK_Connector connect; if (connect.connect (stream, addr_) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p Error while connecting\n"), ACE_TEXT ("connect"))); const ACE_TCHAR *sbuf = ACE_TEXT ("\011shutdown"); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("shutdown stream handle = %x\n"), stream.get_handle ())); if (stream.send_n (sbuf, (ACE_OS::strlen (sbuf) + 1) * sizeof (ACE_TCHAR)) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("send_n"))); stream.close (); }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { // args string dicomStem(argc > 1 ? argv[1] : "/data/subjects/xc_ferro/TrioTim-35115-20070929-151440-250000/250000"); int series = argc > 2 ? atoi(argv[2]) : 10; int numImgs = argc > 3 ? atoi(argv[3]) : 248; long tr = 1000*(argc > 4 ? atof(argv[4]) : 2000); int port = argc > 5 ? atoi(argv[5]) : 15000; string host(argc > 6 ? argv[6] : "localhost"); cout << "1 using dicomStem=" << dicomStem << endl; cout << "2 using series=" << series << endl; cout << "3 using numImgs=" << numImgs << endl; cout << "4 using tr=" << tr << endl; cout << "5 using port=" << port << endl; cout << "6 using host=" << host << endl; // Local server address. ACE_INET_Addr my_addr (port, host.c_str()); // Data transfer object. ACE_SOCK_Stream stream; // Initialize the connector. ACE_SOCK_Connector connector; // keep making new connections while we havent sent the whole series DicomImage *image; for(int i = 0; i < numImgs && (image = loadNextInSeries(dicomStem,series)) != NULL && !connector.connect (stream, my_addr); i++) { cout << "made connection, loading image" << endl; image->setMinMaxWindow(); unsigned short *upixelData = (unsigned short*)(image->getOutputData(16 /* bits */)); RtExternalImageInfo *ei = new RtExternalImageInfo(); ei->lImageDataLength = image->getOutputDataSize(); ei->lNumberOfPixels = ei->lImageDataLength/2; ei->bIsMoCo = true; ei->iNoOfImagesInMosaic = 32; ei->iMosaicGridSize = 6; ei->nCol = image->getHeight()/ei->iMosaicGridSize; ei->nLin = image->getWidth()/ei->iMosaicGridSize; // ei->nCol = 64; // ei->nLin = 64; ei->dThick = 3.5; ei->dPosSag = 2.50517; ei->dPosCor = -29.9335; ei->dPosTra = -75.1856; ei->dNorSag = -0.00637429; ei->dNorCor = 0.337923; ei->dNorTra = 0.941152; ei->dRowSag = 0.99998; ei->dRowCor = 0.00194039; ei->dRowTra = 0.00607602; ei->dColSag = 0.000227022; ei->dColCor = 0.941172; ei->dColTra = -0.337928; ei->iAcquisitionNumber = i+1; cout << "sending img " << ei->iAcquisitionNumber << endl; char *data = new char[ei->iSizeOfRtExternalImageInfo]; data = ei->convertToScannerDataArray(); cout << "sending info of size " << ei->iSizeOfRtExternalImageInfo << endl; stream.send_n (data, ei->iSizeOfRtExternalImageInfo); delete data; cout << "sending img of size " << ei->lImageDataLength << endl; // shorten to 12 bits short *pixelData = (short*) malloc(image->getOutputDataSize()); for(int i = 0; i < ei->lImageDataLength/2; i++) { pixelData[i] = upixelData[i]/16; } stream.send_n (pixelData, ei->lImageDataLength); usleep(tr); stream.close(); delete ei; delete image; free(pixelData); } return 0; }
int ACE_TMAIN (int, ACE_TCHAR *[]) { ACE_INET_Addr srvr (50000, ACE_LOCALHOST); ACE_SOCK_Connector connector; ACE_SOCK_Stream peer; ACE_ASSERT (connector.connect (peer, srvr) != -1); ssize_t bc; // Listing 1 code/ch06 iovec send[4]; send[0].iov_base = const_cast<char *> ("up"); send[0].iov_len = 2; send[1].iov_base = const_cast<char *> ("time"); send[1].iov_len = 4; send[2].iov_base = const_cast<char *> ("\n"); send[2].iov_len = 1; peer.sendv (send, 3); // Listing 1 // // A more clever approach would use something like this: // Where the addCommand() method allocates and populates // the query array from a set of global commands. // // Listing 2 code/ch06 iovec query[3]; addCommand (query, UPTIME); addCommand (query, HUMIDITY); addCommand (query, TEMPERATURE); peer.sendv (query, 3); // Listing 2 // Listing 3 code/ch06 iovec receive[2]; receive[0].iov_base = new char [32]; receive[0].iov_len = 32; receive[1].iov_base = new char [64]; receive[1].iov_len = 64; bc = peer.recvv (receive, 2); // Listing 3 // Listing 4 code/ch06 for (int i = 0; i < 2 && bc > 0; ++i) { size_t wc = receive[i].iov_len; if (static_cast<size_t> (bc) < wc) wc = static_cast<size_t> (bc); ACE_OS::write (ACE_STDOUT, receive[i].iov_base, wc); bc -= receive[i].iov_len; delete [] (reinterpret_cast<char *> (receive[i].iov_base)); } // Listing 4 // Listing 5 code/ch06 peer.send_n ("uptime\n", 7); iovec response; peer.recvv (&response); ACE_OS::write (ACE_STDOUT, response.iov_base, response.iov_len); delete [] reinterpret_cast<char *> (response.iov_base); // Listing 5 peer.close (); return (0); }
void JAWS_Synch_IO::transmit_file (JAWS_IO_Handler *ioh, const char *filename, const char *header, unsigned int header_size, const char *trailer, unsigned int trailer_size) { int result = 0; if (filename == 0) { ioh->transmit_file_error (-1); return; } JAWS_Cached_FILE cf (filename); if (cf.file ()->get_handle () != ACE_INVALID_HANDLE && cf.mmap () != 0) { #if defined (ACE_JAWS_BASELINE) || defined (ACE_WIN32) ACE_FILE_Info info; cf.file ()->get_info (info); if (cf.file ()->get_info (info) == 0 && info.size_ > 0) { ACE_SOCK_Stream stream; stream.set_handle (ioh->handle ()); if (((u_long) stream.send_n (header, header_size) == header_size) && (stream.send_n (cf.mmap ()->addr (), info.size_) == info.size_) && ((u_long) stream.send_n (trailer, trailer_size) == trailer_size)) { ioh->transmit_file_complete (); return; } else { result = -1; } } else { result = -1; } #else // Attempting to use writev // Is this faster? iovec iov[3]; int iovcnt = 0; if (header_size > 0) { iov[iovcnt].iov_base = const_cast<char*> (header); iov[iovcnt].iov_len = header_size; iovcnt++; } ACE_FILE_Info info; if (cf.file ()->get_info (info) == 0 && info.size_ > 0) { iov[iovcnt].iov_base = (char *) cf.mmap ()->addr (); iov[iovcnt].iov_len = info.size_; iovcnt++; } if (trailer_size > 0) { iov[iovcnt].iov_base = const_cast<char*> (trailer); iov[iovcnt].iov_len = trailer_size; iovcnt++; } if (ACE_OS::writev (ioh->handle (), iov, iovcnt) < 0) { result = -1; } else { ioh->transmit_file_complete (); return; } #endif /* ACE_JAWS_BASELINE */ } else if (cf.file ()->get_handle () != ACE_INVALID_HANDLE && cf.mmap () == 0) { this->transmit_file (ioh, cf.file ()->get_handle (), header, header_size, trailer, trailer_size); return; } else { result = -1; } if (result != 0) { ioh->transmit_file_error (result); } }
void JAWS_Synch_IO_No_Cache::transmit_file (const char *filename, const char *header, int header_size, const char *trailer, int trailer_size) { int result = 0; // Can we access the file? if (ACE_OS::access (filename, R_OK) == -1) { //ugly hack to send in HTTP_Status_Code::STATUS_NOT_FOUND result = ACE_Filecache_Handle::ACE_ACCESS_FAILED; this->handler_->transmit_file_error (result); return; } ACE_stat stat; // Can we stat the file? if (ACE_OS::stat (filename, &stat) == -1) { //ugly hack to send HTTP_Status_Code::STATUS_FORBIDDEN result = ACE_Filecache_Handle::ACE_STAT_FAILED; this->handler_->transmit_file_error (result); return; } ACE_OFF_T size = stat.st_size; // Can we open the file? ACE_HANDLE handle = ACE_OS::open (filename, O_RDONLY); if (handle == ACE_INVALID_HANDLE) { //ugly hack to send HTTP_Status_Code::STATUS_FORBIDDEN result = ACE_Filecache_Handle::ACE_OPEN_FAILED; this->handler_->transmit_file_error (result); return; } char* f = new char[size]; ACE_Auto_Basic_Array_Ptr<char> file (f); ACE_OS::read_n (handle, f, size); ACE_SOCK_Stream stream; stream.set_handle (this->handle_); if ((stream.send_n (header, header_size) == header_size) && (stream.send_n (f, size) == size) && (stream.send_n (trailer, trailer_size) == trailer_size)) { this->handler_->transmit_file_complete (); } else { //ugly hack to default to HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR result = -1; this->handler_->transmit_file_error (result); } ACE_OS::close (handle); }
int DC_Service_Request::do_request(KSG_WORK_SVR_HANDLER *handle) { ACE_Message_Block *mblk = handle->mblk_; ACE_SOCK_Stream peer; ACE_Message_Block *resp_buf; unsigned char *msg_begin = (unsigned char*)mblk->rd_ptr(); unsigned char *out_buf; unsigned char crc_code[4]; int data_len = mblk->length(); short pack_len; int len; int ret; peer.set_handle(handle->handle_); ACE_HEX_DUMP((LM_DEBUG,mblk->rd_ptr(),mblk->length())); if(msg_begin[0]!=0xC0 && msg_begin[data_len]!=0xC1) { ACE_DEBUG((LM_ERROR,"收到数据包起始符错误...")); return -1; } BUF_2_SHORT_BE(pack_len,(msg_begin+1)); if(data_len - 3 < pack_len ) { ACE_DEBUG((LM_ERROR,"收到错误数据包长度错误...")); return -1; } // check crc /* pack_len = GenerateCRC16(msg_begin+3,data_len-3-3); SHORT_2_BUF_BE(pack_len,crc_code); if(memcmp(crc_code,msg_begin+data_len-3,2)!=0) { ACE_DEBUG((LM_ERROR,"收到数据包CRC校验错误...")); return 0; } */ if(calc_sum(msg_begin+3,data_len-3-2)!=msg_begin[data_len-2]) { ACE_DEBUG((LM_ERROR,"收到数据包CRC校验错误...")); return 0; } ACE_NEW_RETURN(resp_buf,ACE_Message_Block(128),-1); len = 0; out_buf = (unsigned char*)resp_buf->wr_ptr(); out_buf[0]=0xC2; out_buf[3]=msg_begin[3]; switch(msg_begin[3]) { case 0x70: ret = do_upload_serial(msg_begin,data_len,out_buf+4,len); break; case 0x71: ret = do_download_blkcard(msg_begin,data_len,out_buf+4,len); break; default: ret = -1; break; } if(ret == 1) { if(len > 0) { // 计算CRC out_buf[4+len]=calc_sum(out_buf+3,len+1); len+=5; out_buf[len++] = 0xC3; pack_len = len - 3; SHORT_2_BUF_BE(pack_len,(out_buf+1)); resp_buf->wr_ptr(len); ACE_HEX_DUMP((LM_DEBUG,resp_buf->rd_ptr(),resp_buf->length())); ACE_Time_Value tv(0); if(peer.send_n(resp_buf,&tv) <=0 ) { ACE_DEBUG((LM_ERROR,"发送应答包失败")); ret = -1; } else { ret = 1; } } else ret = 0; } resp_buf->release(); return ret; }
int ACE_TMAIN(int argc, ACE_TCHAR **argv) { // size and count for transmissions int size = 0, count = -1; // the server's answer is a single byte char answer; // parse the <size> argument if ((argc < 2) || (((size = ACE_OS::strtol(argv[1], 0, 10)) < 1) || (errno == EINVAL))) return printUsage(argv[0]); // take size as the number of MiB and create appropriate buffer size *= BASE; char *someData = new (std::nothrow) char[size]; if (someData == 0) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%N:%l: Failed to allocate ") ACE_TEXT ("data buffer.\n")), -1); // put someData in an auto_ptr so it gets deleted automatically auto_ptr<char> pSomeData(someData); // parse the <count> argument if available if ((argc == 3) && (((count = ACE_OS::strtol(argv[2], 0, 10)) < 1) || (errno == EINVAL))) return printUsage(argv[0]); // the server listens on localhost on default port (from common.h) ACE_INET_Addr serverAddr(PORT, "localhost"); ACE_SOCK_Stream stream; ACE_SOCK_Connector connector; // -1 is running indefinitely while ((count == -1) || (count-- != 0)) { // some output, that we know something is happening //ACE_DEBUG((LM_DEBUG, ACE_TEXT("%N:%l: Passes left: %i\n"), count)); ACE_DEBUG((LM_DEBUG, ACE_TEXT("."))); // connect to the server and get the stream if (connector.connect(stream, serverAddr) == -1) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N:%l: Failed to connect to ") ACE_TEXT ("server. (errno = %i: %m)\n"), ACE_ERRNO_GET)); break; } try { // send the request to the server (number of MiB in the next call) // Note: only use the sizeof and pointer to int on compatible // platforms (i.e. little-endian/big-endian, data type size) if (stream.send_n(&size, sizeof(size), &connTimeout) != sizeof(size)) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N:%l: Failed to send ") ACE_TEXT ("request. (errno = %i: %m)\n"), ACE_ERRNO_GET)); throw 1; } // receive the answer if (stream.recv_n(&answer, sizeof(answer), &connTimeout) != 1) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N: %l: Failed to receive ") ACE_TEXT ("1st response. (errno = %i: %m)\n"), ACE_ERRNO_GET)); throw 1; } // server answer, 'K" indicates a positive answer if (answer == 'K') { // send a huge message to the server if (stream.send_n(someData, size, &connTimeout) != size) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N:%l: Failed to send ") ACE_TEXT ("someData. (errno = %i: %m)\n"), ACE_ERRNO_GET)); throw 1; } // get an answer if (stream.recv_n(&answer, sizeof(answer), &connTimeout) != 1) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N: %l: Failed to receive ") ACE_TEXT ("2nd response. (errno = %i: %m)\n"), ACE_ERRNO_GET)); throw 1; } // check the answer if (answer != 'K') { cout << "The server was unable to process the data." << endl; } } } catch (...) { // ok we know an error occurred, we need to close the socket. // The we'll try again. } // close the current stream if (stream.close() == -1) { ACE_ERROR((LM_ERROR, ACE_TEXT("%N:%l: Failed to close ") ACE_TEXT ("socket. (errno = %i: %m)\n"), ACE_ERRNO_GET)); break; } } // while cout << "Bye. Bye" << endl; return 0; }
// conduct the UnMarshalled Octet performance test using separate // send_n calls with Nagle's algorithm disabled ACE_SCTP::HIST runUnmarshalledOctetTest(ACE_CDR::Octet *buf, size_t seqLen, ACE_SOCK_Stream & stream){ ACE_CDR::ULong const testIterations = Options_Manager::test_iterations; size_t bt; ACE_CDR::ULong cnt = 0; // variables for the timing measurements ACE_hrtime_t startTime, endTime; ACE_CDR::Double messageLatency_usec = 0.0; ACE_CDR::ULong msgLen = seqLen*ACE_CDR::OCTET_SIZE; // explicity configure Nagling. Default is // Options_Manager::test_enable_nagle=0 so default configurations is // NO NAGLING ACE_CDR::Long nagle; if (Options_Manager::test_enable_nagle) nagle=0; else nagle=1; if (Options_Manager::test_transport_protocol == IPPROTO_SCTP){ // default - sctp case if (-1 == stream.set_option(IPPROTO_SCTP, SCTP_NODELAY, &nagle, sizeof nagle)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "set_option"), 0); } else { // tcp case if (-1 == stream.set_option(IPPROTO_TCP, TCP_NODELAY, &nagle, sizeof nagle)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "set_option"), 0); } // prime the client and server before starting the test for(cnt=0;cnt<primerIterations;++cnt){ // send message size // TODO : The message length should be CDR encoded ACE_CDR::ULong msgLenExpressed = ACE_HTONL(msgLen); if (-1 == stream.send_n (&msgLenExpressed, ACE_CDR::LONG_SIZE, 0, &bt)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_n"), 0); // send a message if (-1 == stream.send_n (buf, msgLen, 0, &bt)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_n"), 0); // block for a Short reply ACE_CDR::Short reply; if ((stream.recv_n(&reply, ACE_CDR::SHORT_SIZE, 0, &bt)) == -1) ACE_ERROR_RETURN((LM_ERROR, "%p\n", "recv_n"), 0); } // AFTER PRIMING THE PUMP CREATE THE HISTOGRAM ACE_SCTP::HIST aceStream_hist = 0; aceStream_hist = createHistogram(msgLen); if (0 == aceStream_hist) ACE_ERROR_RETURN((LM_ERROR, "%p\n", "histogram create failed"), 0); iovec iov[2]; // PERFORMANCE TEST LOOP for (cnt = 0; cnt < testIterations; ++cnt){ // get the start time startTime = ACE_OS::gethrtime(); if (!startTime) ACE_ERROR_RETURN((LM_ERROR, "%p\n", "ACE_OS::gethrtime()"), 0); ACE_CDR::ULong msgLenExpressed = ACE_HTONL(msgLen); iov[0].iov_base = reinterpret_cast<char *> (&msgLenExpressed); iov[0].iov_len = ACE_CDR::LONG_SIZE; iov[1].iov_base = reinterpret_cast<char *> (buf); iov[1].iov_len = msgLen; if (-1 == stream.sendv_n (iov, 2)) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "send_n"), 0); // block for a Short reply ACE_CDR::Short reply; if ((stream.recv_n(&reply, ACE_CDR::SHORT_SIZE, 0, &bt)) == -1) ACE_ERROR_RETURN((LM_ERROR, "%p\n", "recv_n"), 0); // get the end time endTime = ACE_OS::gethrtime(); if (!endTime) ACE_ERROR_RETURN((LM_ERROR, "%p\n", "ACE_OS::gethrtime()"), 0); // compute the message latency in micro-seconds messageLatency_usec = (static_cast<double> (ACE_UINT64_DBLCAST_ADAPTER(endTime)) - static_cast<double> (ACE_UINT64_DBLCAST_ADAPTER(startTime))) / microsec_clock_scale_factor; // record the message latency in the histogram ACE_SCTP::record(messageLatency_usec, aceStream_hist); } // THE HEADER MESSAGE SENT TO THE SERVER CONTAINED THE NUMBER OF // PRIMER AND TEST MESSAGES TO BE SENT AFTER WHICH THE SERVER WILL // CLOSE THE STREAM SO ONCE WE REACH THIS POINT THE STREAM IS NO // LONGER VALID AND WE CLOSE IT. stream.close(); // allocated by runTest delete[] buf; return aceStream_hist; }
int ACE_TMAIN (int, ACE_TCHAR *[]) { /* * Here we will use the default ctor and the set() * method to configure it. After each set() we will * display the address as a string and then connect * to each respective server. We can reuse the addr * instance once connection has been established. * // Listing 1 code/ch06 ACE_INET_Addr addr; ... addr.set ("HAStatus", ACE_LOCALHOST); ... addr.set ("HALog", ACE_LOCALHOST); // Listing 1 * */ ACE_INET_Addr addr; ACE_TCHAR peerAddress[64]; // Listing 2 code/ch06 addr.set (ACE_TEXT("HAStatus"), ACE_LOCALHOST); if (addr.addr_to_string (peerAddress, sizeof(peerAddress), 0) == 0) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Connecting to %s\n"), peerAddress)); } // Listing 2 // Listing 3 code/ch06 ACE_SOCK_Stream status; ACE_OS::last_error(0); ACE_SOCK_Connector statusConnector (status, addr); if (ACE_OS::last_error()) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("status")), 100); // Listing 3 addr.set (ACE_TEXT("HALog"), ACE_LOCALHOST); if (addr.addr_to_string (peerAddress, sizeof(peerAddress), 0) == 0) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Connecting to %s\n"), peerAddress )); } // Listing 4 code/ch06 ACE_SOCK_Connector logConnector; ACE_Time_Value timeout (10); ACE_SOCK_Stream log; if (logConnector.connect (log, addr, &timeout) == -1) { if (ACE_OS::last_error() == ETIME) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Timeout while ") ACE_TEXT ("connecting to log server\n"))); } else { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("log"))); } return (101); } // Listing 4 /* * We generally let the OS pick our local port number but * if you want, you can choose that also: // Listing 5 code/ch06 ACE_SOCK_Connector logConnector; ACE_INET_Addr local (4200, ACE_LOCALHOST); if (logConnector.connect (log, addr, 0, local) == -1) { ... // Listing 5 } */ char buf[64]; // Listing 6 code/ch06 ACE_Time_Value sendTimeout (0, 5); if (status.send_n ("uptime\n", 7, &sendTimeout) == -1) { if (ACE_OS::last_error() == ETIME) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Timeout while sending ") ACE_TEXT ("query to status server\n"))); } // Listing 6 else { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("send_n"))); } return (102); } // Listing 7 code/ch06 ssize_t bc ; ACE_Time_Value recvTimeout (0, 1); if ((bc = status.recv (buf, sizeof(buf), &recvTimeout)) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("recv"))); return (103); } log.send_n (buf, bc); // Listing 7 status.close (); log.close (); return (0); }