コード例 #1
0
ファイル: udf_rw.c プロジェクト: Steve888888/aerospike-server
/*
 * Internal Function: Entry function from UDF code path to send
 * 					  success result to the caller. Performs
 * 					  value translation.
 */
void
send_result(as_result * res, udf_call * call)
{
	as_val * v = res->value;
	if ( res->is_success ) {

		if ( cf_context_at_severity(AS_UDF, CF_DETAIL) ) {
			char * str = as_val_tostring(v);
			cf_detail(AS_UDF, "SUCCESS: %s", str);
			cf_free(str);
		}

		send_success(call, v);

	} else { // Else -- NOT success
		if (as_val_type(v) == AS_STRING) {
			send_udf_failure(call, as_string_fromval(v));
		} else {
			char lua_err_str[1024];
			size_t len = (size_t)sprintf(lua_err_str, "%s:0: in function %s() - error() argument type not handled", call->def.filename, call->def.function);

			call->tr->result_code = AS_PROTO_RESULT_FAIL_UDF_EXECUTION;
			send_failure_str(call, lua_err_str, len);
		}
	}
}
コード例 #2
0
ファイル: NRFLib.cpp プロジェクト: sharst/NRFLib
unsigned char NRFLib::wait_for_send(void) {
	unsigned char success = TX_SENDING;
	do {
		success = send_success();
	} while (success==TX_SENDING);
	return success;
}
コード例 #3
0
ファイル: share_share.cpp プロジェクト: a1406/fytx
static size_t write_callback( char *ptr, size_t size, size_t nmemb, void *userdata)
{
	int open_id;	
	Json::Value msgValue, respVal;
	Json::Reader reader;

	if(false == reader.parse(ptr, msgValue))
	{
		send_fail();
		goto done;
	}

	if (!msgValue["error"].isNull()) {
		send_fail();		
		goto done;
	}

	if (msgValue["id"].isNull()) {
		send_fail();
		goto done;
	}
	update_new_share((struct share_data *)userdata);
	send_success();
done:	
	return size * nmemb;
}
コード例 #4
0
ファイル: lircrcd.cpp プロジェクト: digideskio/lirc
static int getmode_func(int fd, char* message, char* arguments)
{
	if (arguments != NULL)
		return send_error(fd, message, "protocol error\n");
	log_trace1("GETMODE");
	if (lirc_getmode(config))
		return send_result(fd, message, lirc_getmode(config));
	return send_success(fd, message);
}
コード例 #5
0
ファイル: lircrcd.cpp プロジェクト: digideskio/lirc
static int code_func(int fd, char* message, char* arguments)
{
	int index;
	struct event_info* ei;
	struct config_info* ci;
	int ret;

	if (arguments == NULL)
		return send_error(fd, message, "protocol error\n");
	index = get_client_index(fd);
	if (index == -1)
		return send_error(fd, message, "identify yourself first!\n");
	if (clis[index].pending_code != NULL)
		return send_error(fd, message, "protocol error\n");

	log_trace2("%s asking for code -%s-", clis[index].ident_string, arguments);

	ei = clis[index].first_event;
	if (ei != NULL) {
		log_trace2("compare: -%s- -%s-", ei->code, arguments);
		if (strcmp(ei->code, arguments) == 0) {
			ci = ei->first;
			if (ci != NULL) {
				log_trace2("result: -%s-", ci->config_string);
				ret = send_result(fd, message, ci->config_string);
				ei->first = ci->next;
				free(ci->config_string);
				free(ci);
				return ret;
			}
			clis[index].first_event = ei->next;
			free(ei->code);
			free(ei);
			return send_success(fd, message);
		} else {
			return send_success(fd, message);
		}
	}

	clis[index].pending_code = strdup(arguments);
	if (clis[index].pending_code == NULL)
		return send_error(fd, message, "out of memory\n");
	return 1;
}
コード例 #6
0
ファイル: lircrcd.cpp プロジェクト: digideskio/lirc
static int setmode_func(int fd, char* message, char* arguments)
{
	const char* mode = NULL;

	log_trace1(arguments != NULL ? "SETMODE %s" : "SETMODE", arguments);
	mode = lirc_setmode(config, arguments);
	if (mode)
		return send_result(fd, message, mode);
	return arguments == NULL ? send_success(fd, message) : send_error(fd, message, "out of memory\n");
}
コード例 #7
0
ファイル: usb_io.c プロジェクト: nben/puppy
/* Receive a Topfield protocol packet.
 * Returns a negative number if the packet read failed for some reason.
 */
