int main(int argc, char** argv) { // Initialize ROS node "sm_talker" ros::init(argc, argv, "sm_listener"); // Required to start timers for non-node ROS use. ros::Time::init(); // Little message to know she's started ROS_INFO_STREAM("STARTING SM_MANAGER"); // Create and execute manager // * Create a TCP server connection on TCP_PORT (see common.h) // * Initialize manager using server connection // * Initialize handler using server connection // * Add handler to manager // * Execute manager spin loop TcpServer server; server.init(TCP_PORT); MessageManager manager; MyHandler handler; manager.init(&server); // Handler initilaized with reply connection (typically the same as // incoming connection but now always) handler.init(&server); manager.add(&handler); manager.spin(); return 0; }
// Message passing routine, used to send and receive a typed message // Useful for checking the packing and unpacking of message data. void fakeMessagePassing(TypedMessage &send, TypedMessage &recv) { const int tcpPort = TEST_PORT_BASE+401; char ipAddr[] = "127.0.0.1"; TcpClient tcpClient; TcpServer tcpServer; SimpleMessage msgSend, msgRecv; send.toTopic(msgSend); // Construct server tcpServer.init(tcpPort); // Construct a client tcpClient.init(&ipAddr[0], tcpPort); tcpClient.makeConnect(); // make tcp client connection tcpServer.makeConnect(); // make tcp server connection tcpClient.sendMsg(msgSend); // send message tcpServer.receiveMsg(msgRecv); // physically receive message recv.init(msgRecv); // copy received message into reference }
void startTcpServer() { cout << "TCP Server[" << LOCATION_ADRR << ":" << UDPSERVER_PORT << "] Thread[" <<CurrentThread::info.tid() << "] Start!" << endl; TcpServer *pTcpServer = new TcpServer(); CHECK_NULLPTR_NORET(pTcpServer); CHECK_RETURN_VAL_NORET(pTcpServer->init(LOCATION_ADRR, UDPSERVER_PORT)); Uint32 addrLen = 0; Int32 acceptFd = -1; struct sockaddr_in clientAddr; while(1) { bzero(&clientAddr, sizeof(clientAddr)); addrLen = sizeof(sockaddr); acceptFd = pTcpServer->sockAccept((sockaddr*)&clientAddr, &addrLen); thread t(tcpServerHandle, pTcpServer, inet_ntoa((&clientAddr)->sin_addr), ntohs((&clientAddr)->sin_port), acceptFd); t.detach(); } delete pTcpServer; cout << "TCP Server[" << LOCATION_ADRR << ":" << UDPSERVER_PORT << "] Thread[" <<CurrentThread::info.tid() << "] End!" << endl; }