void udp_event(void) { #if USE_UDP0 udp_receive(&udp0); #endif // USE_UDP0 #if USE_UDP1 udp_receive(&udp1); #endif // USE_UDP1 #if USE_UDP2 udp_receive(&udp2); #endif // USE_UDP2 }
void receive_tweet(char* buffer, struct sockaddr_in* server) { struct sockaddr_in client; int transfered = 0, timeoutcount = 0; /* Get message from message server */ if (!udp_send(sock, "Gimmeh!", server, sizeof(struct sockaddr_in))) error_and_exit("Cannot send message to server", __FILE__, __LINE__); do { transfered = udp_receive(sock, buffer, BUFSIZE, &client, sizeof(client)); if (transfered == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { ++timeoutcount; if (timeoutcount >= MAX_TIMEOUTS) { printf("Maximum timeouts exceeded. Server is considered dead."); exit(0); } printf("Server timed out (%d).\n", timeoutcount); } error_and_exit("Cannot read from server", __FILE__, __LINE__); } else if (transfered == 0) { error_and_exit("Server has shut down", __FILE__, __LINE__); } } while (transfered <= 0); if (server->sin_addr.s_addr != client.sin_addr.s_addr) error_and_exit("Received message from unexpected server", __FILE__, __LINE__); }
int main(int *argc, char argv[]) { char sendBuf[100]; char receiveBuf[100]; struct timespec waitTime; clock_gettime(CLOCK_REALTIME, &waitTime); udp_init_client(&conn, 9999, "192.168.0.1"); //VARIABLES FOR REGULATING double error, integral, u, y; double reference = 1; double Kp = 10; double Ki = 800; double period = PERIOD; strncpy(sendBuf,"START", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); //strlen(sendBuf)+1 //strncpy(sendBuf,"SET:12.3456",sizeof(sendBuf)); //udp_send(&conn,sendBuf,sizeof(sendBuf)); int numRep = RUNTIME/PERIOD; int i; for ( i = 0 ; i < numRep ; i++ ) { timespec_add_us(&waitTime,PERIOD*1000000); clock_nanosleep(&waitTime); strncpy(sendBuf,"GET", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); udp_receive(&conn,receiveBuf,sizeof(receiveBuf)); char * numVal = receiveBuf + 8; y = atof(numVal); //printf("Y-value from server is %f\n",y); //WE HAVE A Y-VALUE, DO PI REGULATING error = reference - y; integral = integral + (error * period); u = Kp * error + Ki * integral; setU(u); } strncpy(sendBuf,"STOP", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); return 0; }
void* client_receiver(void * params) { while(running) { struct line *buf = (struct line*) malloc(sizeof(struct line)); udp_receive((char *)buf, sizeof(struct line)); screen_out_add(buf); screen_refresh(); } return NULL; }
int main(int argc, char** argv) { struct sockaddr_in server; struct sockaddr_in client; char buffer[BUFSIZE]; socklen_t len_client; int transfered = 0; char* msg; pthread_t thread_tweet_fetcher; if (argc < 2) { printf("USAGE: %s [port]\n", argv[0]); return 0; } atexit(at_exit); set_signal_handlers(); T = twitter_new(); printf("Reading tweets...\n"); fetch_tweets(T, 15); pthread_create(&thread_tweet_fetcher, NULL, thread_fetch_tweets, NULL); printf("Opening server...\n"); sock = udp_create_socket(&server, sizeof(server), htonl(INADDR_ANY), atoi(argv[1]), 0); if (sock < 0) error_and_exit("cannot create socket", __FILE__, __LINE__); if (bind(sock, (struct sockaddr*)&server, sizeof(server)) < 0) error_and_exit("Cannot bind to socket", __FILE__, __LINE__); len_client = sizeof(client); for (;;) { transfered = udp_receive(sock, buffer, BUFSIZE, &client, len_client); switch (transfered) { case -1: error_and_exit("Failed to receive message", __FILE__, __LINE__); case 0: error_and_exit("Client has shut down...", __FILE__, __LINE__); } msg = get_tweet(T); transfered = udp_send(sock, msg, &client, sizeof(client)); free(msg); if (!transfered) error_and_exit("Sending has failed!", __FILE__, __LINE__); } close(sock); return 0; }
int main(int argc, char *argv[]) { /* Argument processing and help */ if (argc != 2) { printf("Use: %s <ip>\n", argv[0]); return -1; } /*---------------------------Preparing Stuff------------------------------*/ /*RIP packet declaration*/ rip_packet rip_request; int rip_request_size = size_of_packet_for_rip_entries(1); /*One request*/ /*RIP packet filling*/ rip_request.command = RIP_REQUEST; rip_request.version = RIP_VERSION; rip_request.zero = 0; printf("RIP Entry has a size of %d\n", (int)sizeof(rip_entry)); rip_request.rip_entries[0].metric = htonl(16); rip_request.rip_entries[0].family_id = 0x0000; print_rip_packet(&rip_request, rip_request_size); // memset(&(rip_request.rip_entries[0]), 0, 20); // rip_request.rip_entries[0].route_tag=0x2222; // memcpy(rip_request.rip_entries[0].ip, ip_to, 4); // memcpy(rip_request.rip_entries[0].mask, ip_to, 4); // memcpy(rip_request.rip_entries[0].nexthop, ip_to,4); /*---------------------------Sending Stuff--------------------------------*/ /*Preparing UDP frame*/ ipv4_addr_t ip_to; ipv4_str_addr(argv[1], ip_to); /*Open tables and whatnot*/ ipv4_open("ipv4_config.txt", "ipv4_route_table.txt"); /*SEEEEEEEEND*/ printf("[RIP] Enviando request RIP..\n"); udp_send(ip_to, RIP_PORT, 2000, (unsigned char*) &rip_request, rip_request_size); /*---------------------------Receiving Stuff------------------------------*/ /*Prepare a buffer and source IP for incoming UDP frame*/ unsigned char r_buffer[UDP_MAX_SIZE]; unsigned short int src_port; ipv4_addr_t source_ip; /*Receive*/ int bsize = udp_receive(source_ip, &src_port, 2000, r_buffer, INFINITE_TIMEOUT); /*print what we just received (a RIP packet supposedly)*/ print_rip_packet((rip_packet*) r_buffer, bsize); return 0; }
int main(int argc, const char *argv[]) { parse_ini("cfg.ini", ini_parser, NULL); int fd = create_server(1001, 1); if( fd == -1 ) return 0; while(1) { udp_receive(fd, packet_recvier); } return 0; }
int main(void) { int actuators_socket; actuators_mesg_t actuators_mesg; plant_inputs_t plant_inputs; if (OK != writer_init_plant_inputs_buffer()) { printf("In plant_server: error in call to "); printf("writer_init_plant_inputs_buffer\n"); } if (OK != udp_server_init(&actuators_socket, PLANT_ACTUATORS_PORT)) { printf("In plant_server: error in call to "); printf("udp_server_init\n"); return -1; } while (1) { if (OK != udp_receive(actuators_socket, &actuators_mesg, sizeof(actuators_mesg_t))) { printf("In plant_server: "); printf("error in call to "); printf("udp_receive\n"); } printf("%.3f %.3f %.3f %.3f\n", actuators_mesg.y19, actuators_mesg.y20, actuators_mesg.y21, actuators_mesg.y22); plant_inputs.y19 = actuators_mesg.y19; plant_inputs.y20 = actuators_mesg.y20; plant_inputs.y21 = actuators_mesg.y21; plant_inputs.y22 = actuators_mesg.y22; if (OK != write_plant_inputs_buffer(&plant_inputs)) { printf("In plant_server: "); printf("error in call to "); printf("write_plant_inputs_buffer\n"); } } return 0; }
/* llmnr responder */ void llmnr_udp_request(int conn) { struct sockaddr from; struct in_addr to; int in_len, out_len, from_len, name_len; from_len = sizeof(from); in_len = udp_receive(conn, &from, &from_len, &to); if (in_len < 13) return; if (in[3] & 0xf0) /* check for standard query */ return; name_len = (unsigned char)(in[12]); if (name_len >= 0xC0) return; if (in[12 + name_len + 3] != DNS_TYPE_A) return; if (strlen(cd_name) != name_len || strncasecmp(cd_name, in+13, name_len)) return; memcpy(out, in, in_len); out_len = in_len; out[2] = 0x80; /* response */ out[7] = 1; /* one answer */ out[out_len++] = 0xc0; /* referrral */ out[out_len++] = 12; /* first entry */ out[out_len++] = 0; /* type A */ out[out_len++] = DNS_TYPE_A; out[out_len++] = 0; /* class IN */ out[out_len++] = 1; out[out_len++] = 0; /* TTL */ out[out_len++] = 0; out[out_len++] = 0; out[out_len++] = 0; out[out_len++] = 0; /* Address length */ out[out_len++] = sizeof(to); memcpy(out+out_len, &to, sizeof(to)); out_len += sizeof(to); udp_send(conn, to, &from, from_len, out_len); }
void wsd_udp_request(int conn) { struct sockaddr from; struct in_addr to; int in_len, out_len, from_len; char recv_ip[32]; from_len = sizeof(from); in_len = udp_receive(conn, &from, &from_len, &to); if (in_len <= 0) return; strncpy(recv_ip, inet_ntoa(to), sizeof(recv_ip)); out_len = sizeof(out); if (handle_request(recv_ip, in, in_len, out, &out_len, 0) == 0) udp_send(conn, to, &from, from_len, out_len); }
void loop(int ipxs, int udps) { for (;;) { struct timeval wt = {60,0}; fd_set fds; int rc; FD_ZERO(&fds); FD_SET(ipxs,&fds); FD_SET(udps,&fds); rc = select(ipxs+udps,&fds,NULL,NULL,&wt); if (rc < 0) { fprintf(stderr,"select: %s\n", strerror(errno)); exit(-2); } if (rc>0) { if (FD_ISSET(ipxs,&fds)) ipx_receive(ipxs); if (FD_ISSET(udps,&fds)) udp_receive(udps); } } }
int main (int argc, char * argv []) { int rc; int linkno; rc = udp_create ("4500::4426", & linkno); if (rc < 0) { printf ("udp_create failed\n"); exit (1); } while (1) { #define psz 17000 uint16_t pkt [psz]; rc = udp_receive (linkno, pkt, psz); if (rc < 0) { printf ("udp_receive failed\n"); exit (1); } else if (rc == 0) { printf ("udp_receive 0\n"); sleep (1); } else { for (int i = 0; i < rc; i ++) { printf (" %06o %04x ", pkt [i], pkt [i]); for (int b = 0; b < 16; b ++) printf ("%c", pkt [i] & (1 << b) ? '1' : '0'); printf ("\n"); } } } }
void* UDP_listener() { printf("UDP says hello\n"); float f =0.0; //tinfo_output_t *y = (tinfo_output_t*) argpcon; char buf[MSG_LENGTH]; int err = pthread_mutex_init(&tinfo_connection.sendlock, NULL); if (err !=0) { printf("sendlock init failed\n"); } while(1){ int err = udp_receive(&tinfo_connection.conn, buf, MSG_LENGTH); if(err ==-1) { printf("Udp_receive failed\n"); } if (!strcmp("SIGNAL",buf)) { sem_post(&new_sig); } else { pthread_mutex_lock(&tinfo_output.lock); sscanf(buf, "GET_ACK:%f", &f); tinfo_output.y_val = f; pthread_mutex_unlock(&tinfo_output.lock); sem_post(&y_updt); } } }
void UDPlis ( void ){ char receiveBuf[100]; while(1){ if(udp_receive(&conn,receiveBuf,sizeof(receiveBuf))){ if(!strncmp(receiveBuf,"SI",2)){ pthread_mutex_lock(&newSIG_mut); newSIG = true; pthread_mutex_unlock(&newSIG_mut); } else { pthread_mutex_lock(&newPIval_mut); strcpy(globalMsgArray,receiveBuf); newPIval = true; pthread_mutex_unlock(&newPIval_mut); } } } }
void *udp_listen(void *arg) { char buf[BUF_LEN]; char *buf_number; int rx; printf("UDP listener thread initialized\n"); while (run_listen) { // Receive system state rx = udp_receive(&conn, buf, BUF_LEN); if (rx > 0) { if(strstr(buf, "GET_ACK:") == buf) { // Get new y value buf_number = &buf[strlen("GET_ACK:")]; pthread_mutex_lock(&y_mutex); y = atof(buf_number); pthread_mutex_unlock(&y_mutex); sem_post(&pi_sem); } #if WITH_SIGNAL else if(strstr(buf, "SIGNAL") == buf) { // Run signal_handler sem_post(&signal_sem); } #endif } } return 0; }
bool DTrack2::receive(void) { char* s; int i, j, k, l, n, len, id; char sfmt[20]; int iarr[3]; float f, farr[6]; int loc_num_bodycal, loc_num_handcal, loc_num_flystick1, loc_num_meatool; if(!valid()){ set_data_neterror(); return false; } // defaults: act_framecounter = 0; act_timestamp = -1; // i.e. not available loc_num_bodycal = loc_num_handcal = -1; // i.e. not available loc_num_flystick1 = loc_num_meatool = 0; // receive UDP packet: len = udp_receive(d_udpsock, d_udpbuf, d_udpbufsize-1, d_udptimeout_us); if(len == -1){ set_data_timeout(); return false; } if(len <= 0){ set_data_neterror(); return false; } s = d_udpbuf; s[len] = '\0'; // process lines: set_data_parseerror(); do{ // line for frame counter: if(!strncmp(s, "fr ", 3)){ s += 3; if(!(s = string_get_ui(s, &act_framecounter))){ // get frame counter act_framecounter = 0; return false; } continue; } // line for timestamp: if(!strncmp(s, "ts ", 3)){ s += 3; if(!(s = string_get_d(s, &act_timestamp))){ // get timestamp act_timestamp = -1; return false; } continue; } // line for additional information about number of calibrated bodies: if(!strncmp(s, "6dcal ", 6)){ s += 6; if(!(s = string_get_i(s, &loc_num_bodycal))){ // get number of calibrated bodies return false; } continue; } // line for standard body data: if(!strncmp(s, "6d ", 3)){ s += 3; for(i=0; i<act_num_body; i++){ // disable all existing data memset(&act_body[i], 0, sizeof(dtrack2_body_type)); act_body[i].id = i; act_body[i].quality = -1; } if(!(s = string_get_i(s, &n))){ // get number of standard bodies (in line) return false; } for(i=0; i<n; i++){ // get data of standard bodies if(!(s = string_get_block(s, "if", &id, &f))){ return false; } if(id >= act_num_body){ // adjust length of vector act_body.resize(id + 1); for(j=act_num_body; j<=id; j++){ memset(&act_body[j], 0, sizeof(dtrack2_body_type)); act_body[j].id = j; act_body[j].quality = -1; } act_num_body = id + 1; } act_body[id].id = id; act_body[id].quality = f; if(!(s = string_get_block(s, "fff", NULL, act_body[id].loc))){ return false; } if(!(s = string_get_block(s, "fffffffff", NULL, act_body[id].rot))){ return false; } } continue; } // line for Flystick data (older format): if(!strncmp(s, "6df ", 4)){ s += 4; if(!(s = string_get_i(s, &n))){ // get number of calibrated Flysticks return false; } loc_num_flystick1 = n; if(n != act_num_flystick){ // adjust length of vector act_flystick.resize(n); act_num_flystick = n; } for(i=0; i<n; i++){ // get data of Flysticks if(!(s = string_get_block(s, "ifi", iarr, &f))){ return false; } if(iarr[0] != i){ // not expected return false; } act_flystick[i].id = iarr[0]; act_flystick[i].quality = f; act_flystick[i].num_button = 8; k = iarr[1]; for(j=0; j<8; j++){ act_flystick[i].button[j] = k & 0x01; k >>= 1; } act_flystick[i].num_joystick = 2; // additionally to buttons 5-8 if(iarr[1] & 0x20){ act_flystick[i].joystick[0] = -1; }else if(iarr[1] & 0x80){ act_flystick[i].joystick[0] = 1; }else{ act_flystick[i].joystick[0] = 0; } if(iarr[1] & 0x10){ act_flystick[i].joystick[1] = -1; }else if(iarr[1] & 0x40){ act_flystick[i].joystick[1] = 1; }else{ act_flystick[i].joystick[1] = 0; } if(!(s = string_get_block(s, "fff", NULL, act_flystick[i].loc))){ return false; } if(!(s = string_get_block(s, "fffffffff", NULL, act_flystick[i].rot))){ return false; } } continue; } // line for Flystick data (newer format): if(!strncmp(s, "6df2 ", 5)){ s += 5; if(!(s = string_get_i(s, &n))){ // get number of calibrated Flysticks return false; } if(n != act_num_flystick){ // adjust length of vector act_flystick.resize(n); act_num_flystick = n; } if(!(s = string_get_i(s, &n))){ // get number of Flysticks return false; } for(i=0; i<n; i++){ // get data of Flysticks if(!(s = string_get_block(s, "ifii", iarr, &f))){ return false; } if(iarr[0] != i){ // not expected return false; } act_flystick[i].id = iarr[0]; act_flystick[i].quality = f; if(iarr[1] > DTRACK2_FLYSTICK_MAX_BUTTON){ return false; } if(iarr[1] > DTRACK2_FLYSTICK_MAX_JOYSTICK){ return false; } act_flystick[i].num_button = iarr[1]; act_flystick[i].num_joystick = iarr[2]; if(!(s = string_get_block(s, "fff", NULL, act_flystick[i].loc))){ return false; } if(!(s = string_get_block(s, "fffffffff", NULL, act_flystick[i].rot))){ return false; } strcpy(sfmt, ""); j = 0; while(j < act_flystick[i].num_button){ strcat(sfmt, "i"); j += 32; } j = 0; while(j < act_flystick[i].num_joystick){ strcat(sfmt, "f"); j++; } if(!(s = string_get_block(s, sfmt, iarr, act_flystick[i].joystick))){ return false; } k = l = 0; for(j=0; j<act_flystick[i].num_button; j++){ act_flystick[i].button[j] = iarr[k] & 0x01; iarr[k] >>= 1; l++; if(l == 32){ k++; l = 0; } } } continue; }
int recive(struct udp_conn *udp, char *buf, int len){ int res = 0; res = udp_receive(udp, buf,len); return res; }
int DTrack::receive_udp_ascii( unsigned long* framenr, double* timestamp, int* nbodycal, int* nbody, dtrack_body_type* body, int max_nbody, int* nflystick, dtrack_flystick_type* flystick, int max_nflystick, int* nmeatool, dtrack_meatool_type* meatool, int max_nmeatool, int* nmarker, dtrack_marker_type* marker, int max_nmarker ){ char* strs[PROT_MAX_LINES]; char* s; int iline, nlines; int i, len, n; unsigned long ul, ularr[2]; // Defaults: *framenr = 0; *timestamp = -1; // i.e. not available *nbodycal = -1; // i.e. not available *nbody = 0; *nflystick = 0; *nmeatool = 0; *nmarker = 0; // Receive udp packet: len = udp_receive(_udpsock, _udpbuf, _udpbufsize, _udptimeout_us); if(len == -1){ return DTRACK_ERR_TIMEOUT; } if(len <= 0){ return DTRACK_ERR_UDP; } // Split packet in lines: if((nlines = split_lines(_udpbuf, len, strs, PROT_MAX_LINES)) == 0){ return DTRACK_ERR_PCK; } // Process lines: for(iline=0; iline<nlines; iline++){ s = strs[iline]; // Line for frame counter: if(!strncmp(s, "fr ", 3)){ s += 3; if(!(s = get_ul(s, framenr))){ // get frame counter *framenr = 0; return DTRACK_ERR_PCK; } continue; } // Line for timestamp: if(!strncmp(s, "ts ", 3)){ s += 3; if(!(s = get_d(s, timestamp))){ // get timestamp *timestamp = 0; return DTRACK_ERR_PCK; } continue; } // Line for additional information about number of calibrated bodies: if(!strncmp(s, "6dcal ", 6)){ if(max_nbody <= 0){ continue; } s += 6; if(!(s = get_ul(s, &ul))){ // get number of bodies return DTRACK_ERR_PCK; } *nbodycal = (int )ul; continue; } // Line for 6d data: if(!strncmp(s, "6d ", 3)){ if(max_nbody <= 0){ continue; } s += 3; if(!(s = get_ul(s, &ul))){ // get number of bodies return DTRACK_ERR_PCK; } *nbody = n = (int )ul; if(n > max_nbody){ n = max_nbody; } for(i=0; i<n; i++){ // get data of body if(!(s = get_block(s, "uf", &body[i].id, &body[i].quality))){ return DTRACK_ERR_PCK; } if(!(s = get_block(s, "ffffff", NULL, body[i].loc))){ return DTRACK_ERR_PCK; } if(!(s = get_block(s, "fffffffff", NULL, body[i].rot))){ return DTRACK_ERR_PCK; } } continue; } // Line for flystick data: if(!strncmp(s, "6df ", 4)){ if(max_nflystick <= 0){ continue; } s += 4; if(!(s = get_ul(s, &ul))){ // get number of flysticks return DTRACK_ERR_PCK; } *nflystick = n = (int )ul; if(n > max_nflystick){ n = max_nflystick; } for(i=0; i<n; i++){ // get data of body if(!(s = get_block(s, "ufu", ularr, &flystick[i].quality))){ return DTRACK_ERR_PCK; } flystick[i].id = ularr[0]; flystick[i].bt = ularr[1]; if(!(s = get_block(s, "ffffff", NULL, flystick[i].loc))){ return DTRACK_ERR_PCK; } if(!(s = get_block(s, "fffffffff", NULL, flystick[i].rot))){ return DTRACK_ERR_PCK; } } continue; } // Line for measurement tool data: if(!strncmp(s, "6dmt ", 5)){ if(max_nmeatool <= 0){ continue; } s += 5; if(!(s = get_ul(s, &ul))){ // get number of flysticks return DTRACK_ERR_PCK; } *nmeatool = n = (int )ul; if(n > max_nmeatool){ n = max_nmeatool; } for(i=0; i<n; i++){ // get data of body if(!(s = get_block(s, "ufu", ularr, &meatool[i].quality))){ return DTRACK_ERR_PCK; } meatool[i].id = ularr[0]; meatool[i].bt = ularr[1]; if(!(s = get_block(s, "fff", NULL, meatool[i].loc))){ return DTRACK_ERR_PCK; } if(!(s = get_block(s, "fffffffff", NULL, meatool[i].rot))){ return DTRACK_ERR_PCK; } } continue; } // Line for single markers: if(!strncmp(s, "3d ", 3)){ if(max_nmarker <= 0){ continue; } s += 3; if(!(s = get_ul(s, &ul))){ // get number of markers return DTRACK_ERR_PCK; } *nmarker = n = (int )ul; if(n > max_nmarker){ n = max_nmarker; } for(i=0; i<n; i++){ // get marker data if(!(s = get_block(s, "uf", &marker[i].id, &marker[i].quality))){ return DTRACK_ERR_PCK; } if(!(s = get_block(s, "fff", NULL, marker[i].loc))){ return DTRACK_ERR_PCK; } } continue; } // ignore invalid line identifier } return DTRACK_ERR_NONE; }
int main(void){ struct udp_conn udpcon; udp_init_client(&udpcon, port, server_ip); char buffer[255]; struct PID_parameters parameters= (struct PID_parameters){ 10, 800, 0, 1, period, }; double y, u; int i=0; struct timespec next; clock_gettime(CLOCK_REALTIME, &next); timespec_add_us(&next, (int)(period*1000000)); udp_send(&udpcon, "START", 6); while(1){ udp_send(&udpcon, "GET", 4); while(1){ udp_receive(&udpcon, buffer, 255); if (buffer[0]=='G'){ break; } } y = doubleFromReply(buffer); u = PID_controller(parameters, y); setFromDouble(buffer,u); udp_send(&udpcon, buffer, 255); clock_nanosleep(&next); timespec_add_us(&next, (int)(period*1000000)); if (i++*period >= 0.5){ break; } } udp_send(&udpcon, "STOP",5); printf("stop\n"); return 0; } void setFromDouble(char *buffer, double u){ buffer[0]='S'; buffer[1]='E'; buffer[2]='T'; buffer[3]=':'; char dummy[255]; sprintf(dummy, "%f", u); char* ptr = dummy; int i=4; while(*ptr !='\0'){ buffer[i++]=*ptr; ptr++; } buffer[i]='\0'; return; }
int main(int argc, char *argv[]){ init_parse(); // Parses the commandline arguments parse_opts(argc, argv); // IE: ./server -cserver.cfg --name "My Server" // server config ReadServerCfg(cfg_file ? :"server.cfg"); // init sockets struct sockaddr_in newclient; unsigned char buffer[MAX_BUF]; int size; fd_set descriptor; //I don't know sock = create_socket(); bind_socket(&sock, INADDR_ANY, sv_hostport); // on termination atexit(&cleanup); signal(SIGABRT, &exit); signal(SIGTERM, &exit); signal(SIGINT, &exit); // initialize rest of stuff OnServerStart(); #ifndef _WIN32 // fps control const int inc = NS_PER_S / sv_fps; int frame = 0; int previous = 0; struct timespec current, next; clock_gettime(CLOCK_MONOTONIC, &next); #endif // timeval for select struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 0; //main loop while (1) { #ifndef _WIN32 frame++; next.tv_nsec += inc; while (next.tv_nsec > NS_PER_S) { //happens exactly once a second next.tv_nsec -= NS_PER_S; next.tv_sec++; fpsnow = frame - previous; previous = frame; OnSecond(); } #endif OnFrame(); FD_ZERO(&descriptor); FD_SET(sock, &descriptor); select(sock + 1, &descriptor, NULL, NULL, &timeout); if (FD_ISSET(sock, &descriptor)){ size = udp_receive(sock, buffer, MAX_BUF, &newclient); if (size < 3) { perror("Invalid packet! (size < 3)\n"); } else { stream *packet = init_stream(NULL); Stream.write(packet, buffer+2, size-2); // There's a chance that the guy left before all of the packet has been processed. while(1){ int id = IsPlayerKnown(newclient.sin_addr, newclient.sin_port); if (id){ if (ValidatePacket(buffer,id)){ PacketConfirmation(buffer,id); //If the numbering is even, send a confirmation player[id].lastpacket = mtime(); int pid = Stream.read_byte(packet); known_handler h = known_table[pid]; if (!h){ printf("Unhandled packet originating from %s (id:%d)\n", player[id].name, id); //stream *lolbuf = init_stream(NULL); //Stream.write(lolbuf, buffer, size); //unknown(lolbuf, pid); unknown(packet, pid); } else h(packet, id); } }else{ int pid = Stream.read_byte(packet); unknown_handler h = unknown_table[pid]; if (!h) unknown(packet, pid); else h(packet, &newclient); } if (EMPTY_STREAM(packet)){ free(packet); break; } } } } check_sendqueue(); #ifdef _WIN32 Sleep(1000 / sv_fps); //who cares about windows :D #else clock_gettime(CLOCK_MONOTONIC, ¤t); if (((current.tv_sec == next.tv_sec) && (current.tv_nsec < next.tv_nsec)) || (current.tv_sec < next.tv_sec)) { clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL); } else { next.tv_nsec = current.tv_nsec + (current.tv_sec - next.tv_sec) * NS_PER_S; } #endif } return EXIT_SUCCESS; }