ssize_t get_tf_packet(int fd, struct tf_packet * packet)
{
    __u8 *buf = (__u8 *) packet;
    int r;

    trace(3, fprintf(stderr, "get_tf_packet\n"));

    r = usb_bulk_read(fd, 0x82, buf, MAXIMUM_PACKET_SIZE,
                      TF_PROTOCOL_TIMEOUT);
    if(r < 0)
    {
        fprintf(stderr, "USB read error: %s\n", strerror(errno));
        return -1;
    }

    if(r < PACKET_HEAD_SIZE)
    {
        fprintf(stderr, "Short read. %d bytes\n", r);
        return -1;
    }

    /* Send SUCCESS as soon as we see a data transfer packet */
    if(DATA_HDD_FILE_DATA == get_u32_raw(&packet->cmd))
    {
        send_success(fd);
    }

    swap_in_packet(packet);

    {
        __u16 crc;
        __u16 calc_crc;
        __u16 len = get_u16(&packet->length);

        if(len < PACKET_HEAD_SIZE)
        {
            fprintf(stderr, "Invalid packet length %04x\n", len);
            return -1;
        }

        crc = get_u16(&packet->crc);
        calc_crc = get_crc(packet);

        /* Complain about CRC mismatch */
        if(crc != calc_crc)
        {
            fprintf(stderr, "WARNING: Packet CRC %04x, expected %04x\n", crc,
                    calc_crc);
        }
    }

    print_packet(packet, " IN<");
    return r;
}
コード例 #8
0
static void on_result(struct state_data *state, struct sign_request *request, json_t *result)
{
    if (state->ses->id != state->ses_id)
        return;
    if (result == NULL)
        goto error;

    json_t *code = json_object_get(result, "code");
    if (code == NULL)
        goto error;
    int error_code = json_integer_value(code);
    if (error_code != 0) {
        const char *message = json_string_value(json_object_get(result, "message"));
        if (message == NULL)
            goto error;
        log_error("sign fail, access_id: %s, authorisation: %s, token: %"PRIu64" code: %d, message: %s",
                request->access_id, request->authorisation, request->tonce, error_code, message);
        send_error(state->ses, state->request_id, 11, message);
        return;
    }

    json_t *data = json_object_get(result, "data");
    if (data == NULL)
        goto error;
    struct clt_info *info = state->info;
    uint32_t user_id = json_integer_value(json_object_get(data, "user_id"));
    if (user_id == 0)
        goto error;

    if (info->auth && info->user_id != user_id) {
        asset_unsubscribe(info->user_id, state->ses);
        order_unsubscribe(info->user_id, state->ses);
    }

    info->auth = true;
    info->user_id = user_id;
    log_error("sign success, access_id: %s, user_id: %u", request->access_id, user_id);
    send_success(state->ses, state->request_id);

    return;

error:
    if (result) {
        char *reply = json_dumps(result, 0);
        log_fatal("invalid reply: %s", reply);
        free(reply);
    }
    send_error_internal_error(state->ses, state->request_id);
}
コード例 #9
0
ファイル: lircrcd.cpp プロジェクト: digideskio/lirc
static int ident_func(int fd, char* message, char* arguments)
{
	int index;

	if (arguments == NULL)
		return send_error(fd, message, "protocol error\n");
	log_trace1("IDENT %s", arguments);
	index = get_client_index(fd);
	if (clis[index].ident_string != NULL)
		return send_error(fd, message, "protocol error\n");
	clis[index].ident_string = strdup(arguments);
	if (clis[index].ident_string == NULL)
		return send_error(fd, message, "out of memory\n");

	log_trace("%s connected", clis[index].ident_string);
	return send_success(fd, message);
}
コード例 #10
0
ファイル: puppy.c プロジェクト: nazgul77/libgphoto2
static int
get_info_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileInfo *info, void *data, GPContext *context)
{
	Camera *camera = data;
	int r;
	struct tf_packet reply;
	char *xfolder = strdup (folder);

	backslash (xfolder);

	r = send_cmd_hdd_dir(camera, xfolder, context);
	free (xfolder);
	if(r < GP_OK)
		return r;

	while(0 < get_tf_packet(camera, &reply, context)) {
		switch (get_u32(&reply.cmd)) {
		case DATA_HDD_DIR:
			decode_and_get_info(camera, folder, &reply, filename, info, context);
			send_success(camera,context);
			break;
		case DATA_HDD_DIR_END:
			return GP_OK;
			break;
		case FAIL:
			gp_log (GP_LOG_ERROR, "topfield", "ERROR: Device reports %s\n",
				decode_error(&reply));
			return GP_ERROR_IO;
			break;
		default:
			gp_log (GP_LOG_ERROR, "topfield", "ERROR: Unhandled packet\n");
			return GP_ERROR_IO;
		}
	}
	return GP_OK;
}
コード例 #11
0
ファイル: puppy.c プロジェクト: silid/libpuppy
int do_hdd_dir(int fd, char *path)
{
    int r;

    r = send_cmd_hdd_dir(fd, path);
    if(r < 0)
    {
        return -EPROTO;
    }

    while(0 < get_tf_packet(fd, &reply))
    {
        switch (get_u32(&reply.cmd))
        {
            case DATA_HDD_DIR:
                decode_dir(&reply);
                send_success(fd);
                break;

            case DATA_HDD_DIR_END:
                return 0;
                break;

            case FAIL:
                fprintf(stderr, "ERROR: Device reports %s\n",
                        decode_error(&reply));
                return -EPROTO;
                break;

            default:
                fprintf(stderr, "ERROR: Unhandled packet\n");
                return -EPROTO;
        }
    }
    return -EPROTO;
}
コード例 #12
0
ファイル: puppy.c プロジェクト: nazgul77/libgphoto2
static int
get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileType type, CameraFile *file, void *data,
	       GPContext *context)
{
	Camera *camera = data;
	int result = GP_ERROR_IO;
	enum {
		START,
		DATA,
		ABORT
	} state;
	int r, pid = 0, update = 0;
	uint64_t byteCount = 0;
	struct utimbuf mod_utime_buf = { 0, 0 };
	char *path;
	struct tf_packet reply;

	if (type != GP_FILE_TYPE_NORMAL)
		return GP_ERROR_NOT_SUPPORTED;

	do_cmd_turbo (camera, "ON", context);

	path = get_path(camera, folder, filename);
	r = send_cmd_hdd_file_send(camera, GET, path, context);
	free (path);
	if(r < 0)
		goto out;

	state = START;
	while(0 < (r = get_tf_packet(camera, &reply, context)))
	{
		update = (update + 1) % 4;
		switch (get_u32(&reply.cmd)) {
		case DATA_HDD_FILE_START:
			if(state == START) {
				struct typefile *tf = (struct typefile *) reply.data;

				byteCount = get_u64(&tf->size);
				pid = gp_context_progress_start (context, byteCount, _("Downloading %s..."), filename);
				mod_utime_buf.actime = mod_utime_buf.modtime =
				tfdt_to_time(&tf->stamp);

				send_success(camera,context);
				state = DATA;
			} else {
				gp_log (GP_LOG_ERROR, "topfield", "ERROR: Unexpected DATA_HDD_FILE_START packet in state %d\n", state);
				send_cancel(camera,context);
				state = ABORT;
			}
			break;

		case DATA_HDD_FILE_DATA:
			if(state == DATA) {
				uint64_t offset = get_u64(reply.data);
				uint16_t dataLen = get_u16(&reply.length) - (PACKET_HEAD_SIZE + 8);
				int w;

				if (!update) { /* avoid doing it too often */
					gp_context_progress_update (context, pid, offset + dataLen);
					if (gp_context_cancel (context) == GP_CONTEXT_FEEDBACK_CANCEL) {
						send_cancel(camera,context);
						state = ABORT;
					}
				}

				if(r < get_u16(&reply.length)) {
					gp_log (GP_LOG_ERROR, "topfield", "ERROR: Short packet %d instead of %d\n", r, get_u16(&reply.length));
					/* TODO: Fetch the rest of the packet */
				}

				w = gp_file_append (file, (char*)&reply.data[8], dataLen);

				if(w < GP_OK) {
					/* Can't write data - abort transfer */
					gp_log (GP_LOG_ERROR, "topfield", "ERROR: Can not write data: %d\n", w);
					send_cancel(camera,context);
					state = ABORT;
				}
			} else {
				gp_log (GP_LOG_ERROR, "topfield", "ERROR: Unexpected DATA_HDD_FILE_DATA packet in state %d\n", state);
				send_cancel(camera,context);
				state = ABORT;
			}
			break;

		case DATA_HDD_FILE_END:
			send_success(camera,context);
			result = GP_OK;
			goto out;
			break;

		case FAIL:
			gp_log (GP_LOG_ERROR, "topfield", "ERROR: Device reports %s\n", decode_error(&reply));
			send_cancel(camera,context);
			state = ABORT;
			break;

		case SUCCESS:
			goto out;
			break;

		default:
			gp_log (GP_LOG_ERROR, "topfield", "ERROR: Unhandled packet (cmd 0x%x)\n", get_u32(&reply.cmd));
			break;
		}
	}
	if (pid) gp_context_progress_stop (context, pid);
out:
	do_cmd_turbo (camera, "OFF", context);
	return result;
}
コード例 #13
0
int process_cmd(FILE *fp, char *command, int buffer_size, char *buffer)
{
	if (!strcmp(command, "/rapl_domains_count") ||
		!strncmp(command, "/rapl_domains_count/",
			strlen("/rapl_domains_count/"))) {
		int count;
		count = powercap_get_rapl_domain_count(command);
		printf("rapl domain count %d\n", count);
		snprintf(buffer, buffer_size, "%d", count);
		send_success_response_plain(fp, buffer);
	} else if (!strncmp(command, "/rapl_domain_name/",
			strlen("/rapl_domain_name/"))) {
		if (read_domain_name(command, buffer_size, buffer) ==
							STATUS_SUCCESS)
			send_success_response_plain(fp, buffer);
		else
			send_error(fp, "Sysfs error", "500",
				"sysfs read error", "name read failed");
	} else if (!strncmp(command, "/rapl_domain_energy/",
			strlen("/rapl_domain_energy/"))) {
		if (read_domain_energy(command, buffer_size, buffer) ==
							STATUS_SUCCESS)
			send_success_response_plain(fp, buffer);
		else
			send_error(fp, "Sysfs error", "500",
				"sysfs read error", "energy read failed");
	} else if (!strncmp(command, "/rapl_domain_max_energy/",
			strlen("/rapl_domain_max_energy/"))) {
		if (read_domain_max_energy(command, buffer_size, buffer) ==
							STATUS_SUCCESS)
			send_success_response_plain(fp, buffer);
		else
			send_error(fp, "Sysfs error", "500",
				"sysfs read error", "energy read failed");
	} else if (!strcmp(command, "/") || !strcmp(command, "/index.html")) {
		int content_len;
		char local_ip_buffer[128];
		if (local_host_name[0] != '\0')
			content_len = snprintf(local_ip_buffer,
					sizeof(local_ip_buffer),
					"var root_path = 'http://%s:%d/';\n",
					local_host_name, local_port_id);
		else
			content_len = snprintf(local_ip_buffer,
					sizeof(local_ip_buffer),
					"var root_path = 'http://%s:%d/';\n",
					local_ip_addr, local_port_id);
		if (local_index_html) {
			FILE *src_fp;
			int size;
			int ch;
			src_fp = fopen(local_index_html_file, "r");
			if (!src_fp) {
				send_error(fp, "Page not found", "404",
				"Not found", "Requested URI not found");
				return TRUE;
			}
			fseek(src_fp, 0L, SEEK_END);
			size = ftell(src_fp);
			fseek(src_fp, 0L, SEEK_SET);
			send_success(fp, size);
			while(!feof(src_fp)) {
				ch = fgetc(src_fp);
				fputc(ch, fp);
			}
			fclose(src_fp);
		} else {
			content_len += strlen(index_html_contents_header);
			content_len += strlen(index_html_contents_contents);
			send_success(fp, content_len);
			fprintf(fp, "%s\n", index_html_contents_header);
			fprintf(fp, "%s\n", local_ip_buffer);
			fprintf(fp, "%s\n", index_html_contents_contents);
		}
	 } else if (!strncmp(command, "/terminate",
			strlen("/terminate"))) {
		return FALSE;
	} else {
		send_error(fp, "Page not found", "404",
				"Not found", "Requested URI not found");
	}

	return TRUE;
}
コード例 #14
0
ファイル: udf_rw.c プロジェクト: LilyMat/aerospike-server
/*
 * Internal Function: Entry function from UDF code path to send
 * 					  success result to the caller. Performs
 * 					  value translation.
 */
