// Get a copy of the handles passed to the spawned process. This // will be the set of handles previously passed to @arg pass_handle(). int ACE_Process_Options::passed_handles (ACE_Handle_Set &set) const { if (this->handles_passed_.num_set () == 0) return 0; set.reset (); set = this->handles_passed_; return 1; }
int ACE_Select_Reactor_Notify::dispatch_notifications (int &number_of_active_handles, ACE_Handle_Set &rd_mask) { ACE_TRACE ("ACE_Select_Reactor_Notify::dispatch_notifications"); ACE_HANDLE const read_handle = this->notification_pipe_.read_handle (); if (read_handle != ACE_INVALID_HANDLE && rd_mask.is_set (read_handle)) { --number_of_active_handles; rd_mask.clr_bit (read_handle); return this->handle_input (read_handle); } else return 0; }
void ChatServer::printHandlesSet(ACE_Handle_Set& handles, bool master, char* procedure_name) { Util::log("%s ACE_Handle_Set %s; called from %s\n", "[ChatServer::printHandlesSet]", (master ? "MASTER" : "ACTIVE"), procedure_name); fd_set * fdset = handles.fdset(); if (fdset) { u_int fd_count = fdset->fd_count; SOCKET* sa = fdset->fd_array; Util::log("\tfd_count=%d\n", fd_count); for (u_int i = 0; i < fd_count; i++) { SOCKET s = sa[i]; Util::log("\t\tSOCKET=%d\n", s); } } else { Util::log("\tfdset is EMPTY\n"); } }
int ACE_TMAIN (int argc, ACE_TCHAR **argv){ Options_Manager optsMgr(argc, argv, ACE_TEXT ("server-opts")); // show usage is requested if (optsMgr._usage) { optsMgr._show_usage(stderr, ACE_TEXT ("server-opts")); return 1; } // If SCTP is not installed then terminate the program, unless TCP // was specified. #ifndef ACE_HAS_SCTP if (optsMgr.test_transport_protocol == IPPROTO_SCTP) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("SCTP was NOT installed when this binary was compiled.\n") ACE_TEXT ("SOCK_STREAM_srv may still be run using TCP ") ACE_TEXT ("via the '-t tcp' option.\n")), 1); #endif // check that valid options were specified if (optsMgr._error) { ACE_OS::fprintf (stderr, "ERROR: %s\n", ACE_TEXT_ALWAYS_CHAR (optsMgr._error_message)); return 1; } // this is the socket that the server will listen on ACE_SOCK_Acceptor acceptor_socket; // Create the address that we want to listen for connections on. If // server_accept_addr=INADDR_ANY (the default), SCTP will listen for // connections on all IP interfaces. If an address is specified, // SCTP will listen for connections on that address ONLY. ACE_INET_Addr serverAddr(optsMgr.server_port, optsMgr.server_accept_addr); ACE_DEBUG((LM_DEBUG, ACE_TEXT ("(%P|%t) Accepting connections on port %u on interface %C using %C\n"), serverAddr.get_port_number(), (optsMgr.server_accept_addr == INADDR_ANY) ? "INADDR_ANY" : serverAddr.get_host_addr(), (optsMgr.test_transport_protocol == IPPROTO_SCTP) ? "IPPROTO_SCTP" : "IPPROTO_TCP")); // this operation creates a socket, binds the specified internet // address to it and calls listen. As this is a wrapper facade // approach, the ACE_OS::{socket,bind,listen} calls are invoked in // the implementation of open. if (acceptor_socket.open(serverAddr, 1, AF_INET, ACE_DEFAULT_BACKLOG, optsMgr.test_transport_protocol) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), 1); // this function checks that the port that was actually bound was // the port we asked for. Apparently some operating systems will // automatically select new ports if the specified port is currently // used. else if (acceptor_socket.get_local_addr(serverAddr) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("get_local_addr")), 1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) starting server at port %d\n"), serverAddr.get_port_number())); // this is the stream object that will associated with a completed // connection (aka the data mode socket). It will be set when accept // is called below. ACE_SOCK_Stream new_stream; // a file decriptor set ACE_Handle_Set handle_set; // add the acceptor socket to the file descriptor set. handle_set.set_bit(acceptor_socket.get_handle()); for (;;){ ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT); ACE_Handle_Set temp = handle_set; // wait for connect() call from client. In the original test there // were two different acceptor sockets for two different // services. So select was needed to wait on both sockets // simultaneously. In this test we could just call accept on the // one socket. int result = ACE_OS::select(ACE_Utils::truncate_cast<int> ((intptr_t)acceptor_socket.get_handle()) +1, (fd_set *) temp, 0, 0, timeout); // check that select did not end with an error. if (result == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), ACE_TEXT ("select"))); // check to see if select timed out. else if (result == 0){ ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) select timed out\n"))); } else { // case where a file descriptor was actually set if (!(temp.is_set(acceptor_socket.get_handle()))){ // CANNOT BE REACHED ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), ACE_TEXT ("select: NO ERROR BUT NO FD SET"))); } else { // call accept to set up the new stream. if (acceptor_socket.accept(new_stream) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("accept"))); continue; } else{ ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) spawning server\n"))); } // Run the server. run_server (new_stream.get_handle ()); } } } ACE_NOTREACHED (return 0;) }
static int run_event_loop (u_short port) { // Raise the socket handle limit to the maximum. ACE::set_handle_limit (); // Create the oneway and twoway acceptors. ACE_SSL_SOCK_Acceptor twoway_acceptor; ACE_SSL_SOCK_Acceptor oneway_acceptor; // Create the oneway and twoway server addresses. ACE_INET_Addr twoway_server_addr (port); ACE_INET_Addr oneway_server_addr (port + 1); // Create acceptors, reuse the address. if (twoway_acceptor.open (twoway_server_addr, 1) == -1 || oneway_acceptor.open (oneway_server_addr, 1) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), 1); else if (twoway_acceptor.get_local_addr (twoway_server_addr) == -1 || oneway_acceptor.get_local_addr (oneway_server_addr) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "get_local_addr"), 1); ACE_DEBUG ((LM_DEBUG, "(%P|%t) starting twoway server at port %d and oneway server at port %d\n", twoway_server_addr.get_port_number (), oneway_server_addr.get_port_number ())); // Keep these objects out here to prevent excessive constructor // calls within the loop. ACE_SSL_SOCK_Stream new_stream; ACE_Handle_Set handle_set; handle_set.set_bit (twoway_acceptor.get_handle ()); handle_set.set_bit (oneway_acceptor.get_handle ()); // Performs the iterative server activities. for (;;) { ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT); ACE_Handle_Set temp = handle_set; int maxfd = int(oneway_acceptor.get_handle ()); if (maxfd < int(twoway_acceptor.get_handle ())) maxfd = int(twoway_acceptor.get_handle ()); int result = ACE_OS::select (maxfd + 1, (fd_set *) temp, 0, 0, timeout); if (result == -1) ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n", "select")); else if (result == 0 && verbose) ACE_DEBUG ((LM_DEBUG, "(%P|%t) select timed out\n")); else { if (temp.is_set (twoway_acceptor.get_handle ())) { int r = twoway_acceptor.accept (new_stream); while (r == -1 && errno == EAGAIN) r = twoway_acceptor.accept (new_stream); if (r == -1) { ACE_ERROR ((LM_ERROR, "%p\n", "accept")); continue; } else ACE_DEBUG ((LM_DEBUG, "(%P|%t) spawning twoway server\n")); // Run the twoway server. twoway_server (new_stream); } if (temp.is_set (oneway_acceptor.get_handle ())) { if (oneway_acceptor.accept (new_stream) == -1) { ACE_ERROR ((LM_ERROR, "%p\n", "accept")); continue; } else ACE_DEBUG ((LM_DEBUG, "(%P|%t) spawning oneway server\n")); // Run the oneway server. oneway_server (new_stream); } } } /* NOTREACHED */ }
int ACE_TMAIN(int argc, ACE_TCHAR* argv[]){ u_long priority_mask = ACE_LOG_MSG->priority_mask (ACE_Log_Msg::PROCESS); ACE_CLR_BITS (priority_mask, LM_DEBUG|LM_TRACE); ACE_LOG_MSG->priority_mask (priority_mask, ACE_Log_Msg::PROCESS); ACE::set_handle_limit(); ACE_Thread_Manager* mgr = ACE_Thread_Manager::instance(); ACE_SOCK_Acceptor _987acceptor; ACE_INET_Addr _987addr(0x987);//2439 if(_987acceptor.open(_987addr, 1) == -1){ ACE_ERROR_RETURN((LM_ERROR, "%p\n", "open"), 1); }else if(_987acceptor.get_local_addr(_987addr) == -1){ ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "get_local_addr"), 1); } ACE_DEBUG((LM_INFO, "(%P|%t) starting 987 server at port %d\n", _987addr.get_port_number())); ACE_SOCK_Stream _str; ACE_Handle_Set _set; _set.set_bit(_987acceptor.get_handle()); do{ ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT); ACE_Handle_Set tmp = _set; int result = ACE_OS::select(ACE_Utils::truncate_cast<int> ((intptr_t) _987acceptor.get_handle()) +1, (fd_set *) tmp, 0, 0, NULL); if(result == -1) ACE_ERROR((LM_ERROR, "(%P|%t) %p\n", "select")); else if(result==0) ACE_DEBUG((LM_INFO, "(%P|%t) select timed out\n")); else{ if(tmp.is_set(_987acceptor.get_handle())){ if(_987acceptor.accept(_str) == -1){ ACE_ERROR((LM_ERROR, "%p\n", "987 accept")); continue; }else{ ACE_DEBUG((LM_INFO, "(%P|%t: %l) spawning 987 server\n")); if(mgr->spawn(process, reinterpret_cast<void*> (_str.get_handle()), THR_DETACHED) == -1){ ACE_ERROR((LM_ERROR, "(%P|%t) %p\n", "spawn")); } } } } }while(true); return 0; }
static int run_event_loop (u_short port) { // Raise the socket handle limit to the maximum. ACE::set_handle_limit (); // Create the oneway and twoway acceptors. ACE_SSL_SOCK_Acceptor twoway_acceptor; ACE_SSL_SOCK_Acceptor oneway_acceptor; // Create the oneway and twoway server addresses. ACE_INET_Addr twoway_server_addr (port); ACE_INET_Addr oneway_server_addr (port + 1); // Create acceptors, reuse the address. if (twoway_acceptor.open (twoway_server_addr, 1) == -1 || oneway_acceptor.open (oneway_server_addr, 1) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), 1); else if (twoway_acceptor.get_local_addr (twoway_server_addr) == -1 || oneway_acceptor.get_local_addr (oneway_server_addr) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "get_local_addr"), 1); ACE_DEBUG ((LM_DEBUG, "(%P|%t) starting twoway server at port %d and oneway server at port %d\n", twoway_server_addr.get_port_number (), oneway_server_addr.get_port_number ())); // Keep these objects out here to prevent excessive constructor // calls within the loop. ACE_Handle_Set handle_set; handle_set.set_bit (twoway_acceptor.get_handle ()); handle_set.set_bit (oneway_acceptor.get_handle ()); ACE_SSL_SOCK_Stream * new_stream = 0; // Performs the iterative server activities. for (;;) { ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT); ACE_Handle_Set temp = handle_set; int result = ACE_OS::select (int (oneway_acceptor.get_handle ()) + 1, (fd_set *) temp, 0, 0, timeout); if (result == -1) ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n", "select")); else if (result == 0 && verbose) ACE_DEBUG ((LM_DEBUG, "(%P|%t) select timed out\n")); else { // A new ACE_SSL_SOCK_Stream must be initialized for each // connection. However, it retains (SSL) state so simply // initializing a new ACE_SSL_SOCK_Stream by passing it a // handle isn't enough, nor is it allowed. Such a scheme is // is sometimes done with the non-SSL aware // ACE_SOCK_Stream. An ACE_SSL_SOCK_Stream should only be // initialized by an ACE_SSL_SOCK_Acceptor (server side), or an // ACE_SSL_SOCK_Connector (client side). // // It is also possible to copy or assign an // ACE_SSL_SOCK_Stream since it implements both // methods/operators. However, the user must ensure that // the copy or assignment is atomic. if (temp.is_set (twoway_acceptor.get_handle ())) { ACE_NEW_RETURN (new_stream, ACE_SSL_SOCK_Stream, -1); if (twoway_acceptor.accept (*new_stream) == -1) { ACE_ERROR ((LM_ERROR, "%p\n", "accept")); delete new_stream; continue; } else ACE_DEBUG ((LM_DEBUG, "(%P|%t) spawning twoway server\n")); // Run the twoway server. run_server (twoway_server, new_stream); } if (temp.is_set (oneway_acceptor.get_handle ())) { ACE_NEW_RETURN (new_stream, ACE_SSL_SOCK_Stream, -1); if (oneway_acceptor.accept (*new_stream) == -1) { ACE_ERROR ((LM_ERROR, "%p\n", "accept")); delete new_stream; continue; } else ACE_DEBUG ((LM_DEBUG, "(%P|%t) spawning oneway server\n")); // Run the oneway server. run_server (oneway_server, new_stream); } } } /* NOTREACHED */ }