void fs_node_recv_data(struct fs_node* node, BYTE* data, size_t len){ if(!node->recv_buffer){ fprintf(stderr, "fs_node_recv_data but node recvbuffer is NULL\n"); return ; } fs_stream_write_data(node->recv_buffer, data, len); const BYTE* stream_data = fs_output_stream_get_dataptr(node->recv_buffer); size_t stream_len = fs_output_stream_get_len(node->recv_buffer); struct fs_pack* pack = NULL; size_t offset = 0; do{ pack = NULL; if(node->server != NULL){ size_t pack_len = fs_server_parse_pack(node->server, stream_data + offset, stream_len - offset, node->node_id, &pack); if(pack_len){ fs_server_on_recv_pack(node->server, pack); } offset += pack_len; } }while (pack != NULL); if(offset > 0 && node->recv_buffer){ fs_output_stream_sub(node->recv_buffer, offset, stream_len - offset); } }
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; }
VALUE rb_OStream_write_data(VALUE self, VALUE v, VALUE len){ Check_Type(v, T_STRING); if(TYPE(v) == T_STRING){ struct fs_output_stream* os = NULL; Data_Get_Struct(self, struct fs_output_stream, os); const char* cStr = StringValuePtr(v); fs_stream_write_data(os, (BYTE*)cStr, FIX2INT(len)); return self; } return Qnil; }
void fs_node_send_data(struct fs_node* node, BYTE* data, size_t len){ if(fs_node_is_closed(node)){ fprintf(stderr, "Try to an closed node[%d] to send data", node->node_id); return; } pthread_mutex_lock(&node->write_mutex); if(!node->send_buffer){ fprintf(stderr, "Try to an unreachable node[%d] to send data", node->node_id); pthread_mutex_unlock(&node->write_mutex); return; } fs_stream_write_data(node->send_buffer, data, len); pthread_mutex_unlock(&node->write_mutex); if(!event_pending(node->write_ev, EV_WRITE, NULL)){ event_add(node->write_ev, NULL); } }
void fs_stream_write_float( struct fs_output_stream* stream, float v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(float)); }
void fs_stream_write_int16( struct fs_output_stream* stream, int16_t v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(int16_t)); }
void fs_stream_write_uint32( struct fs_output_stream* stream, uint32_t v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(uint32_t)); }
void fs_stream_write_byte( struct fs_output_stream* stream, BYTE v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(BYTE)); }
void fs_stream_write_ulong( struct fs_output_stream* stream, unsigned long v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(unsigned long)); }
void fs_stream_write_long( struct fs_output_stream* stream, long v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(long)); }
void fs_stream_write_string( struct fs_output_stream* stream, const char* data, size_t len ){ fs_stream_write_uint16(stream, len); fs_stream_write_data(stream, (BYTE*)data, len); }
void fs_stream_write_double( struct fs_output_stream* stream, double v){ fs_stream_write_data(stream, (BYTE*)&v, sizeof(double)); }