USING_NAMESPACE int main() { Stream stream; Connector connector; if( connector.connect(stream, InterAddress(6000, "127.0.0.1"), TimeValue(1)) ) { char ip[20]; int32 port; stream.getLocalAddress(ip, port); printf("local addr:[%s][%d]\n", ip, port); char buf[100] = "hello world!"; if( SOCKET_ERROR != stream.send(buf, strlen(buf)) ) printf("send successed!\n"); char recvbuf[100]; memset(recvbuf, 0, sizeof(recvbuf)); if (SOCKET_ERROR != stream.recv(recvbuf, 100)) printf("recv: %s", recvbuf); } else { printf("Connector failed!!"); } getchar(); return 0; }
Sender * Invocation_Thread::create_connection (void) { int result = 0; // Connector for creating new connections. Connector connector (this->thread_manager_, this->reactor_, this->nested_upcalls_); // <server_handle> is a global variable. It will be used later by // the Close_Socket_Thread. result = connector.connect (client_handle, server_handle, this->run_receiver_thread_); ACE_TEST_ASSERT (result == 0); ACE_UNUSED_ARG (result); // Create a new sender. Sender *sender = new Sender (client_handle, this->connection_cache_); // Register it with the cache. this->connection_cache_.add_connection (sender); // // There might be a race condition here. The sender has been added // to the cache and is potentially available to other threads // accessing the cache. Therefore, the other thread may use this // sender and potentially close the sender before it even gets // registered with the Reactor. // // This is resolved by marking the connection as busy when it is // first added to the cache. And only once the thread creating the // connection is done with it, it is marked a available in the // cache. // // This order of registration is important. // // Register the handle with the Reactor. result = this->reactor_.register_handler (client_handle, sender, ACE_Event_Handler::READ_MASK); #if 0 ACE_TEST_ASSERT (result == 0); ACE_UNUSED_ARG (result); #else if (result != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) create_connection h %d, %p\n"), client_handle, ACE_TEXT ("register_handler"))); #endif return sender; }
void main_connect() { ACE_INET_Addr addr(PORT_NO, "192.168.0.56"); Connector myconnector; My_Svc_Handler *my_svc_handler = new My_Svc_Handler; myconnector.connect(my_svc_handler, addr); while(1) Reactor::instance()->handle_events(); return; }
Connector* NetFactory::connect(const string& ip, int port, INetReactor &netReactor, const char* remoteHostName) { NetAddress peerAddr(ip, port); // ´´½¨Ò»¸öÍøÂçÁ¬½ÓÆ÷£¬¸ÃÍøÂç¼àÌýÆ÷½«±»×¢²áµ½ÍøÂçÖÐ Connector* connector = new Connector(peerAddr, &netReactor, nextNet(), remoteHostName, this); if (NULL == connector) { LOG_SYSTEM_ERR << "connect to <" << ip << ": " << port << "> failed, not enough memory"; return NULL; } if (!connector->open()) { LOG_SYSTEM_ERR << "connect to <" << ip << ": " << port << "> failed, open failed"; delete connector; return NULL; } connector->connect(); m_connectors.push_back(connector); return connector; }
int run_main (int argc, ACE_TCHAR *argv[]) { ACE_START_TEST (ACE_TEXT ("Proactor_Scatter_Gather_Test")); if (::parse_args (argc, argv) == -1) return -1; chunk_size = ACE_OS::getpagesize (); if (client_only) ACE_DEBUG ((LM_INFO, ACE_TEXT ("Running as client only, page size %d\n"), chunk_size)); else if (server_only) ACE_DEBUG ((LM_INFO, ACE_TEXT ("Running as server only, page size %d\n"), chunk_size)); else ACE_DEBUG ((LM_INFO, ACE_TEXT ("Running as server and client, page size %d\n"), chunk_size)); Acceptor acceptor; Connector connector; ACE_INET_Addr addr (port); if (!client_only) { // Simplify, initial read with zero size if (-1 == acceptor.open (addr, 0, 1)) { ACE_TEST_ASSERT (0); return -1; } } if (!server_only) { if (-1 == connector.open (1, ACE_Proactor::instance ())) { ACE_TEST_ASSERT (0); return -1; } // connect to first destination if (addr.set (port, host, 1, addr.get_type ()) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), host), -1); connector.set_address (addr); if (-1 == connector.connect (addr)) { ACE_TEST_ASSERT (0); return -1; } } ACE_Proactor::instance ()->run_event_loop (); // As Proactor event loop now is inactive it is safe to destroy all // senders connector.stop (); acceptor.stop (); ACE_Proactor::instance()->close_singleton (); // now compare the files - available only when on same machine int success = 0; if (!client_only && !server_only) { ACE_DEBUG ((LM_INFO, ACE_TEXT ("Comparing the input file and the output file...\n"))); success = -1; // map the two files, then perform memcmp { ACE_Mem_Map original_file (input_file); ACE_Mem_Map reconstructed_file (output_file); if (original_file.addr () && original_file.addr () != MAP_FAILED && reconstructed_file.addr () && reconstructed_file.addr () != MAP_FAILED) { // compare lengths if ((original_file.size () == reconstructed_file.size ()) && // and if same size, compare file data (0 == ACE_OS::memcmp (original_file.addr (), reconstructed_file.addr (), original_file.size ()))) success = 0; } } if (0 == success) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("input file and the output file identical!\n"))); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("input file and the output file are different!\n"))); } if (!client_only) ACE_OS::unlink (output_file); ACE_END_TEST; return success; }