int main(int argc, char** argv) { if (argc < 2 || argc > 4) { printf("usage: server <port> [<ip>]\n"); exit(1); } TCPStream* stream = NULL; TCPAcceptor* acceptor = NULL; if (argc == 3) { acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]); } else { acceptor = new TCPAcceptor(atoi(argv[1])); } if (acceptor->start() == 0) { while (1) { stream = acceptor->accept(); if (stream != NULL) { ssize_t len; char line[256]; while ((len = stream->receive(line, sizeof(line))) > 0) { line[len] = 0; printf("received - %s\n", line); stream->send(line, len); } delete stream; } } } exit(0); }
int main(int argc, char** argv) { if (argc < 2 || argc > 4) { printf("usage: server <port> [<ip>]\n"); exit(1); } TCPStream* stream = NULL; TCPAcceptor* acceptor = NULL; if (argc == 3) { acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]); } else { acceptor = new TCPAcceptor(atoi(argv[1])); } printf("server started successfully\n"); if (acceptor->start() == 0) { while(1) { stream = acceptor->accept(); if (stream != NULL) { size_t len; char buffer[256]; while((len = stream->receive(buffer, sizeof(buffer))) > 0) { buffer[len] = '\0'; printf("received: %s\n", buffer); stream->send(buffer, len); } delete stream; } } } perror("Could not start the server"); exit(-1); }
/* * 起一个监听端口。 * * @param spec: 格式 [upd|tcp]:ip:port * @param streamer: 数据包的双向流,用packet创建,解包,组包。 * @param serverAdapter: 用在服务器端,当Connection初始化及Channel创建时回调时用 * @return IO组件一个对象的指针 */ IOComponent *Transport::listen (const char *spec, IPacketStreamer *streamer, IServerAdapter *serverAdapter, int timeout) { MutexGuard guard(&_stopMutex); if (_stop) { ANET_LOG(SPAM, "Transport(%p) Stoped!", this); return NULL; } if (NULL == spec || NULL == streamer || NULL == serverAdapter) { ANET_LOG(WARN, "Invalid parameters for listen(%p,%p,%p)", spec, streamer, serverAdapter); return NULL; } char tmp[512]; char *args[32]; strncpy(tmp, spec, 512); tmp[511] = '\0'; if (parseAddr(tmp, args, 32) != 3) { return NULL; } if (strcasecmp(args[0], "tcp") == 0) { char *host = args[1]; int port = atoi(args[2]); // Server Socket ServerSocket *socket = new ServerSocket(); assert(socket); if (!socket->setAddress(host, port)) { delete socket; return NULL; } // TCPAcceptor TCPAcceptor *acceptor = new TCPAcceptor(this, socket, streamer, serverAdapter, timeout); assert(acceptor); if (!acceptor->init()) { delete acceptor; return NULL; } // 返回 return acceptor; } else if (strcasecmp(args[0], "udp") == 0) {} return NULL; }
int main(int argc, char** argv) { int conn_timeout = 10, rw_timeout = 0; // As of now this is to choose the server to connect to if(argc != 3){ printf("Usage: server <port> [<ip>]\n"); exit(1); } TCPStream* stream = NULL; TCPAcceptor* acceptor = NULL; acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]); // This is hard coded to the server thats running the redis server acl::string addr("135.44.219.148:6380"); acl::acl_cpp_init(); acl::log::stdout_open(true); acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout); acl::redis_pubsub redis(&client); // Hard coded channel acl::string channel = "channel", msg; redis.clear(); // Have this program run continuously, and if an ip address is picked up, publish it to the redis server if (acceptor->start() == 0) { while (1) { stream = acceptor->accept(); if (stream != NULL) { printf("IP %s\n", stream->getPeerIP().c_str()); msg.format("%s", stream->getPeerIP().c_str()); redis.publish(channel, msg, msg.length()); } } } #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); #endif return 0; }
int main(int argc, char *argv[]) { // TCP server TCPStream *stream = NULL; TCPAcceptor *acceptor = NULL; if (argc != 1) { usage(argv); return -1; } initCommandMap(); acceptor = new TCPAcceptor(TCP_PORT); if (acceptor->start() != 0) { cerr << LOG_PREFIX "Could not start TCPAcceptor" << endl; return -1; } cout << LOG_PREFIX "Initialized TLM Server" << endl; while(1) { cout << LOG_PREFIX "Waiting for connection." << endl; stream = acceptor->accept(); cout << LOG_PREFIX "Connection Accepted, sending telemetry. " << endl; if (stream != NULL) { ssize_t len; char line[256]; string retval; const char *txstr; while ((len = stream->receive(line, sizeof(line))) > 0) { line[len] = 0; retval = parse(line); txstr = retval.c_str(); stream->send(txstr, retval.length()); } delete stream; cout << LOG_PREFIX "Connection closed." << endl; } } delete acceptor; return 0; }
void peerListener::listen(){ int timer = 0; messageReader* mr = new messageReader(peer, ""); TCPAcceptor* acceptor = new TCPAcceptor(peer->port, peer->address.c_str(), false, false, true); if (acceptor->start() == 0) { while (timer < mainTimer) { TCPStream* stream = acceptor->accept(); if (stream != NULL) { mr->readMsg(stream); PeerHandler* ph = new PeerHandler(peer, stream, peer->tmpPeerName); ph->start(); peer->peerHandlers[peer->tmpPeerName]= ph; } sleep(1); timer += 1; } } delete mr; delete acceptor; }
/* * 起一个监听端口。 * * @param spec: 格式 [upd|tcp]:ip:port * @param streamer: 数据包的双向流,用packet创建,解包,组包。 * @param serverAdapter: 用在服务器端,当Connection初始化及Channel创建时回调时用 * @return IO组件一个对象的指针 */ IOComponent *Transport::listen(const char *spec, IPacketStreamer *streamer, IServerAdapter *serverAdapter) { char tmp[1024]; char *args[32]; strncpy(tmp, spec, 1024); tmp[1023] = '\0'; if (parseAddr(tmp, args, 32) != 3) { return NULL; } if (strcasecmp(args[0], "tcp") == 0) { char *host = args[1]; int port = atoi(args[2]); // Server Socket ServerSocket *socket = new ServerSocket(); if (!socket->setAddress(host, port)) { delete socket; return NULL; } // TCPAcceptor TCPAcceptor *acceptor = new TCPAcceptor(this, socket, streamer, serverAdapter); if (!acceptor->init()) { delete acceptor; return NULL; } // 加入到iocomponents中,及注册可读到socketevent中 addComponent(acceptor, true, false); // 返回 return acceptor; } else if (strcasecmp(args[0], "udp") == 0) {} return NULL; }
//this function received the command from ios, and push them to the queue. int SocketServerManager::run(){ TCPStream* stream = NULL; TCPAcceptor* acceptor = NULL; CommandQueue cmdQueue; Command rcvCmd; acceptor = new TCPAcceptor(8081); if (acceptor->start() == 0) { while (1) { stream = acceptor->accept(); if (stream != NULL) { ssize_t len; char line[256]; while ((len = stream->receive(line, sizeof(line))) > 0) { line[len] = 0; string cmd = string(line); std::cout << "running Command: " << cmd << std::endl; if( cmd == "forward"){ rcvCmd.type = pi2::FORWARD; rcvCmd.m1speed = 15; rcvCmd.m2speed = 15; cmdQueue.putCommand(rcvCmd); } else if( cmd == "back"){ rcvCmd.type = pi2::BACK; rcvCmd.m1speed = -15; rcvCmd.m2speed = -15; cmdQueue.putCommand(rcvCmd); } else if( cmd == "left"){ rcvCmd.type = pi2::LEFT; rcvCmd.m1speed = 0; rcvCmd.m2speed = 8; cmdQueue.putCommand(rcvCmd); } else if( cmd == "right"){ rcvCmd.type = pi2::RIGHT; rcvCmd.m1speed = 8; rcvCmd.m2speed = 0; cmdQueue.putCommand(rcvCmd); } else if( cmd == "stop"){ rcvCmd.type = pi2::STOP; rcvCmd.m1speed = 0; rcvCmd.m2speed = 0; cmdQueue.putCommand(rcvCmd); } else if( cmd == "conndone"){ rcvCmd.type = pi2::CONNDONE; rcvCmd.m1speed = 0; rcvCmd.m2speed = 0; cmdQueue.putCommand(rcvCmd); } else{ rcvCmd.type = pi2::NOCOMMAND; rcvCmd.m1speed = 0; rcvCmd.m2speed = 0; cmdQueue.putCommand(rcvCmd); } stream->send(line, len); } delete stream; } } } }
void EnvRun(string ip, uint16_t port, int logLevel) #endif /* ANDROID */ { //1. Initialize the logger logLevel = logLevel < 0 ? 0 : logLevel; logLevel = logLevel >= _FINEST_ ? _FINEST_ : logLevel; Logger::Init(); BaseLogLocation *pLogLocation = NULL; #ifdef ANDROID pLogLocation = new LogCatLogLocation(); pLogLocation->SetLevel(logLevel); Logger::AddLogLocation(pLogLocation); #else pLogLocation = new ConsoleLogLocation(true); pLogLocation->SetLevel(logLevel); Logger::AddLogLocation(pLogLocation); #endif /* ANDROID */ #ifdef BUILD_SIGNATURE //1.1 Create the settings string FATAL("Build signature:\n%s", BUILD_SIGNATURE); #endif /* BUILD_SIGNATURE */ //2. Create the default protocol factory DefaultProtocolFactory *pFactory = new DefaultProtocolFactory(); if (!ProtocolFactoryManager::RegisterProtocolFactory(pFactory)) { ASSERT("Unable to register default protocols factory"); } //3. Initialize the I/O IOHandlerManager::Initialize(); //4. Create the AppleStreamingClientApplication application Variant configuration; configuration[CONF_APPLICATION_NAME] = "applestreaminclient"; configuration[CONF_APPLICATION_ALIASES].PushToArray("asc"); configuration[CONF_APPLICATION_DEFAULT] = (bool)true; configuration[CONF_APPLICATION_VALIDATEHANDSHAKE] = (bool)true; configuration[CONF_APPLICATION_KEYFRAMESEEK] = (bool)true; configuration[CONF_APPLICATION_CLIENTSIDEBUFFER] = (int32_t) 15; configuration[CONF_APPLICATION_SEEKGRANULARITY] = 1.00; configuration[CONF_APPLICATION_MEDIAFOLDER] = "./"; configuration[CONF_APPLICATION_GENERATE_META_FILES] = (bool)false; configuration["rtspHost"] = format("rtsp://127.0.0.1:%hu/", port); AppleStreamingClientApplication *pApp = new AppleStreamingClientApplication( configuration); #ifdef ANDROID pApp->SetJavaCallBackInterface(ci); #endif /* ANDROID */ if (!pApp->Initialize()) { ASSERT("Unable to initialize the application"); } //6. Register it to the app manager if (!ClientApplicationManager::RegisterApplication(pApp)) { ASSERT("Unable to register application"); } //7. Create the RTSP acceptor Variant acceptorConfig; acceptorConfig[CONF_IP] = ip; acceptorConfig[CONF_PORT] = (uint16_t) port; acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTSP; vector<uint64_t> chain; chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]); if (chain.size() == 0) { ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL])); } TCPAcceptor *pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain); if (!pAcceptor->StartAccept(pApp)) { ASSERT("Unable to fire up acceptor"); } //8. Create the bin variant acceptor acceptorConfig[CONF_PORT] = (uint16_t) (port + 1); acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_BIN_VARIANT; chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]); if (chain.size() == 0) { ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL])); } pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain); if (!pAcceptor->StartAccept(pApp)) { ASSERT("Unable to fire up acceptor"); } //9. Create the xml variant acceptor acceptorConfig[CONF_PORT] = (uint16_t) (port + 2); acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_XML_VARIANT; chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]); if (chain.size() == 0) { ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL])); } pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain); if (!pAcceptor->StartAccept(pApp)) { ASSERT("Unable to fire up acceptor"); } #ifdef HAS_PROTOCOL_RTMP //9. Create the RTMP acceptor acceptorConfig[CONF_PORT] = (uint16_t) (1935); acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTMP; chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]); if (chain.size() == 0) { ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL])); } pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain); if (!pAcceptor->StartAccept(pApp)) { ASSERT("Unable to fire up acceptor"); } #endif /* HAS_PROTOCOL_RTMP */ //10. Create the timer UDP protocol #ifdef HAS_MS_TIMER acceptorConfig[CONF_PORT] = (uint16_t) (port + 3); acceptorConfig[CONF_PROTOCOL] = "fineTimer"; acceptorConfig["FineTimerPeriod"] = 0.2; chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]); if (chain.size() == 0) { ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL])); } UDPCarrier *pUDPCarrier = UDPCarrier::Create(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT]); if (pUDPCarrier == NULL) { ASSERT("Unable to bind on udp://%s:%hu", STR(acceptorConfig[CONF_IP]), (uint16_t) acceptorConfig[CONF_PORT]); } BaseProtocol *pTimer = ProtocolFactoryManager::CreateProtocolChain(chain, acceptorConfig); pTimer->GetFarEndpoint()->SetIOHandler(pUDPCarrier); pUDPCarrier->SetProtocol(pTimer->GetFarEndpoint()); pApp->SetFineTimerId(pTimer->GetId()); #endif /* HAS_MS_TIMER */ inet_aton("127.0.0.1", &gAddress.sin_addr); memset(&gAddress, 0, sizeof (gAddress)); gAddress.sin_family = AF_INET; gAddress.sin_port = EHTONS(port + 1); //10. Run while (IOHandlerManager::Pulse()) { IOHandlerManager::DeleteDeadHandlers(); ProtocolManager::CleanupDeadProtocols(); } //11. Shutting down protocols manager; ProtocolManager::Shutdown(); ProtocolManager::CleanupDeadProtocols(); //12. Shutting down I/O handlers manager IOHandlerManager::ShutdownIOHandlers(); IOHandlerManager::DeleteDeadHandlers(); IOHandlerManager::Shutdown(); //13. Unregister and delete default protocol handler ProtocolFactoryManager::UnRegisterProtocolFactory(pFactory); delete pFactory; pFactory = NULL; //14. Shutting down applications ClientApplicationManager::Shutdown(); //15. Shutting down the logger leaving you in the dark. Bye bye Logger::Free(true); }