int main(void) { SystemInit(); userInit(); singleKey[0] = keyInitOne(0,RCC_APB2Periph_GPIOB, GPIOB, GPIO_Pin_10, key1ShortPress, key1LongPress); keys.keyNum++; singleKey[1] = keyInitOne(1,RCC_APB2Periph_GPIOA, GPIOA, GPIO_Pin_8, key2ShortPress, key2LongPress); keys.keyNum++; keys.singleKey = (keyTypedef_t *)&singleKey; keyParaInit(&keys); gizwitsInit(); ZB_HA_Init(); #if EN_DEBUG > 0 printf("Gokit Init Success \r\n"); #endif while(1) { watchdogFeed(); #if EN_HAM == 0 userHandle(); #endif gizwitsHandle((gizwitsReport_t *)&reportData); ZB_HA_Handle(); } }
int main(int argc, char* argv[]) { // GLUT initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); int screenWidth = glutGet(GLUT_SCREEN_WIDTH); int screenHeight = glutGet(GLUT_SCREEN_HEIGHT); windowWidth = 1280; windowHeight = 720; glutInitWindowSize(windowWidth, windowHeight); glutInitWindowPosition((screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2); glutCreateWindow("Solar System"); // Register callbacks glutDisplayFunc(display); glutReshapeFunc(reshape); // callback when the window is resized glutKeyboardFunc(keyboard); glutSpecialFunc(move); glutMouseFunc(mouse); glutPassiveMotionFunc(motion); // callback when the mouse is moving glutIdleFunc(idle); // idle-time callback // init and check GLEW, version, extensions if (!initExtensions()){ printf("Failed to init extensions.\n"); return 0; } // create and compile shaders/program if (!initShaders("shaders/trackball.vert", "shaders/trackball.frag")){ printf("Failed to init program and shaders\n"); return 0; } // user initialization if (!userInit()){ printf("Failed to userInit()\n"); return 0; } // Start rendering loop //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glutMainLoop(); return 0; }
int addUsers(LUser* luser,char* nick,char* pass){ if(luser->numUser >= MAXUSER){ return -1; } int found = 0; int i = 0; while(i < luser->numUser && found == 0){ if(strcmp(nick,luser->listU[i]->nick) == 0){ found = 1; } i++; } if(found == 1){ return -2; } User *usr = userInit(nick,pass); luser->listU[luser->numUser] = usr; luser->numUser++; if(DEBUG_MODE)printf("addUser -> Usuario %s creado en memoria\n",nick); return 0; }
// Main program entry point int main(int argc, char **argv) { for (int i = 0; i < (1 + MAX_PLAYERS); i++) threadsFree[i] = -1; setProgName(argv[0]); debugDisable(); infoPrint("Server Gruppe 01"); userInit(); // Create PID lock file or exit if it already exists debugPrint("Making sure we're running only once..."); if (singleton(LOCKFILE) != 0) return 1; // Socket structure that could be modified by the command line arguments struct sockaddr_in server; server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(PORT); // Use the default port defined in "common"! // Prepare parsing the command line options using getopt() debugPrint("Parsing command line options..."); const char* short_options = "hvp:"; struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "verbose", no_argument, 0, 'v' }, { "port", required_argument, 0, 'p' }, { NULL, 0, NULL, 0 } }; // Actual getopt() loop int option_index = 0; int loop = 1; while (loop != 0) { int c = getopt_long(argc, argv, short_options, long_options, &option_index); switch (c) { // Show help text when using -h case 'h': show_help(); exit(1); break; // Enable verbose (debug) output case 'v': debugEnable(); break; // Set port to listen on case 'p': if(optarg) { if (isOnlyDigits(optarg)) { server.sin_port = htons(atoi(optarg)); } else { errorPrint("Port has to be a decimal number!"); exit(1); } } break; // Unknown option, show error & help message default: case '?': errorPrint("Option not implemented yet -- %s (%c)", argv[optind-1], c); show_help(); exit(1); break; // All options have been parsed case -1: loop = 0; break; } } infoPrint("Serverport: %i", ntohs(server.sin_port)); // Create the pipes that will be used to communicate with the loader debugPrint("Creating Pipes..."); if (!createPipes()) return 1; // Actually fork and run the loader as child process debugPrint("Forking to run loader..."); if (!forkLoader()) return 1; debugPrint("Starting Threads..."); // Start the score agent thread if (pthread_create(&threads[0], NULL, scoreThread, NULL) != 0) { threadsFree[0] = -2; errnoPrint("pthread_create"); return 1; } clientInit(); // Create the listening socket debugPrint("Creating socket..."); int listen_socket = socket(AF_INET, SOCK_STREAM, 0); if (listen_socket == -1) { errnoPrint("socket"); return 1; } // Enable address reusing, so we don't get bind errors int on = 1; setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); // Register our Interrupt Signal handler signal(SIGINT, intHandler); // Store the listening socket in our user database userSetMainSocket(listen_socket); // Actually bind the listening socket if (bind(listen_socket, (struct sockaddr*) &server, sizeof(struct sockaddr_in)) == -1) { errnoPrint("bind"); close(listen_socket); closePipes(); loaderCloseSharedMemory(); return 1; } // Prepare the pselect() timeout data structure fd_set fds; struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = SOCKET_TIMEOUT * 1000000; sigset_t blockset; sigfillset(&blockset); sigdelset(&blockset, SIGINT); // Main thread main loop, waiting for new connections debugPrint("Waiting for connections..."); while (getRunning()) { // Listen to the listening socket if (listen(listen_socket, MAX_QUERYS) == -1) { errnoPrint("listen"); close(listen_socket); closePipes(); loaderCloseSharedMemory(); cleanCategories(); return 1; } // Wait for activity on the listening socket FD_ZERO(&fds); FD_SET(listen_socket, &fds); int retval = pselect(listen_socket + 1, &fds, NULL, NULL, &ts, &blockset); if (retval == -1) { if (errno == EINTR) { // We should exit because the user pressed Ctrl + C close(listen_socket); closePipes(); loaderCloseSharedMemory(); cleanCategories(); return 0; } else { // pselect encountered an error! errnoPrint("select"); close(listen_socket); closePipes(); loaderCloseSharedMemory(); cleanCategories(); return 1; } } else if (retval == 0) { // Nothing happened and pselect timed out continue; } else { // We have a new connection, accept it struct sockaddr_in remote_host; socklen_t sin_size = sizeof(struct sockaddr_in); int client_socket = accept(listen_socket, (struct sockaddr *) &remote_host, &sin_size); if (client_socket == -1) { errnoPrint("accept"); close(listen_socket); closePipes(); loaderCloseSharedMemory(); cleanCategories(); return 1; } debugPrint("Got a new connection! Performing Login..."); loginHandleSocket(client_socket); } } // Clean up behind ourselves close(listen_socket); closePipes(); loaderCloseSharedMemory(); cleanCategories(); pthread_exit(NULL); return 0; }
int main(int argc, char *argv[]) { setlocale(LC_ALL, "Russian"); char buff[20*1024]; int bytes_recv; printf("TCP DEMO CLIENT \n"); if (WSAStartup(0x202, (WSADATA * )&buff[0])) { printf("WSAStart error %d\n", WSAGetLastError()); return -1; } SOCKET my_sock; my_sock = socket(AF_INET, SOCK_STREAM, 0); if (my_sock < 0) { printf("Socket() error %d\n", WSAGetLastError()); return -1; } sockaddr_in dest_addr; dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(PORT); HOSTENT *hst; if(inet_addr(SERVERADDR) != INADDR_NONE) dest_addr.sin_addr.s_addr = inet_addr(SERVERADDR); else if(hst = gethostbyname(SERVERADDR)) ((unsigned long *)&dest_addr.sin_addr)[0] = ((unsigned long **)hst->h_addr_list)[0][0]; else { printf("Invalid address %s\n", SERVERADDR); closesocket(my_sock); WSACleanup(); return -1; } if(connect(my_sock, (sockaddr *)&dest_addr, sizeof(dest_addr))) { printf("Connect error %d\n", WSAGetLastError()); return -1; } printf("Соединение с %s успешно установлено\n\ Type quit for quit \n\n", SERVERADDR); bytes_recv = recv(my_sock, &buff[0], sizeof(buff), 0); buff[bytes_recv] = 0; printf("%s \n", buff); UserName = userInit(my_sock); DWORD thID1; DWORD thID2; CreateThread(NULL, NULL, sendProcess, &my_sock, NULL, &thID1); CreateThread(NULL, NULL, recvProcess, &my_sock, NULL, &thID2); while (true) { if(sendOver && recvOver) { printf("Program is closed"); break; } Sleep(1000); } closesocket(my_sock); WSACleanup(); return 0; }