void
send_result(as_result * res, udf_call * call, void *udata)
{
	// The following "no-op" line serves to quiet the compiler warning of an
	// otherwise unused variable.
	udata = udata;
	as_val * v = res->value;
	if ( res->is_success ) {

		if ( cf_context_at_severity(AS_UDF, CF_DETAIL) ) {
			char * str = as_val_tostring(v);
			cf_detail(AS_UDF, "SUCCESS: %s", str);
			cf_free(str);
		}

		if ( v != NULL ) {
			switch( as_val_type(v) ) {
				case AS_NIL:
				{
					send_success(call, AS_PARTICLE_TYPE_NULL, NULL, 0);
					break;
				}
				case AS_BOOLEAN:
				{
					as_boolean * b = as_boolean_fromval(v);
					int64_t bi = as_boolean_tobool(b) == true ? 1 : 0;
					send_success(call, AS_PARTICLE_TYPE_INTEGER, &bi, 8);
					break;
				}
				case AS_INTEGER:
				{
					as_integer * i = as_integer_fromval(v);
					int64_t ri = as_integer_toint(i);
					send_success(call, AS_PARTICLE_TYPE_INTEGER, &ri, 8);
					break;
				}
				case AS_STRING:
				{
					// this looks bad but it just pulls the pointer
					// out of the object
					as_string * s = as_string_fromval(v);
					char * rs = (char *) as_string_tostring(s);
					send_success(call, AS_PARTICLE_TYPE_STRING, rs, as_string_len(s));
					break;
				}
				case AS_BYTES:
				{
					as_bytes * b = as_bytes_fromval(v);
					uint8_t * rs = as_bytes_get(b);
					send_success(call, AS_PARTICLE_TYPE_BLOB, rs, as_bytes_size(b));
					break;
				}
				case AS_MAP:
				case AS_LIST:
				{
					as_buffer buf;
					as_buffer_init(&buf);

					as_serializer s;
					as_msgpack_init(&s);

					int res = as_serializer_serialize(&s, v, &buf);

					if (res != 0) {
						const char * error = "Complex Data Type Serialization failure";
						cf_warning(AS_UDF, "%s (%d)", (char *)error, res);
						as_buffer_destroy(&buf);
						send_cdt_failure(call, AS_PARTICLE_TYPE_STRING, (char *)error, strlen(error));
					}
					else {
						// Do not use this until after cf_detail_binary() can accept larger buffers.
						// cf_detail_binary(AS_UDF, buf.data, buf.size, CF_DISPLAY_HEX_COLUMNS, 
						// "serialized %d bytes: ", buf.size);
						send_success(call, to_particle_type(as_val_type(v)), buf.data, buf.size);
						// Not needed stack allocated - unless serialize has internal state
						// as_serializer_destroy(&s);
						as_buffer_destroy(&buf);
					}

					break;
				}
				default:
				{
					cf_debug(AS_UDF, "SUCCESS: VAL TYPE UNDEFINED %d\n", as_val_type(v));
					send_success(call, AS_PARTICLE_TYPE_STRING, NULL, 0);
					break;
				}
			}
		} else {
			send_success(call, AS_PARTICLE_TYPE_NULL, NULL, 0);
		}
	} else { // Else -- NOT success
		as_string * s   = as_string_fromval(v);
		char *      rs  = (char *) as_string_tostring(s);

		cf_debug(AS_UDF, "FAILURE when calling %s %s %s", call->filename, call->function, rs);
		send_udf_failure(call, AS_PARTICLE_TYPE_STRING, rs, as_string_len(s));
	}
}
コード例 #15
0
ファイル: d_node.c プロジェクト: chenxiaoyang1/SBPFS
void process_req(struct list_entry * ent) {
	char* head_data;
	int head_data_len;
	struct sbpfs_head head;
	if (ent->req == 1) {
		char* data = (char*) malloc(ent->length);
		if (data == NULL) {
			printf("malloc data failed\n");
			return;
		}
		int succeed = readblock(ent->block_num, ent->offset, ent->length, data);
		printf("succeed: %d\n", succeed);
		if (succeed < 0)
			send_err(ent->skt, "read_block", "cannot read block");
		else {
			head.data = NULL;
			head.title = REQUEST_OK;
			head.entry_num = 0;
			char data_length_s[32];
			sprintf(data_length_s, "%d", ent->length);
			mkent(head,CONTENT_LEN,data_length_s);
			if (make_head(&head_data, &head_data_len, &head) == -1) {
				printf("mkhead failed\n");
				free(head_data);
				free(data);
				return;
			}
			if (sbp_send(ent->skt, head_data, head_data_len) < 0) {
				printf("send head err failed\n");
				sbp_perror("a");
				perror("a");
				free(data);
				free(head_data);
				return;
			}
			if (sbp_send(ent->skt, data, ent->length) < 0) {
				printf("send data err failed\n");
				sbp_perror("a");
				perror("a");
				free(data);
				free(head_data);

				return;
			}
			free(data);
			free(head_data);
			return;
		}
	}
	if (ent->req == 2) {
		int succeed = writeblock(ent->block_num, ent->offset, ent->length,
				ent->data);
		printf("succeed in write : %d\n", succeed);
		if (succeed < 0) {
			send_err(ent->skt, "write_block", "cannot write block");
		} else {
			head.data = NULL;
			head.title = REQUEST_OK;
			head.entry_num = 0;
			mkent(head,CONTENT_LEN,"0");
		}
		if (make_head(&head_data, &head_data_len, &head) == -1) {
			printf("mkhead failed\n");
			free(head_data);
			return;
		}
		if (sbp_send(ent->skt, head_data, head_data_len) < 0) {
			printf("send head err failed\n");
			sbp_perror("a");
			perror("a");
			free(head_data);
			return;
		}
		char*ip = "192.168.1.107";
		send_success(ip,ent->block_num);
	}
	if (ent->req == 3) {
		int succeed = writeblock(ent->block_num, ent->offset, ent->length,
				ent->data);
		printf("succeed in copy : %d\n", succeed);

	}
}
コード例 #16
0
ファイル: share_get.cpp プロジェクト: a1406/fytx
int main(void)
{
	int len;
	char *lenstr,poststr[512];
	char *player_id, *server_id, *pay_id;
	int n_pay_id;
	int n_pay_times;
	int can_share = 1;
	uint64_t effect = 0;
	MYSQL_RES *res = NULL;
	MYSQL_ROW row;	
	char sql[256];
/*
	len = 10;
	for (;;) {
		if (len != 10)
			break;
		sleep(2);
	}
*/
	
	init_db((char *)"127.0.0.1", 3306, (char *)"pay", (char *)"root", (char *)"123456");

	printf("Content-Type:text/html\n\n");
	lenstr=getenv("CONTENT_LENGTH");
	if(lenstr == NULL) {
		printf("<DIV STYLE=\"COLOR:RED\">Errorarameters should be entered!</DIV>\n");
		return (0);
	}
	len=atoi(lenstr) + 1;
	if (len >= 512)
		return (0);
	
	fgets(poststr,len,stdin);
	parse_post_data(poststr, len);
	server_id = get_value((char *)"server_id");
	player_id = get_value((char *)"player_id");
	pay_id = get_value((char *)"pay_id");	
	if (!server_id || !player_id || !pay_id)
		return (0);	


	sprintf(sql, "select share_times, pay_times from share where server_id = %s and player_id = %s", server_id, player_id);
	res = query(sql, 1, NULL);
	if (!res) {
		send_fail();
		goto done;
	}
	row = fetch_row(res);
	if (!row) {
		send_fail();
		goto done;
	}

	n_pay_id = atoi(pay_id);
	if (!check_can_get_share_pay(row, n_pay_id)) {
		send_fail();
		goto done;
	}

	n_pay_times = atoi(row[1]);
	n_pay_times |= (1 << n_pay_id);

	free_query(res);
	res = NULL;
	
	sprintf(sql, "update `share` set `pay_times` = %d where `server_id` = %s and `player_id` = %s",
		n_pay_times, server_id, player_id);
	
	query(sql, 1, &effect);

	if (effect != 1) {
		send_fail();
		goto done;
	}
	
	send_success(n_pay_id);
	send_charge_gold_req(atoi(player_id), get_gold[n_pay_id], 0, (char *)"share reward", (char *)"127.0.0.1", (char *)"3008");
	
done:
	if (res)
		free_query(res);
	fflush(stdout);
	close_db();		
	return 0;
}
コード例 #17
0
ファイル: puppy.c プロジェクト: silid/libpuppy
int do_hdd_file_get(int fd, char *srcPath, char *dstPath)
{
    int result = -EPROTO;
    time_t startTime = time(NULL);
    enum
    {
        START,
        DATA,
        ABORT
    } state;
    int dst = -1;
    int r;
    int update = 0;
    __u64 byteCount = 0;
    struct utimbuf mod_utime_buf = { 0, 0 };

    dst = open64(dstPath, O_WRONLY | O_CREAT | O_TRUNC,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    if(dst < 0)
    {
        fprintf(stderr, "ERROR: Can not open destination file: %s\n",
                strerror(errno));
        return errno;
    }

    r = send_cmd_hdd_file_send(fd, GET, srcPath);
    if(r < 0)
    {
        goto out;
    }

    state = START;
    while(0 < (r = get_tf_packet(fd, &reply)))
    {
        update = (update + 1) % 16;
        switch (get_u32(&reply.cmd))
        {
            case DATA_HDD_FILE_START:
                if(state == START)
                {
                    struct typefile *tf = (struct typefile *) reply.data;

                    byteCount = get_u64(&tf->size);
                    mod_utime_buf.actime = mod_utime_buf.modtime =
                        tfdt_to_time(&tf->stamp);

                    send_success(fd);
                    state = DATA;
                }
                else
                {
                    fprintf(stderr,
                            "ERROR: Unexpected DATA_HDD_FILE_START packet in state %d\n",
                            state);
                    send_cancel(fd);
                    state = ABORT;
                }
                break;

            case DATA_HDD_FILE_DATA:
                if(state == DATA)
                {
                    __u64 offset = get_u64(reply.data);
                    __u16 dataLen =
                        get_u16(&reply.length) - (PACKET_HEAD_SIZE + 8);
                    ssize_t w;

                    if(!update && !quiet)
                    {
                        progressStats(byteCount, offset + dataLen, startTime);
                    }

                    if(r < get_u16(&reply.length))
                    {
                        fprintf(stderr,
                                "ERROR: Short packet %d instead of %d\n", r,
                                get_u16(&reply.length));
                        /* TODO: Fetch the rest of the packet */
                    }

                    w = write(dst, &reply.data[8], dataLen);
                    if(w < dataLen)
                    {
                        /* Can't write data - abort transfer */
                        fprintf(stderr, "ERROR: Can not write data: %s\n",
                                strerror(errno));
                        send_cancel(fd);
                        state = ABORT;
                    }
                }
                else
                {
                    fprintf(stderr,
                            "ERROR: Unexpected DATA_HDD_FILE_DATA packet in state %d\n",
                            state);
                    send_cancel(fd);
                    state = ABORT;
                }
                break;

            case DATA_HDD_FILE_END:
                send_success(fd);
                result = 0;
                goto out;
                break;

            case FAIL:
                fprintf(stderr, "ERROR: Device reports %s\n",
                        decode_error(&reply));
                send_cancel(fd);
                state = ABORT;
                break;

            case SUCCESS:
                goto out;
                break;

            default:
                fprintf(stderr, "ERROR: Unhandled packet (cmd 0x%x)\n",
                        get_u32(&reply.cmd));
        }
    }
    utime(dstPath, &mod_utime_buf);
    finalStats(byteCount, startTime);

  out:
    close(dst);
    return result;
}