void Bridge::checkNewConnections() { if (FD_ISSET(main_socket, &readset)) { int options, cd; addrinfo hints = getHints(AI_CANONNAME | AI_PASSIVE); addrinfo *serv_info; sockaddr_in client_addr; socklen_t calen = sizeof(client_addr); if ((cd = accept(main_socket, (sockaddr* ) &client_addr, &calen)) == FAILURE) { cerr << "Connection failed to connect" << endl; exit(1); } if ((options = fcntl(cd, F_GETFL)) == FAILURE) { cerr << "Failed to get new socket connection options" << endl; exit(1); } options |= O_NONBLOCK; if (fcntl(cd, F_SETFL, options) == FAILURE) { cerr << "Failed to set new socket connection options" << endl; exit(1); } if (getpeername(cd, (sockaddr *)&client_addr, &calen) == FAILURE) { cerr << "No socket found!" << endl; exit(1); } if (getaddrinfo(ipv4_2_str(client_addr.sin_addr.s_addr).c_str(), NULL, &hints, &serv_info) == FAILURE) { cerr << "Failed to get address info " << endl; exit(1); } if (current_ports < max_ports) { current_ports++; if (DebugON) cout <<"PORTS AVAILABLE: " << max_ports - current_ports << endl; cout << "connect from '" + string(serv_info->ai_canonname) + "' at '" + to_string(ntohs(client_addr.sin_port)) + "'" << endl; connected_ifaces[to_string(ntohs(client_addr.sin_port))] = cd; if (DebugON) printConnections(); string response("accept"); write(cd, response.c_str(), response.length()); } else { string response("reject"); write(cd, response.c_str(), response.length()); close(cd); } freeaddrinfo(serv_info); } }
void exportState(int iteration, owConfigProperty * config, float * position, float * connections, float * velocity, int * membranes, float * muscleActivationSignal) { std::string filename = config->getLoadPath() + std::string("state_") + to_string(iteration, 8, '0') + std::string(".vtp"); std::ofstream outFile(filename.c_str()); if (!outFile) { throw std::runtime_error("Cannot create VTK file."); } if (numConns == -1) { /* Initialization of variables */ countExistingConnections(connections, config); setEndianness(); } outFile << "<VTKFile type=\"PolyData\" version=\"0.1\"" << " byte_order=" << (isBigEndian ? "\"BigEndian\"" : "\"LittleEndian\"") << ">\n"; outFile << "<PolyData>\n"; outFile << "<Piece" << " NumberOfPoints=\"" << config->getParticleCount() << "\"" << " NumberOfPolys=\"" << config->numOfMembranes << "\"" << " NumberOfLines=\"" << numConns << "\"" << ">\n"; printParticles(outFile, position, velocity, config); printMembranes(outFile, membranes, config); printConnections(outFile, connections, config); outFile << "<CellData>\n"; printCellTypes(outFile, config); printMuscleNumbers(outFile, connections, config); printMuscleActivation(outFile, connections, muscleActivationSignal, config); outFile << "</CellData>\n"; outFile << "</Piece>\n"; outFile << "</PolyData>\n"; outFile << "</VTKFile>\n"; outFile.close(); }
bool Bridge::checkExitServer() { if (FD_ISSET(KEYBOARD, &readset)) //keyboard { string input; getline(cin, input); if (input == "debug") { DebugON = !DebugON; } if (input == "exit") { return true; } if (input == "ttl") { printConnections(); } } return false; }
// // Draw_NetworkScreen // void Draw_NetworkScreen(void) { const float box_x_min = 50.0f; const float box_x_max = 500.0f; const float box_y_min = 50.0f; const float box_y_max = 440.0f; const float msg_y = box_y_max - 32.0f; char buffer[80]; glDisable(GL_TEXTURE_2D); // Draw a blue screen // in the background glColor3ub(20, 120, 235); glLineWidth(3.0f); glBegin(GL_QUADS); glVertex3f(box_x_min, box_y_min, 0.0f); glVertex3f(box_x_max, box_y_min, 0.0f); glVertex3f(box_x_max, box_y_max, 0.0f); glVertex3f(box_x_min, box_y_max, 0.0f); glEnd(); // draw outline around object glColor3ub(255, 255, 255); glBegin(GL_LINE_LOOP); glVertex3f(box_x_min, box_y_min, 0.0f); glVertex3f(box_x_max, box_y_min, 0.0f); glVertex3f(box_x_max, box_y_max, 0.0f); glVertex3f(box_x_min, box_y_max, 0.0f); glEnd(); // The next line // will be used for the message screen glBegin(GL_LINE_LOOP); glVertex3f(box_x_min, msg_y, 0.0f); glVertex3f(box_x_max, msg_y, 0.0f); glEnd(); glLineWidth(1.0f); // before drawing the name, save selection Display_NetSel(network_str_); // Print the actual text Setup_NetMenu(CLIENT_NET_MENU, END_CLIENT_NET, VERT_TYPE_); Setup_NetMenu(C_HORZ_MENU, END_CLIENT_NET, HORZ_TYPE_); Setup_NetMenu(N_TITLE_X, N_TITLE_YY2, SERVER_SETTINGS); Setup_NetMenu(SERVER_NET_MENU, END_SERVER_NET, VERT_TYPE_); Setup_NetMenu(S_SERV_HORZ, E_SERV_HORZ, HORZ_TYPE_); // Draw the static text TextBegin(network_text); DrawString(network_text, T_COL_6, T_ROW_1, network_str_[_TEXT_CONNECT_]); TextEnd(); TextBegin(network_text); DrawString(network_text, T_COL_7, T_ROW_1, network_str_[_TEXT_DISCONNECT_]); TextEnd(); // Server buttons -- TextBegin(network_text); DrawString(network_text, T_COL_6, T_ROW_2, network_str_[_TEXT_START_]); TextEnd(); TextBegin(network_text); DrawString(network_text, T_COL_7, T_ROW_2, network_str_[_TEXT_SHUTDOWN_]); TextEnd(); // And at the bottom // the network and help screens and messages TextBegin(network_text); // get network message Get_NetworkMsg(buffer); DrawString(network_text, TEXT_HORZ_START, T_ROW_3, buffer); TextEnd(); // print connections if there are any printConnections(); } // end of the functino
int _tmain(int argc, _TCHAR* argv[]) { // Get the command line options WCHAR *lPort, *rPort, *rAddr, *targetName = NULL; int i = 1; argv[i] ? NULL : info(argv[0]); bool showTargetName = false; if (!_wcsicmp(argv[i], L"shT")) { showTargetName = true; i++; argv[i] ? NULL : info(argv[0]); } if (!_wcsicmp(argv[i], L"setT")) { (targetName = argv[++i]) ? NULL : info(argv[0]); i++; argv[i] ? NULL : info(argv[0]); } if (!_wcsicmp(argv[i], L"lp")) { (lPort = argv[++i]) ? NULL : info(argv[0]); (rAddr = argv[++i]) ? NULL : info(argv[0]); (rPort = argv[++i]) ? NULL : info(argv[0]); } else { lPort = L"3128"; (rAddr = argv[i++]) ? NULL : info(argv[0]); (rPort = argv[i++]) ? NULL : info(argv[0]); } if (WSAInit()) return -1; //---------------------------------------------------------------------------------------------------- ADDRINFOT hints, *remoteAddr = NULL, *inetAddr = NULL; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; int iResult = GetAddrInfo(rAddr, rPort, &hints, &remoteAddr); if (iResult != 0) { switch (iResult) { case WSAHOST_NOT_FOUND: wprintf(L"Host not found\n"); break; case WSANO_DATA: wprintf(L"No data record found\n"); break; default: wprintf(L"Gethost function failed with error: %i\n", iResult); } return -1; } WCHAR hostName[NI_MAXHOST]; iResult = GetNameInfo(remoteAddr->ai_addr, sizeof(SOCKADDR), hostName, NI_MAXHOST, NULL, 0, NI_NAMEREQD); if (iResult != 0) { switch (iResult) { case EAI_AGAIN: wprintf(L"The name can not be determined at the moment\n"); break; case EAI_BADFLAGS: wprintf(L"The flags parameter has an invalid value\n"); break; case EAI_FAIL: wprintf(L"Name info failed\n"); break; case EAI_FAMILY: wprintf(L"It does not recognize the address family\n"); break; case EAI_MEMORY: wprintf(L"Not enough memory\n"); break; case EAI_NONAME: wprintf(L"The name can not be determined\n"); break; default: wprintf(L"Nameinfo function failed with error: %i\n", iResult); } return -1; } if (!targetName) { targetName = buildTargetName(hostName); } if (showTargetName) { wprintf(L"Target Name: '%s'\n", targetName); } // Create a listening socket SOCKET ListenSocket = newSock(); if (ListenSocket == INVALID_SOCKET) return -1; // Listen address memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; iResult = GetAddrInfo(NULL, lPort, &hints, &inetAddr); if (iResult != 0) { switch (iResult) { case WSAHOST_NOT_FOUND: wprintf(L"Host not found\n"); break; case WSANO_DATA: wprintf(L"No data record found\n"); break; default: wprintf(L"Gethost function failed with error: %i\n", iResult); } return -1; } //------------------------- // Bind the listening socket iResult = bind(ListenSocket, inetAddr->ai_addr, sizeof(SOCKADDR)); if (iResult != 0) { wprintf(L"bind failed with error: %i\n", WSAGetLastError()); return -1; } //------------------------- // Create a new event WSAEVENT NewEvent; if (createEv(&NewEvent)) return -1; //------------------------- // Associate event types FD_ACCEPT and FD_CLOSE // with the listening socket and NewEvent iResult = WSAEventSelect(ListenSocket, NewEvent, FD_ACCEPT); if (iResult != 0) { wprintf(L"WSAEventSelect failed with error: %i\n", WSAGetLastError()); return -1; } //------------------------- // Start listening on the socket iResult = listen(ListenSocket, 0); if (iResult != 0) { wprintf(L"listen failed with error: %i\n", WSAGetLastError()); return -1; } wprintf(L"NegotiateProxy listen on port %s\n", lPort); //------------------------- // Add the socket and event to the arrays, increment number of events SOCKET SocketArray[WSA_MAXIMUM_WAIT_EVENTS]; WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS]; DWORD EventTotal = 0; SocketArray[EventTotal] = ListenSocket; EventArray[EventTotal++] = NewEvent; //bool needAuth[WSA_MAXIMUM_WAIT_EVENTS]; //memset(needAuth, true, sizeof(needAuth)); bool toProxy; DWORD secondIndex; DWORD index; SOCKET ClientSocket, ProxySocket; WSANETWORKEVENTS NetworkEvents; // The buffers for the reception - transmission //char prBuff[BUFF_SIZE], clBuff[BUFF_SIZE]; int connections = 0; char* previousMessagesArray[WSA_MAXIMUM_WAIT_EVENTS]; memset(previousMessagesArray, NULL, WSA_MAXIMUM_WAIT_EVENTS); for (int i = 0; i < WSA_MAXIMUM_WAIT_EVENTS; previousMessagesArray[i] = NULL, i++) {} while (1) { // Wait for network events on all sockets index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, FALSE); index -= WSA_WAIT_EVENT_0; if ((index != WSA_WAIT_FAILED) && (index != WSA_WAIT_TIMEOUT)) { WSAEnumNetworkEvents(SocketArray[index], EventArray[index], &NetworkEvents); WSAResetEvent(EventArray[index]); toProxy = (index % 2) ? true : false; secondIndex = toProxy ? index + 1 : index - 1; if ((NetworkEvents.lNetworkEvents & FD_ACCEPT) && (NetworkEvents.iErrorCode[FD_ACCEPT_BIT] == 0)) { ClientSocket = accept(ListenSocket, NULL, NULL); if (INVALID_SOCKET == ClientSocket) wprintf(L"Invalid socket\n"); ProxySocket = socket(AF_INET, SOCK_STREAM, 0); if (INVALID_SOCKET == ProxySocket) wprintf(L"INVALID_SOCKET ERROR!!!\n"); iResult = connect(ProxySocket, remoteAddr->ai_addr, sizeof(SOCKADDR)); if (iResult != 0) { wprintf(L"Connection failed with error: %d\n", WSAGetLastError()); return -1; } printConnections(++connections); if (createEv(&NewEvent)) return -1; SocketArray[EventTotal] = ClientSocket; EventArray[EventTotal] = NewEvent; //------------------------- // Associate event types FD_READ and FD_CLOSE // with the client socket and NewEvent iResult = WSAEventSelect(SocketArray[EventTotal], EventArray[EventTotal], FD_READ | FD_CLOSE); if (iResult != 0) { wprintf(L"WSAEventSelect failed with error: %d\n", WSAGetLastError()); return -1; } EventTotal++; if (createEv(&NewEvent)) return -1; SocketArray[EventTotal] = ProxySocket; EventArray[EventTotal] = NewEvent; //------------------------- // Associate event types FD_READ and FD_CLOSE // with the proxy socket and NewEvent iResult = WSAEventSelect(SocketArray[EventTotal], EventArray[EventTotal], FD_READ | FD_CLOSE); if (iResult != 0) { wprintf(L"WSAEventSelect failed with error: %d\n", WSAGetLastError()); return -1; } EventTotal++; } if ((NetworkEvents.lNetworkEvents & FD_READ) && (NetworkEvents.iErrorCode[FD_READ_BIT] == 0)) { // transfers data iResult = transmit( &SocketArray[index], &SocketArray[secondIndex], toProxy, previousMessagesArray, index, targetName, hostName, remoteAddr->ai_addr, &EventArray[index]); if (iResult < 0) return -1; } if (NetworkEvents.lNetworkEvents & FD_CLOSE) { closesocket(SocketArray[secondIndex]); closesocket(SocketArray[index]); WSACloseEvent(EventArray[secondIndex]); WSACloseEvent(EventArray[index]); // Move from the top to the free place SocketArray[index] = toProxy ? SocketArray[EventTotal - 2] : SocketArray[EventTotal - 1]; SocketArray[secondIndex] = toProxy ? SocketArray[EventTotal - 1] : SocketArray[EventTotal - 2]; EventArray[index] = toProxy ? EventArray[EventTotal - 2] : EventArray[EventTotal - 1]; EventArray[secondIndex] = toProxy ? EventArray[EventTotal - 1] : EventArray[EventTotal - 2]; EventTotal -= 2; printConnections(--connections); } } } return 0; }