int ACE_MEM_Acceptor::accept (ACE_MEM_Stream &new_stream, ACE_MEM_Addr *remote_sap, ACE_Time_Value *timeout, int restart, int reset_new_handle) { ACE_TRACE ("ACE_MEM_Acceptor::accept"); int *len_ptr = 0; sockaddr *addr = 0; int in_blocking_mode = 1; if (this->shared_accept_start (timeout, restart, in_blocking_mode) == -1) return -1; else { do new_stream.set_handle (ACE_OS::accept (this->get_handle (), addr, len_ptr)); while (new_stream.get_handle () == ACE_INVALID_HANDLE && restart != 0 && errno == EINTR && timeout == 0); if (remote_sap != 0) { ACE_INET_Addr temp (ACE_reinterpret_cast (sockaddr_in *, addr), *len_ptr); remote_sap->set_port_number(temp.get_port_number ()); } }
int run_client (u_short port, ACE_MEM_IO::Signal_Strategy strategy) { int status = 0; ACE_MEM_Addr to_server (port); ACE_MEM_Connector connector; connector.preferred_strategy (strategy); ACE_MEM_Stream stream; // connector.preferred_strategy (ACE_MEM_IO::MT); if (connector.connect (stream, to_server.get_remote_addr ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Failed to connect to <%C> %p\n"), to_server.get_host_name (), ACE_TEXT ("connector.connect()")), -1); ACE_TCHAR buf[MAXPATHLEN]; for (size_t cntr = 0; cntr < NUMBER_OF_ITERATIONS; cntr ++) { ACE_OS::snprintf (buf, MAXPATHLEN, ACE_TEXT ("Iteration ") ACE_SIZE_T_FORMAT_SPECIFIER, cntr); ssize_t slen = (ACE_OS::strlen (buf) + 1) * sizeof (ACE_TCHAR); if (stream.send (buf, slen) < slen) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("In stream.send()"))); status = -1; break; } if (stream.recv (buf, MAXPATHLEN) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("stream.recv()"))); status = -1; break; } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("run_client(), got echo %s\n"), buf)); } status = stream.close () == -1 ? -1 : status; return status; }
static int run_client (void) { ACE_MEM_Connector connector; ACE_MEM_Stream stream; ACE_MEM_Addr server_addr (ACE_DEFAULT_SERVER_PORT); if (connector.connect (stream, server_addr.get_remote_addr ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "connect"), -1); char buf [MAXPATHLEN]; while (fgets (buf, MAXPATHLEN, stdin) >0) { stream.send (buf, ACE_OS::strlen (buf)+1); stream.recv (buf, MAXPATHLEN); ACE_DEBUG ((LM_DEBUG, "Echo: %s\n", buf)); } return 0; }
int ACE_MEM_Acceptor::shared_accept_finish (ACE_MEM_Stream new_stream, int in_blocking_mode, bool reset_new_handle) const { ACE_TRACE ("ACE_MEM_Acceptor::shared_accept_finish ()"); ACE_HANDLE new_handle = new_stream.get_handle (); // Check to see if we were originally in blocking mode, and if so, // set the <new_stream>'s handle and <this> handle to be in blocking // mode. if (in_blocking_mode) { // Save/restore errno. ACE_Errno_Guard error (errno); // Only disable ACE_NONBLOCK if we weren't in non-blocking mode // originally. ACE::clr_flags (this->get_handle (), ACE_NONBLOCK); ACE::clr_flags (new_handle, ACE_NONBLOCK); } #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0) if (reset_new_handle) // Reset the event association inherited by the new handle. ::WSAEventSelect ((SOCKET) new_handle, 0, 0); #else ACE_UNUSED_ARG (reset_new_handle); #endif /* ACE_WIN32 */ if (new_handle == ACE_INVALID_HANDLE) return -1; return 0; }
int ACE_MEM_Connector::connect (ACE_MEM_Stream &new_stream, const ACE_INET_Addr &remote_sap, ACE_Time_Value *timeout, const ACE_Addr &local_sap, int reuse_addr, int flags, int perms) { ACE_TRACE ("ACE_MEM_Connector::connect"); if (!this->address_.same_host (remote_sap)) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%P|%t) MEM_Connector can't connect ") ACE_TEXT ("to %C:%d which is not a local endpoint ") ACE_TEXT ("(local address is %C:%d)\n"), remote_sap.get_host_name (), remote_sap.get_port_number (), this->address_.get_host_name (), this->address_.get_port_number ()), -1); else this->address_.set_port_number (remote_sap.get_port_number ()); ACE_SOCK_Stream temp_stream; if (ACE_SOCK_Connector::connect (temp_stream, this->address_.get_local_addr (), timeout, local_sap, reuse_addr, flags, perms) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_MEM_Connector::connect")), -1); ACE_HANDLE new_handle = temp_stream.get_handle (); new_stream.set_handle (new_handle); new_stream.disable (ACE_NONBLOCK); // Do not close the handle. // now we should setup the mmap malloc. ACE_TCHAR buf[MAXPATHLEN]; // @@ Need to handle timeout here. ACE_INT16 server_strategy = ACE_MEM_IO::Reactive; // Receive the signaling strategy theserver support. if (ACE::recv (new_handle, &server_strategy, sizeof (ACE_INT16)) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Connector::connect error receiving strategy\n")), -1); // If either side don't support MT, we will not use it. #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM) if (! (this->preferred_strategy_ == ACE_MEM_IO::MT && server_strategy == ACE_MEM_IO::MT)) #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */ server_strategy = ACE_MEM_IO::Reactive; if (ACE::send (new_handle, &server_strategy, sizeof (ACE_INT16)) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Connector::connect error sending strategy\n")), -1); ACE_INT16 buf_len; // Byte-order is not a problem for this read. if (ACE::recv (new_handle, &buf_len, sizeof (buf_len)) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Connector::connect error receiving shm filename length\n")), -1); if (ACE::recv (new_handle, buf, buf_len) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Connector::connect error receiving shm filename.\n")), -1); if (new_stream.init (buf, static_cast<ACE_MEM_IO::Signal_Strategy> (server_strategy), &this->malloc_options_) == -1) return -1; return 0; }
int ACE_MEM_Acceptor::accept (ACE_MEM_Stream &new_stream, ACE_MEM_Addr *remote_sap, ACE_Time_Value *timeout, bool restart, bool reset_new_handle) { ACE_TRACE ("ACE_MEM_Acceptor::accept"); int in_blocking_mode = 1; if (this->shared_accept_start (timeout, restart, in_blocking_mode) == -1) return -1; else { sockaddr *addr = 0; struct sockaddr_in inet_addr; int *len_ptr = 0; int len = 0; if (remote_sap != 0) { addr = reinterpret_cast<sockaddr *> (&inet_addr); len = sizeof (inet_addr); len_ptr = &len; } do // On Win32 the third parameter to <accept> must be a NULL // pointer if to ignore the client's address. new_stream.set_handle (ACE_OS::accept (this->get_handle (), addr, len_ptr)); while (new_stream.get_handle () == ACE_INVALID_HANDLE && restart != 0 && errno == EINTR && timeout == 0); if (remote_sap != 0) { ACE_INET_Addr temp (&inet_addr, len); remote_sap->set_port_number (temp.get_port_number ()); } } if (this->shared_accept_finish (new_stream, in_blocking_mode, reset_new_handle) == -1) return -1; // Allocate 2 * MAXPATHLEN so we can accomodate the unique // name that gets appended later ACE_TCHAR buf [2 * MAXPATHLEN + 1]; ACE_INET_Addr local_addr; if (new_stream.get_local_addr (local_addr) == -1) return -1; if (this->mmap_prefix_ != 0) { ACE_OS::sprintf (buf, ACE_TEXT ("%s_%d_"), this->mmap_prefix_, local_addr.get_port_number ()); } else { ACE_TCHAR name[25]; // - 24 is so we can append name to the end. if (ACE::get_temp_dir (buf, MAXPATHLEN - 24) == -1) { ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("Temporary path too long, ") ACE_TEXT ("defaulting to current directory\n"))); buf[0] = 0; } ACE_OS::sprintf (name, ACE_TEXT ("MEM_Acceptor_%d_"), local_addr.get_port_number ()); ACE_OS::strcat (buf, name); } ACE_TCHAR unique [MAXPATHLEN]; ACE_OS::unique_name (&new_stream, unique, MAXPATHLEN); ACE_OS::strcat (buf, unique); // Make sure we have a fresh start. ACE_OS::unlink (buf); new_stream.disable (ACE_NONBLOCK); ACE_HANDLE new_handle = new_stream.get_handle (); // Protocol negociation: // Tell the client side what level of signaling strategy // we support. ACE_MEM_IO::Signal_Strategy client_signaling = #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM) this->preferred_strategy_; #else // We don't support MT. ACE_MEM_IO::Reactive; #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */ if (ACE::send (new_handle, &client_signaling, sizeof (ACE_INT16)) == -1) ACELIB_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Acceptor::accept error sending strategy\n")), -1); // Now we get the signaling strategy the client support. if (ACE::recv (new_handle, &client_signaling, sizeof (ACE_INT16)) == -1) ACELIB_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("ACE_MEM_Acceptor::%p error receiving strategy\n"), ACE_TEXT ("accept")), -1); // Ensure minimum buffer size if (this->malloc_options_.minimum_bytes_ < ACE_MEM_STREAM_MIN_BUFFER) this->malloc_options_.minimum_bytes_ = ACE_MEM_STREAM_MIN_BUFFER; // Client will decide what signaling strategy to use. // Now set up the shared memory malloc pool. if (new_stream.init (buf, static_cast<ACE_MEM_IO::Signal_Strategy> (client_signaling), &this->malloc_options_) == -1) return -1; // @@ Need to handle timeout here. ACE_UINT16 buf_len = static_cast<ACE_UINT16> ((ACE_OS::strlen (buf) + 1) * sizeof (ACE_TCHAR)); // No need to worry about byte-order because both parties should always // be on the same machine. if (ACE::send (new_handle, &buf_len, sizeof (ACE_UINT16)) == -1) return -1; // Now send the pathname of the mmap file. if (ACE::send (new_handle, buf, buf_len) == -1) return -1; return 0; }