fs_bool fs_node_bind_event(struct fs_node* node, fs_id node_id, int socket, struct sockaddr* sa, socklen_t socklen, struct fs_server* server, struct event_base* event_base ){ int ret = 0; if(node->read_ev){ event_free(node->read_ev); } if(node->write_ev){ event_free(node->write_ev); } node->node_id = node_id; node->socket = socket; node->server = server; node->read_ev = (struct event*)fs_malloc(sizeof(struct event)); node->write_ev = (struct event*)fs_malloc(sizeof(struct event)); const char bOper = 1; evutil_make_socket_nonblocking(node->socket); setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&bOper, sizeof(const char)); ret = event_assign(node->read_ev, event_base, node->socket, EV_READ | EV_PERSIST, libevent_cb_node_onrecv_data, node); ret |= event_assign(node->write_ev, event_base, node->socket, EV_WRITE | EV_PERSIST, libevent_cb_node_onsend_data, node); if(ret != 0){ goto fail; } node->recv_buffer = fs_create_output_stream(1024); node->send_buffer = fs_create_output_stream(1024); pthread_mutex_init(&node->write_mutex, NULL); pthread_mutex_init(&node->close_mutex, NULL); event_add(node->read_ev, NULL); event_add(node->write_ev, NULL); goto success; success: return fs_true; fail: if(node->read_ev) event_free(node->read_ev); if(node->write_ev) event_free(node->write_ev); return fs_false; }
struct fs_output_stream* fs_create_output_stream(size_t len){ struct fs_output_stream* ret = (struct fs_output_stream*)fs_malloc(sizeof(*ret)); fs_zero(ret, sizeof(*ret)); ret->data = (BYTE*)fs_malloc(len); ret->pos = 0; ret->buff_len = len; ret->weak_ref = fs_false; return ret; }
struct fs_invoke_call_function* fs_create_invoke_call(VALUE (*func)(VALUE), int argc, VALUE* argv){ VALUE *copy_argv = fs_malloc(argc * sizeof(VALUE)); memcpy(copy_argv, argv, argc * sizeof(VALUE)); struct fs_invoke_call_function* ret = (struct fs_invoke_call_function*)fs_malloc(sizeof(*ret)); fs_zero(ret, sizeof(*ret)); ret->func = func; ret->argc = argc; ret->argv = copy_argv; return ret; }
size_t fs_ruby_tcp_parse_pack(struct fs_server* server, const BYTE* data, ssize_t len, fs_id node_id, struct fs_pack** pack){ #if 0 if(len <= 0) return 0; *pack = fs_create_empty_pack(); fs_zero(*pack, sizeof(**pack)); (*pack)->data = fs_malloc(len); (*pack)->data_stream = fs_create_input_stream((*pack)->data, len); (*pack)->len = len; (*pack)->node_id = node_id; memcpy((*pack)->data, data, len); return len; #endif if(len < pack_head_len) { return 0; } // BYTE pack_byte_order = data[0]; BYTE pack_len1 = data[1]; BYTE pack_len2 = data[2]; BYTE pack_len3 = data[3]; BYTE pack_len4 = data[4]; uint32_t pack_len = (pack_len4 << 24 | pack_len3 << 16 | pack_len2 << 8 | pack_len1); if(len >= pack_len && pack_len >= pack_head_len){ *pack = fs_create_empty_pack(); struct fs_pack* ret_pack = *pack; fs_zero(ret_pack, sizeof(*ret_pack)); ret_pack->node_id = node_id; ret_pack->data = fs_malloc(pack_len - pack_head_len/* 减去5个字节头 */); ret_pack->input_stream = fs_create_input_stream(ret_pack->data, len); ret_pack->len = pack_len - pack_head_len; ret_pack->data_input_stream_id = Qnil; ret_pack->data_output_stream_id = Qnil; memcpy(ret_pack->data, data + pack_head_len, pack_len - pack_head_len); return pack_len; } return 0; }
static FishSoundComment * fs_comment_new (const char * name, const char * value) { FishSoundComment * comment; if (!fs_comment_validate_byname (name, value)) return NULL; /* Ensures that name != NULL, value != NULL, and validates strings */ comment = fs_malloc (sizeof (FishSoundComment)); if (comment == NULL) return NULL; comment->name = fs_strdup (name); if (comment->name == NULL) { fs_free (comment); return NULL; } comment->value = fs_strdup (value); if (comment->value == NULL) { fs_free (comment->name); fs_free (comment); return NULL; } return comment; }
struct fs_node* fs_create_node(struct fs_server* server){ struct fs_node* ret = (struct fs_node*)fs_malloc(sizeof(*ret)); fs_zero(ret, sizeof(*ret)); ret->server = server; return ret; }
static char * fs_strdup (const char * s) { char * ret; if (s == NULL) return NULL; ret = fs_malloc (fs_comment_len(s) + 1); if (ret == NULL) return NULL; return strcpy (ret, s); }
FSParamsString::FSParamsString(const FSParamsString& copy): FSParamsObject<const char*>(NULL), m_data(NULL){ m_len = copy.m_len; m_data = (char*)fs_malloc(m_len + 1); this->val = m_data; this->type = PARAMS_TYPE_UTF8; memset(m_data, 0, m_len + 1); memcpy(m_data, copy.m_data, m_len); }
FSParamsString::FSParamsString(const char* val, uint32_t len): FSParamsObject<const char*>(NULL), m_data(NULL){ m_len = len; m_data = (char*)fs_malloc(len + 1); this->val = m_data; this->type = PARAMS_TYPE_UTF8; memset(m_data, 0, m_len + 1); memcpy(m_data, val, len); }
size_t fs_ruby_pack_to_data( struct fs_server* server, struct fs_pack* pack, BYTE** out){ BYTE* data = NULL; size_t len = 0; if(pack->data_output_stream_id != Qnil){ struct fs_output_stream* os = NULL; Data_Get_Struct(pack->data_output_stream_id, struct fs_output_stream, os); data = (BYTE*)fs_output_stream_get_dataptr(os); len = fs_output_stream_get_len(os); } if(pack->data_input_stream_id != Qnil){ struct fs_input_stream* is = NULL; Data_Get_Struct(pack->data_input_stream_id, struct fs_input_stream, is); data = (BYTE*)fs_input_stream_get_data_ptr(is); len = fs_input_stream_get_len(is); } if (pack->input_stream != NULL) { data = (BYTE*)fs_input_stream_get_data_ptr(pack->input_stream); len = fs_input_stream_get_len(pack->input_stream); } VALUE c_server = fs_server_get_script_id(server); VALUE byte_order = rb_funcall(c_server, rb_intern("byte_order"), 0); struct fs_output_stream* fos = fs_create_output_stream_ext; fs_stream_write_byte(fos, FIX2INT(byte_order)); fs_stream_write_int32(fos, (int32_t)len + pack_head_len); fs_stream_write_data(fos, data, len); size_t ret_len = fs_output_stream_get_len(fos); *out = fs_malloc(ret_len); memcpy(*out, fs_output_stream_get_dataptr(fos), ret_len); fs_stream_free_output(fos); if(len == 0){ rb_raise(rb_eRuntimeError, "try send data len for 0"); } return ret_len; }
struct fs_output_stream* fs_create_output_with_data( BYTE* data, size_t len){ struct fs_output_stream* ret = (struct fs_output_stream*)fs_malloc(sizeof(*ret)); fs_zero(ret, sizeof(*ret)); ret->data = data; ret->pos = 0; ret->buff_len = len; ret->weak_ref = fs_true; return ret; }
VALUE wrap_Pack_allocate (VALUE self) { struct fs_pack* p = fs_malloc(sizeof(*p)); fs_zero(p, sizeof(*p)); VALUE instance = Data_Wrap_Struct (self, NULL, wrap_Pack_free, p); p->script_id = (fs_id)instance; p->data_output_stream_id = Qnil; p->data_input_stream_id = Qnil; return instance; }
size_t fs_ruby_http_parse_pack(struct fs_server* server, const BYTE* data, ssize_t len, fs_id node_id, struct fs_pack** pack){ if(len <= 0) return 0; *pack = fs_create_empty_pack(); fs_zero(*pack, sizeof(**pack)); (*pack)->data = fs_malloc(len); (*pack)->input_stream = fs_create_input_stream((*pack)->data, len); (*pack)->len = len; (*pack)->node_id = node_id; memcpy((*pack)->data, data, len); return len; }
static char * fs_strdup_len (const char * s, size_t len) { char * ret; if (s == NULL) return NULL; if (len == 0) return NULL; len = fs_comment_clamp(len); ret = fs_malloc (len + 1); if (ret == NULL) return NULL; if (strncpy (ret, s, len) == NULL) { fs_free (ret); return NULL; } ret[len] = '\0'; return ret; }
/*-----------------------------------------------------------------------------------*/ struct fs_file * fs_open(const char *name) { struct fs_file *file; const struct fsdata_file *f; file = fs_malloc(); if(file == NULL) { return NULL; } #if LWIP_HTTPD_CUSTOM_FILES if(fs_open_custom(file, name)) { file->is_custom_file = 1; return file; } file->is_custom_file = 0; #endif /* LWIP_HTTPD_CUSTOM_FILES */ for(f = FS_ROOT; f != NULL; f = f->next) { if (!strcmp(name, (char *)f->name)) { file->data = (const char *)f->data; file->len = f->len; file->index = f->len; file->pextension = NULL; file->http_header_included = f->http_header_included; #if HTTPD_PRECALCULATED_CHECKSUM file->chksum_count = f->chksum_count; file->chksum = f->chksum; #endif /* HTTPD_PRECALCULATED_CHECKSUM */ #if LWIP_HTTPD_FILE_STATE file->state = fs_state_init(file, name); #endif /* #if LWIP_HTTPD_FILE_STATE */ return file; } } fs_free(file); return NULL; }
struct fs_pack* fs_create_empty_pack(){ struct fs_pack* ret = (struct fs_pack*)fs_malloc(sizeof(*ret)); fs_zero(ret, sizeof(*ret)); return ret; };
void FSParams::init_from_stream(struct fs_input_stream* is, fs_bool check_head){ fs_bool inval = true; if(check_head){ params_type type = (params_type)fs_stream_read_byte(is); inval = (type == PARAMS_TYPE_DOUCMENT || type == PARAMS_TYPE_ARRAY); } if(inval){ uint16_t size = fs_stream_read_uint16(is); for(int i = 0 ; i < size ; i++){ char* key = NULL; if(type_hash){ fs_stream_read_string(is, &key); }else{ key = (char*)fs_malloc(12); sprintf(key, "%d", i); } params_type child_type = (params_type)fs_stream_read_byte(is); switch (child_type) { case PARAMS_TYPE_INT: set(key, fs_stream_read_int32(is)); break; case PARAMS_TYPE_INT64: set(key, fs_stream_read_int64(is)); break; case PARAMS_TYPE_BOOL: set(key, fs_stream_read_byte(is) == 1); break; case PARAMS_TYPE_DOUBLE: set(key, fs_stream_read_double(is)); break; case PARAMS_TYPE_ARRAY: { FSParamsArray* array = new FSParamsArray; array->init_from_stream(is, false); set(key, array, PARAMS_TYPE_ARRAY); break; } case PARAMS_TYPE_DOUCMENT:{ FSParamsHash* hash = new FSParamsHash; hash->init_from_stream(is, false); set(key, hash, PARAMS_TYPE_DOUCMENT); break; } case PARAMS_TYPE_UTF8:{ char* value; size_t len = fs_stream_read_string(is, &value); set(key, (void*)value, (uint32_t)len); break; } case PARAMS_TYPE_NULL: set(key, NULL, 0, PARAMS_TYPE_NULL); break; default: break; } if(key){ fs_free(key); } } } }