/* * Clear all barriers and release their waiters with an error code. */ static void clear_barriers(void) { barrier_ctrl_t *barrierp, *barriernextp; info("Clearing all barriers\n"); for (barrierp = allbarriers; barrierp != NULL; barrierp = barriernextp) { int i; barriernextp = barrierp->next; barrierp->next = NULL; for (i = 0; i < barrierp->index; i++) { release_client(barrierp->waiters[i].sock, barrierp->waiters[i].ipaddr, barrierp->waiters[i].istcp, SERVER_ERROR_SIGHUP); } free(barrierp); barrierp = NULL; } allbarriers = NULL; }
ResultCode Store::connection_test() { ResultCode rc = OK; // Check that we can connect to cassandra by getting a client. This logs in // and switches to the specified keyspace, so is a good test of whether // cassandra is working properly. TRC_STATUS("Starting store"); try { get_client(); release_client(); } catch(TTransportException te) { TRC_ERROR("Store caught TTransportException: %s", te.what()); rc = CONNECTION_ERROR; } catch(NotFoundException nfe) { TRC_ERROR("Store caught NotFoundException: %s", nfe.what()); rc = NOT_FOUND; } catch(...) { TRC_ERROR("Store caught unknown exception!"); rc = UNKNOWN_ERROR; } return rc; }
void remove_client(struct client *cl_dat) { struct client *cl, *cll; for (cl = clientlist, cll = (struct client *)0; cl; ) { if (cl_dat == cl) { if (cll) cll->ct_next = cl->ct_next; else clientlist = cl->ct_next; release_client(cl); x_free(cl); break; } cll = cl; cl = cll->ct_next; } }
// called at atexit() to free malloced memory in variable global; void release_default_global() { // clean threads; if (global.cluster_mode == CLUSTER_CLIENT || global.cluster_mode == CLUSTER_SINGLETON) { release_client(); } else { // TODO(weizhenwei): master thread clean utils; release_server(); } pthread_mutex_destroy(&total_thread_mutex); dmd_log(LOG_ERR, "in function %s, before dump stats!\n", __func__); // dump and release global statistics; pthread_mutex_lock(&global_stats->mutex); dump_statistics(global_stats); pthread_mutex_unlock(&global_stats->mutex); release_statistics(global_stats); // close database connection; close_db(opendmd_db); }
void client_destroy(client_t *client) { if (client == NULL) return; if (release_client (client)) return; /* write log entry if ip is set (some things don't set it, like outgoing * slave requests */ if (client->respcode && client->parser) logging_access(client); if (client->con) connection_close(client->con); if (client->parser) httpp_destroy(client->parser); global_lock (); global.clients--; stats_event_args (NULL, "clients", "%d", global.clients); global_unlock (); /* drop ref counts if need be */ if (client->refbuf) refbuf_release (client->refbuf); /* we need to free client specific format data (if any) */ if (client->free_client_data) client->free_client_data (client); free(client->username); free(client->password); free(client); }
static inline void ccci_put_client(struct ccci_dev_client *client) { WARN_ON(client==NULL); if (atomic_dec_and_test(&client->user)) release_client(client); }
bool Store::do_sync(Operation* op, SAS::TrailId trail) { bool success = false; Client* client = NULL; ResultCode cass_result = OK; std::string cass_error_text = ""; // Set up whether the perform should be retried on failure. // Only try once, unless there's connection error, in which case try twice. bool retry = false; int attempt_count = 0; // Call perform() to actually do the business logic of the request. Catch // exceptions and turn them into return codes and error text. do { retry = false; try { attempt_count++; // Get a client to execute the operation. client = get_client(); success = op->perform(client, trail); } catch(TTransportException& te) { cass_result = CONNECTION_ERROR; cass_error_text = (boost::format("Exception: %s [%d]") % te.what() % te.getType()).str(); // SAS log the connection error. SAS::Event event(trail, SASEvent::CASS_CONNECT_FAIL, 0); event.add_var_param(cass_error_text); SAS::report_event(event); // Recycle the connection. release_client(); client = NULL; if (attempt_count <= 1) { // Connection error, destroy and recreate the connection, and retry the // request once TRC_DEBUG("Connection error, retrying"); retry = true; cass_result = OK; } } catch(InvalidRequestException& ire) { cass_result = INVALID_REQUEST; cass_error_text = (boost::format("Exception: %s [%s]") % ire.what() % ire.why.c_str()).str(); } catch(NotFoundException& nfe) { cass_result = NOT_FOUND; cass_error_text = (boost::format("Exception: %s") % nfe.what()).str(); } catch(RowNotFoundException& nre) { cass_result = NOT_FOUND; cass_error_text = (boost::format("Row %s not present in column_family %s") % nre.key % nre.column_family).str(); } catch(UnavailableException& ue) { cass_result = UNAVAILABLE; cass_error_text = (boost::format("Exception: %s") % ue.what()).str(); } catch(...) { cass_result = UNKNOWN_ERROR; cass_error_text = "Unknown error"; } } while (retry); if (cass_result == OK) { if (_comm_monitor) { _comm_monitor->inform_success(); } } else { if (_comm_monitor) { if (cass_result == CONNECTION_ERROR) { _comm_monitor->inform_failure(); } else { _comm_monitor->inform_success(); } } if (cass_result == NOT_FOUND) { // We expect to get "not found" errors during normal operation // (e.g. invalid usernames) so log it at a much lower level. TRC_DEBUG("Cassandra request failed: rc=%d, %s", cass_result, cass_error_text.c_str()); } else { TRC_ERROR("Cassandra request failed: rc=%d, %s", cass_result, cass_error_text.c_str()); } op->unhandled_exception(cass_result, cass_error_text, trail); } return success; }
int main(){ setLogDebugLevel( dbgLevel ); setLogFile( logFile ); setLogSilentMode(1); writeLog( 0, "Starting remoteAdv Master Client - Version: %s", version ); // Gets a hostent struct containing data about the given host struct hostent *server = gethostbyname( server_ip ); if(server == NULL) { herror("ERROR: Host lookup failed"); exit(1); } // Create a TCP socket and return a file descriptor for accessing it int sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) { perror("ERROR: Open socket failed"); exit(1); } // Create and initialize a sock_addr struct contianing info about the server to connect to struct sockaddr_in server_addr; memset( &server_addr , 0 , sizeof(server_addr)); // Denotes an internet socket server_addr.sin_family = AF_INET; // Copies several values from the server hostent struct into this one memcpy( &server_addr.sin_addr.s_addr, server->h_addr, server->h_length ); // Sets the server port to the one passed in server_addr.sin_port = server_port; // Connect to the server int connectResult = connect(sockfd,(struct sockaddr*)&server_addr, sizeof(server_addr)); if(connectResult == -1) { perror("ERROR: Connection to server failed"); exit(1); } int auth_result = authenticate( sockfd ); if( auth_result == -1 ) { writeLog( -1, "Server authentication failed" ); exit(1); } else { writeLog( 2, "Server connection successful" ); writeLog( 3, "Connected to server at '%s:%d'", server_ip, server_port ); } int end = 0; // Wait to send commands while( !end ) { printMenu(); // Display options to allow user to see options. int input = 0; scanf( "%d", &input ); switch( input ) { case sel_list_slaves: system( "clear" ); // Clear works on unix like enviorments, this will throw an error on windows. list_slaves( sockfd ); break; case sel_claim_client: system( "clear" ); claim_client( sockfd ); break; case sel_release_client: release_client( sockfd ); break; case sel_set_debug_level: set_debug_level( sockfd ); break; case sel_kill_client: kill_client( sockfd ); break; case sel_close: end = 1; default: break; } fflush( stdout ); system( "clear" ); } // Close the connection to the server close( sockfd ); return 0; }
/** * void process_dgram(char *dgram, struct sockaddr_in *addr) * * Processes accepted datagram. Checks if sequential ID is correct, if it's lower, * we resend the ACK packet and dont bother with that datagram anymore, because * it was already processed before. */ void process_dgram(char *dgram, struct sockaddr_in *addr) { /* Protocol token */ char *token; /* Token length */ int token_len; /* Command */ char *type; /* Generic char buffer */ char *generic_chbuff; /* Sequential ID of received packet */ int packet_seq_id; /* Client which we receive from */ client_t *client; /* Generic unsigned int var */ unsigned int generic_uint; /* String representation of address */ char addr_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &addr->sin_addr, addr_str, INET_ADDRSTRLEN); /* Log */ sprintf(log_buffer, "DATA_IN: %s <--- %s:%d", dgram, addr_str, htons(addr->sin_port) ); log_line(log_buffer, LOG_DEBUG); token = strtok(dgram, ";"); token_len = strlen(token); generic_chbuff = strtok(NULL, ";"); packet_seq_id = (int) strtol(generic_chbuff, NULL, 0); /* Check if datagram belongs to us */ if( (token_len == strlen(STRINGIFY(APP_TOKEN))) && strncmp(token, STRINGIFY(APP_TOKEN), strlen(STRINGIFY(APP_TOKEN))) == 0 && packet_seq_id > 0) { type = strtok(NULL, ";"); /* New client connection */ if(strncmp(type, "CONNECT", 7) == 0) { add_client(addr); client = get_client_by_addr(addr); if(client) { send_ack(client, 1, 0); send_reconnect_code(client); /* Release client */ release_client(client); } } /* Reconnect */ else if(strncmp(type, "RECONNECT", 9) == 0) { client = get_client_by_index(get_client_index_by_rcode(strtok(NULL, ";"))); if(client) { /* Sends ACK aswell after resetting clients SEQ_ID */ reconnect_client(client, addr); /* Release client */ release_client(client); } } /* Client should already exist */ else { client = get_client_by_addr(addr); if(client != NULL) { /* Check if expected seq ID matches */ if(packet_seq_id == client->pkt_recv_seq_id) { /* Get command */ if(strncmp(type, "CREATE_GAME", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); create_game(client); } /* Receive ACK packet */ else if(strncmp(type, "ACK", 3) == 0) { recv_ack(client, (int) strtoul(strtok(NULL, ";"), NULL, 10)); update_client_timestamp(client); } /* Close client connection */ else if(strncmp(type, "CLOSE", 5) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); remove_client(&client); } /* Keepalive loop */ else if(strncmp(type, "KEEPALIVE", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); } /* Join existing game */ else if(strncmp(type, "JOIN_GAME", 9) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); join_game(client, strtok(NULL, ";")); } /* Leave existing game */ else if(strncmp(type, "LEAVE_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); leave_game(client); } /* Start game */ else if(strncmp(type, "START_GAME", 10) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); start_game(client); } /* Rolling die */ else if(strncmp(type, "DIE_ROLL", 8) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); roll_die(client); } /* Moving figure */ else if(strncmp(type, "FIGURE_MOVE", 11) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); /* Parse figure id */ generic_chbuff = strtok(NULL, ";"); generic_uint = (unsigned int) strtoul(generic_chbuff, NULL, 10); move_figure(client, generic_uint); } else if(strncmp(type, "MESSAGE", 7) == 0) { /* ACK client */ send_ack(client, packet_seq_id, 0); broadcast_message(client, strtok(NULL, ";")); } } /* Packet was already processed */ else if(packet_seq_id < client->pkt_recv_seq_id && strncmp(type, "ACK", 3) != 0) { send_ack(client, packet_seq_id, 1); } /* If client didnt close conection */ if(client != NULL) { /* Release client */ release_client(client); } } } } }
int create_client(GtkWidget *widget,gpointer Parameter) { ClientConfigStruct *CCS=(ClientConfigStruct *)Parameter; FILE *file=NULL; unsigned long FileSize=0; char *FileBuffer=NULL,*strbuffer=NULL,*pStart=NULL,tmp=85; int i; if(release_client()!=0) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); return -1; } file=fopen("Client.exe","rb+"); if(file==NULL) { MessageBox(NULL,"Open file failed!","ERROR",MB_OK); } fseek(file,0,SEEK_END); FileSize=ftell(file); fseek(file,0,SEEK_SET); FileBuffer=(char *)malloc(sizeof(char)*FileSize); if(FileBuffer==NULL) exit(-1); memset(FileBuffer,NULL,sizeof(char)*FileSize); fread(FileBuffer,sizeof(char),FileSize,file); /*-------------------修改客户端文件内容---------------------*/ //修改服务端IP地址 pStart=match_str(FileBuffer,"SERVER_IP:",FileSize,strlen("SERVER_IP:")); if(pStart==NULL) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Server_IP_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=strlen("SERVER_IP:"),i=0;i<50;pStart++,i++) *pStart^=tmp; //修改服务端监听端口 pStart=match_str(FileBuffer,"SERVER_PORT:",FileSize,strlen("SERVER_PORT:")); if(pStart==NULL) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Server_Port_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=strlen("SERVER_PORT:"),i=0;i<50;pStart++,i++) *pStart^=tmp; //修改HTTP代理服务器IP pStart=match_str(FileBuffer,"PROXY_IP:",FileSize,strlen("PROXY_IP:")); if(pStart==NULL) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_IP_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=strlen("PROXY_IP:"),i=0;i<50;pStart++,i++) *pStart^=tmp; //修改HTTP代理服务器连接端口 pStart=match_str(FileBuffer,"PROXY_PORT:",FileSize,strlen("PROXY_PORT:")); if(pStart==NULL) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Port_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=strlen("PROXY_PORT:"),i=0;i<50;pStart++,i++) *pStart^=tmp; //修改代理服务器登陆账号 pStart=match_str(FileBuffer,"PROXY_USERNAME:"******"PROXY_USERNAME:"******"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Username_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=15,i=0;i<50;pStart++,i++) *pStart^=tmp; //修改代理服务器登陆密码 pStart=match_str(FileBuffer,"PROXY_PASSWORD:"******"PROXY_PASSWORD:"******"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } pStart++; strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Password_entry)); memcpy(pStart,strbuffer,strlen(strbuffer)); for(pStart-=strlen("PROXY_PASSWORD:"******"RC4_KEY:",FileSize,strlen("RC4_KEY:")); for(pStart-=strlen("RC4_KEY:"),i=0;i<256;pStart++,i++) *pStart^=tmp; /*-------------------保存修改的内容---------------------*/ fclose(file); if((file=fopen("Client.exe","wb"))==NULL) { MessageBox(NULL,"Create client failed!","ERROR",MB_OK); fclose(file); free(FileBuffer); return -1; } fwrite(FileBuffer,sizeof(char),FileSize,file); fclose(file); free(FileBuffer); MessageBox(NULL,"Create client successfully!","Success",MB_OK); return 0; }
/* * Handle a barrier request. */ static int handle_request(int sock, struct sockaddr_in *client, barrier_req_t *barrier_reqp, int istcp) { barrier_ctrl_t *barrierp = allbarriers; int i; /* * Find the control structure. Since a waiter can check in before * the controller does, just create a new one. The count will go * to -1 if its a waiter. It will all even out later! */ while (barrierp) { if (!strcmp(barrierp->name, barrier_reqp->name)) break; barrierp = barrierp->next; } if (!barrierp) { barrierp = calloc(1, sizeof(barrier_ctrl_t)); if (!barrierp) fatal("Out of memory!"); barrier_reqp->name[sizeof(barrier_reqp->name) - 1] = '\0'; barrierp->name = strdup(barrier_reqp->name); if (!barrierp->name) fatal("Out of memory!"); if (verbose) info("Barrier created: %s\n", barrierp->name); barrierp->next = allbarriers; allbarriers = barrierp; } if (barrier_reqp->request == BARRIER_INIT) { barrierp->count += barrier_reqp->count; barrierp->ipaddr.s_addr = client->sin_addr.s_addr; if (verbose) info("%s: Barrier initialized: %s %d (%d)\n", inet_ntoa(client->sin_addr), barrierp->name, barrier_reqp->count, barrierp->count); if (barrier_reqp->flags & BARRIER_INIT_NOWAIT) { info("%s: Barrier Master not waiting: %s\n", inet_ntoa(client->sin_addr), barrierp->name); release_client(sock, client->sin_addr, istcp, 0); goto check; } } else if (barrier_reqp->request == BARRIER_WAIT) { barrier_reqp->count = -1; barrierp->count -= 1; if (verbose) info("%s: Barrier waiter: %s %d\n", inet_ntoa(client->sin_addr), barrierp->name, barrierp->count); } else { error("Ignoring invalid barrier request from %s", inet_ntoa(client->sin_addr)); return 1; } /* * Record the waiter info for later wakeup, including the * initializer, who is also woken up. */ barrierp->waiters[barrierp->index].count = barrier_reqp->count; barrierp->waiters[barrierp->index].sock = sock; barrierp->waiters[barrierp->index].istcp = istcp; barrierp->waiters[barrierp->index].error = barrier_reqp->error; barrierp->waiters[barrierp->index].ipaddr.s_addr = client->sin_addr.s_addr; barrierp->index++; if (barrier_reqp->error > barrierp->error) barrierp->error = barrier_reqp->error; if (istcp) tcpsockets[sock].barrier = barrierp; /* * If the count goes to zero, wake everyone up. We write back a * value to make it "easy" for the waiter to notice an error, * say if the server node goes down or the daemon dies. */ check: if (barrierp->count != 0) return 0; for (i = 0; i < barrierp->index; i++) { if (verbose) info("%s: releasing %s\n", barrierp->name, inet_ntoa(barrierp->waiters[i].ipaddr)); release_client(barrierp->waiters[i].sock, barrierp->waiters[i].ipaddr, barrierp->waiters[i].istcp, barrierp->error); } /* * And free the barrier structure from the list. */ remove_barrier(barrierp); return 0; }