diff --git a/compiler/generate/t_c_generator.cc b/compiler/generate/t_c_generator.cc index 17cd7171..932736e6 100644 --- a/compiler/generate/t_c_generator.cc +++ b/compiler/generate/t_c_generator.cc @@ -122,9 +122,7 @@ class t_c_generator : public t_oop_generator { string base_type_name(t_base_type *type); string type_to_enum(t_type *type); string constant_value(string name, t_type *type, t_const_value *value); - string function_signature(t_function *tfunction); string argument_list(t_struct *tstruct); - string xception_list(t_struct *tstruct); string declare_field(t_field *tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false); /* generation functions */ @@ -135,25 +133,83 @@ class t_c_generator : public t_oop_generator { void generate_object(t_sandesh *tsandesh); void generate_object_internal(string name, const vector &members, bool is_sandesh); void generate_struct_writer(ofstream &out, string name, const vector &fields, bool is_sandesh, bool is_function=true); + void generate_struct_writer_to_buffer(ofstream &out, string name, const vector &fields, bool is_sandesh, bool is_function=true); void generate_struct_reader(ofstream &out, string name, const vector &fields, bool is_sandesh, bool is_function=true); + void generate_struct_reader_from_buffer(ofstream &out, string name, const vector &fields, bool is_sandesh, bool is_function=true); void generate_struct_deleter(ofstream &out, string name, const vector &fields, bool is_sandesh, bool is_function=true); + void generate_buffer_bounds_chk(ofstream &out, string length, int error_ret); + void generate_buffer_incr_offset(ofstream &out, string length); + + void generate_write_buffer_memcpy(ofstream &out, string source, string length, bool ref); + void generate_write_buffer_memcpy_incr_offset(ofstream &out, string source, string length, bool ref); + void generate_write_buffer_chk_memcpy_incr_offset(ofstream &out, string source, string length, int error_ret, bool ref); + void generate_write_buffer_binary(ofstream &out, string buf, string buf_len, int error_ret); + + void generate_struct_begin_writer_to_buffer(ofstream &out, string name, bool is_sandesh, int error_ret); + void generate_struct_end_writer_to_buffer(ofstream &out, bool is_sandesh); + void generate_field_begin_writer_to_buffer(ofstream &out, string key, string field_type, int error_ret); + void generate_field_end_writer_to_buffer(ofstream &out); + void generate_field_stop_writer_to_buffer(ofstream &out, int error_ret); + void generate_list_begin_writer_to_buffer(ofstream &out, string element_type, string length, int error_ret); + void generate_list_end_writer_to_buffer(ofstream &out); + + void generate_serialize_bool_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_byte_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_i16_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_i32_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_i64_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_u16_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_u32_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_u64_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_string_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_xml_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_ipaddr_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_uuid_t_to_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_double_to_buffer(ofstream &out, string name, int error_ret); + + void generate_read_buffer_memcpy(ofstream &out, string dest, string length, bool ref); + void generate_read_buffer_memcpy_incr_offset(ofstream &out, string dest, string length, bool ref); + void generate_read_buffer_chk_memcpy_incr_offset(ofstream &out, string dest, string length, int error_ret, bool ref); + + void generate_struct_begin_reader_from_buffer(ofstream &out, string name, bool is_sandesh, int error_ret); + void generate_struct_end_reader_from_buffer(ofstream &out, bool is_sandesh); + void generate_field_begin_reader_from_buffer(ofstream &out, string field_type, string field_id, int error_ret); + void generate_field_end_reader_from_buffer(ofstream &out); + void generate_list_begin_reader_from_buffer(ofstream &out, string element_size, int error_ret); + void generate_list_end_reader_from_buffer(ofstream &out); + + void generate_deserialize_bool_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_byte_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_i16_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_i32_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_i64_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_u16_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_u32_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_u64_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_string_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_xml_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_ipaddr_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_uuid_t_from_buffer(ofstream &out, string name, int error_ret); + void generate_deserialize_double_from_buffer(ofstream &out, string name, int error_ret); + void generate_serialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret); + void generate_serialize_field_to_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret); void generate_serialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret); + void generate_serialize_struct_to_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret); void generate_serialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret); - void generate_serialize_map_element(ofstream &out, t_map *tmap, string key, string value, int error_ret); - void generate_serialize_set_element(ofstream &out, t_set *tset, string element, int error_ret); + void generate_serialize_container_to_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret); void generate_serialize_list_element(ofstream &out, t_list *tlist, string list, string index, int error_ret); + void generate_serialize_list_element_to_buffer(ofstream &out, t_list *tlist, string list, string index, int error_ret); void generate_deserialize_field(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret); + void generate_deserialize_field_from_buffer(ofstream &out, t_field *tfield, string prefix, string suffix, int error_ret); void generate_deserialize_struct(ofstream &out, t_struct *tstruct, string prefix, int error_ret); + void generate_deserialize_struct_from_buffer(ofstream &out, t_struct *tstruct, string prefix, int error_ret); void generate_deserialize_container(ofstream &out, t_type *ttype, string prefix, int error_ret); - void generate_deserialize_map_element(ofstream &out, t_map *tmap, string prefix, int error_ret); - void generate_deserialize_set_element(ofstream &out, t_set *tset, string prefix, int error_ret); + void generate_deserialize_container_from_buffer(ofstream &out, t_type *ttype, string prefix, int error_ret); void generate_deserialize_list_element(ofstream &out, t_list *tlist, string prefix, string index, int error_ret); - - string generate_new_hash_from_type(t_type * ttype); - string generate_new_array_from_type(t_type * ttype); + void generate_deserialize_list_element_from_buffer(ofstream &out, t_list *tlist, string prefix, string index, int error_ret); }; /** @@ -400,24 +456,30 @@ void t_c_generator::generate_sandesh_info () { string sandesh_name_u = initial_caps_to_underscores(sandesh_name); out << indent() << "{" << endl; indent_up(); - out << indent() << ".name = \"" << sandesh_name << "\"," << endl; - out << indent() << ".size = sizeof(" << sandesh_name << ")," << endl; - out << indent() << ".read = " << sandesh_name_u << "_read," << endl; - out << indent() << ".write = " << sandesh_name_u << "_write," << endl; - out << indent() << ".process = " << sandesh_name_u << "_process," << endl; - out << indent() << ".free = " << sandesh_name_u << "_free," << endl; + out << + indent() << ".name = \"" << sandesh_name << "\"," << endl << + indent() << ".size = sizeof(" << sandesh_name << ")," << endl << + indent() << ".read = " << sandesh_name_u << "_read," << endl << + indent() << ".read_binary_from_buffer = " << sandesh_name_u << "_read_binary_from_buffer," << endl << + indent() << ".write = " << sandesh_name_u << "_write," << endl << + indent() << ".write_binary_to_buffer = " << sandesh_name_u << "_write_binary_to_buffer," << endl << + indent() << ".process = " << sandesh_name_u << "_process," << endl << + indent() << ".free = " << sandesh_name_u << "_free," << endl; indent_down(); out << indent() << "}," << endl; } // Add end of array marker out << indent() << "{" << endl; indent_up(); - out << indent() << ".name = NULL," << endl; - out << indent() << ".size = 0," << endl; - out << indent() << ".read = NULL," << endl; - out << indent() << ".write = NULL," << endl; - out << indent() << ".process = NULL," << endl; - out << indent() << ".free = NULL," << endl; + out << + indent() << ".name = NULL," << endl << + indent() << ".size = 0," << endl << + indent() << ".read = NULL," << endl << + indent() << ".read_binary_from_buffer = NULL," << endl << + indent() << ".write = NULL," << endl << + indent() << ".write_binary_to_buffer = NULL," << endl << + indent() << ".process = NULL," << endl << + indent() << ".free = NULL," << endl; indent_down(); out << indent() << "}, " << endl; indent_down(); @@ -571,7 +633,7 @@ string t_c_generator::base_type_name(t_base_type *type) { case t_base_type::TYPE_IPADDR: return "ipaddr_t"; case t_base_type::TYPE_UUID: - return "uuid_t"; + return "ct_uuid_t"; default: throw "compiler error: no C base type name for base type " + t_base_type::t_base_name (tbase); @@ -702,30 +764,6 @@ string t_c_generator::constant_value(string name, t_type *type, t_const_value *v return render.str(); } -/** - * Renders a function signature of the form 'type name(args)' - * - * @param tfunction Function definition - * @return String of rendered function definition - */ -string t_c_generator::function_signature(t_function* tfunction) { - t_type* ttype = tfunction->get_returntype(); - t_struct* arglist = tfunction->get_arglist(); - t_struct* xlist = tfunction->get_xceptions(); - string fname = initial_caps_to_underscores(tfunction->get_name()); - - bool has_return = !ttype->is_void(); - bool has_args = arglist->get_members().size() == 0; - bool has_xceptions = xlist->get_members().size() == 0; - return - "gboolean " + this->nspace_lc + fname + " (" + this->nspace - + service_name_ + "If * iface" - + (has_return ? ", " + type_name(ttype) + "* _return" : "") - + (has_args ? "" : (", " + argument_list (arglist))) - + (has_xceptions ? "" : (", " + xception_list (xlist))) - + ", GError ** error)"; -} - /** * Renders a field list * @@ -750,31 +788,6 @@ string t_c_generator::argument_list (t_struct* tstruct) { return result; } -/** - * Renders mutable exception lists - * - * @param tstruct The struct definition - * @return Comma sepearated list of all field names in that struct - */ -string t_c_generator::xception_list (t_struct* tstruct) { - string result = ""; - - const vector& fields = tstruct->get_members(); - vector::const_iterator f_iter; - bool first = true; - for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { - if (first) { - first = false; - } else { - result += ", "; - } - result += type_name((*f_iter)->get_type(), false, false) + "* " + - (*f_iter)->get_name(); - } - return result; -} - - /** * Declares a field, including any necessary initialization. */ @@ -941,11 +954,21 @@ void t_c_generator::generate_object_internal(string name, this->nspace_lc << name_u << "_write (" << dtname << "* w" << dvname << ", ThriftProtocol *protocol, int *error);" << endl; + f_types_ << + "int32_t " << + this->nspace_lc << name_u << + "_write_binary_to_buffer (" << dtname << + "* w" << dvname << ", uint8_t *buf, const size_t buf_len, int *error);" << endl; f_types_ << "int32_t " << this->nspace_lc << name_u << "_read (" << dtname << "* r" << dvname << ", ThriftProtocol *protocol, int *error);" << endl; + f_types_ << + "int32_t " << + this->nspace_lc << name_u << + "_read_binary_from_buffer (" << dtname << + "* r" << dvname << ", uint8_t *buf, const size_t buf_len, int *error);" << endl; f_types_ << "void " << this->nspace_lc << name_u << @@ -961,7 +984,9 @@ void t_c_generator::generate_object_internal(string name, // start writing the object implementation .c file // generate struct I/O methods generate_struct_reader (f_types_impl_, name, members, is_sandesh); + generate_struct_reader_from_buffer (f_types_impl_, name, members, is_sandesh); generate_struct_writer (f_types_impl_, name, members, is_sandesh); + generate_struct_writer_to_buffer (f_types_impl_, name, members, is_sandesh); generate_struct_deleter (f_types_impl_, name, members, is_sandesh); } @@ -1047,6 +1072,244 @@ void t_c_generator::generate_struct_deleter (ofstream &out, endl; } +void t_c_generator::generate_buffer_bounds_chk(ofstream &out, + string length, + int error_ret) { + out << + indent() << "if (Xbuflen < Xoffset + " << length << ") {" << endl << + indent() << " return " << error_ret << ";" << endl << + indent() << "}" << endl; +} + +void t_c_generator::generate_buffer_incr_offset(ofstream &out, + string length) { + out << + indent() << "Xoffset += " << length << ";" << endl; +} + +void t_c_generator::generate_write_buffer_memcpy(ofstream &out, + string source, + string length, + bool ref) { + if (ref) { + out << + indent() << "memcpy (Xbuf + Xoffset, &" << source << ", " << + length << ");" << endl; + } else { + out << + indent() << "memcpy (Xbuf + Xoffset, " << source << ", " << + length << ");" << endl; + } +} + +void t_c_generator::generate_read_buffer_memcpy(ofstream &out, + string dest, + string length, + bool ref) { + if (ref) { + out << + indent() << "memcpy (&" << dest << ", Xbuf + Xoffset, " << + length << ");" << endl; + } else { + out << + indent() << "memcpy (" << dest << ", Xbuf + Xoffset, " << + length << ");" << endl; + } +} + +void t_c_generator::generate_write_buffer_memcpy_incr_offset(ofstream &out, + string source, + string length, + bool ref) { + generate_write_buffer_memcpy (out, source, length, ref); + generate_buffer_incr_offset (out, length); +} + +void t_c_generator::generate_read_buffer_memcpy_incr_offset(ofstream &out, + string dest, + string length, + bool ref) { + generate_read_buffer_memcpy (out, dest, length, ref); + generate_buffer_incr_offset (out, length); +} + +void t_c_generator::generate_write_buffer_chk_memcpy_incr_offset(ofstream &out, + string source, + string length, + int error_ret, + bool ref) { + generate_buffer_bounds_chk (out, length, error_ret); + generate_write_buffer_memcpy_incr_offset (out, source, length, ref); +} + +void t_c_generator::generate_read_buffer_chk_memcpy_incr_offset(ofstream &out, + string dest, + string length, + int error_ret, + bool ref) { + generate_buffer_bounds_chk (out, length, error_ret); + generate_read_buffer_memcpy_incr_offset (out, dest, length, ref); +} + +void t_c_generator::generate_write_buffer_binary(ofstream &out, string buf, + string buf_len, int error_ret) { + string nbuf_len(tmp("Xnbuflen")); + generate_buffer_bounds_chk (out, "4", error_ret); + scope_up(out); + out << + indent() << "int32_t " << nbuf_len << " = htonl (" << + buf_len << ");" << endl; + generate_write_buffer_memcpy_incr_offset (out, nbuf_len, "4", true); + scope_down(out); + generate_write_buffer_chk_memcpy_incr_offset (out, buf, buf_len, error_ret, + false); +} + +void t_c_generator::generate_struct_begin_writer_to_buffer(ofstream &out, + string name, + bool is_sandesh, + int error_ret) { + if (is_sandesh) { + ostringstream os; + os << name.length(); + string name_len(os.str()); + out << + indent() << "/* thrift_protocol_write_sandesh_begin */" << endl; + generate_write_buffer_binary (out, "\"" + name + "\"", name_len, + error_ret); + } else { + out << indent() << "/* thrift_protocol_write_struct_begin */" << endl; + } +} + +void t_c_generator::generate_struct_begin_reader_from_buffer(ofstream &out, + string name, + bool is_sandesh, + int error_ret) { + if (is_sandesh) { + out << + indent() << "/* thrift_protocol_read_sandesh_begin */" << endl; + generate_deserialize_string_from_buffer (out, name, error_ret); + out << + indent() << "if (" << name << ") { os_free (" << name << "); " << + name << " = NULL; }" << endl << endl; + } else { + out << indent() << "/* thrift_protocol_read_struct_begin */" << endl; + } +} + +void t_c_generator::generate_struct_end_writer_to_buffer(ofstream &out, + bool is_sandesh) { + if (is_sandesh) { + out << indent() << "/* thrift_protocol_write_sandesh_send */" << endl; + } else { + out << indent() << "/* thrift_protocol_write_struct_send */" << endl; + } +} + +void t_c_generator::generate_struct_end_reader_from_buffer(ofstream &out, + bool is_sandesh) { + if (is_sandesh) { + out << indent() << "/* thrift_protocol_read_sandesh_send */" << endl; + } else { + out << indent() << "/* thrift_protocol_read_struct_send */" << endl; + } +} + +void t_c_generator::generate_field_stop_writer_to_buffer(ofstream &out, + int error_ret) { + string field_stop(tmp("Xfield_stop")); + out << endl << + indent() << "/* thrift_protocol_field_stop */" << endl; + generate_buffer_bounds_chk (out, "1", error_ret); + scope_up(out); + out << + indent() << "int8_t " << field_stop << " = (int8_t) T_STOP;" << endl; + generate_write_buffer_memcpy_incr_offset (out, field_stop, "1", true); + scope_down(out); +} + +void t_c_generator::generate_struct_writer_to_buffer(ofstream &out, + string name, + const vector &fields, + bool is_sandesh, + bool is_function) { + string name_u = initial_caps_to_underscores(name); + string wname = "w" + name_u; + string sname = is_sandesh ? "sandesh" : "struct"; + string dname = is_sandesh ? "void" : name; + string dwname = is_sandesh ? "wsandesh" : wname; + + vector ::const_iterator f_iter; + int error_ret = 0; + + if (is_function) { + error_ret = -1; + indent(out) << + "int32_t" << endl << + this->nspace_lc << name_u << "_write_binary_to_buffer (" << + dname << "* " << dwname << + ", uint8_t *Xbuf, const size_t Xbuflen, int *Xerror)" << endl; + } + indent(out) << "{" << endl; + indent_up(); + + out << + indent() << "int32_t Xret;" << endl << + indent() << "int32_t Xoffset = 0;" << endl << + indent() << "u_int32_t Xi;" << endl << + endl; + + if (is_sandesh) { + out << indent() << name << "* " << wname << " = " << + "(" << name << "* ) " << dwname << ";" << + endl << endl; + } + + // satisfy -Wall in case we don't use some variables + out << + indent() << "/* satisfy -Wall in case these aren't used */" << endl << + indent() << "THRIFT_UNUSED_VAR (Xret);" << endl << + indent() << "THRIFT_UNUSED_VAR (Xi);" << endl << + indent() << "THRIFT_UNUSED_VAR (" << wname << ");" << endl; + + out << endl; + + generate_struct_begin_writer_to_buffer(out, name, is_sandesh, error_ret); + + for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { + // Ignore auto generated fields + if ((*f_iter)->get_auto_generated()) { + continue; + } + if ((*f_iter)->get_req() == t_field::T_OPTIONAL) { + indent(out) << "if (" << wname << "->__isset_" << (*f_iter)->get_name() << " == 1) {" << endl; + indent_up(); + } + ostringstream key_os; + key_os << (*f_iter)->get_key(); + string key(key_os.str()); + string tenum(type_to_enum((*f_iter)->get_type())); + generate_field_begin_writer_to_buffer (out, key, tenum, error_ret); + generate_serialize_field_to_buffer (out, *f_iter, wname + "->", "", error_ret); + generate_field_end_writer_to_buffer (out); + if ((*f_iter)->get_req() == t_field::T_OPTIONAL) { + indent(out) << "if (" << wname << "->__isset_" << (*f_iter)->get_name() << " == 1) {" << endl; + indent_up(); + } + } + generate_field_stop_writer_to_buffer(out, error_ret); + generate_struct_end_writer_to_buffer(out, is_sandesh); + if (is_function) { + indent(out) << "return Xoffset;" << endl; + } + + indent_down(); + indent(out) << + "}" << endl << + endl; +} + /** * Generates functions to write Thrift structures and sandeshs to a stream. */ @@ -1349,33 +1612,637 @@ void t_c_generator::generate_struct_reader(ofstream &out, endl; } -void t_c_generator::generate_serialize_field(ofstream &out, - t_field *tfield, - string prefix, - string suffix, - int error_ret) { - t_type *type = get_true_type (tfield->get_type()); - string name = prefix + tfield->get_name() + suffix; +/** + * Generates code to read Thrift structures from a buffer. + */ +void t_c_generator::generate_struct_reader_from_buffer(ofstream &out, + string name, + const vector &fields, + bool is_sandesh, + bool is_function) { + string name_u = initial_caps_to_underscores(name); + string rname = "r" + name_u; + int error_ret = 0; + vector ::const_iterator f_iter; + string sname = is_sandesh ? "sandesh" : "struct"; + string dname = is_sandesh ? "void" : name; + string drname = is_sandesh ? "rsandesh" : rname; - if (type->is_void()) { - throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name; + if (is_function) { + error_ret = -1; + indent(out) << + "/* reads a " << name_u << " " << sname << " */" << endl << + "int32_t" << endl << + this->nspace_lc << name_u << + "_read_binary_from_buffer (" << dname << + " *" << drname << ", uint8_t *Xbuf, const size_t Xbuflen, int *Xerror)" << endl; } - if (type->is_struct() || type->is_xception()) { - generate_serialize_struct (out, (t_struct *) type, name, error_ret); - } else if (type->is_container()) { - generate_serialize_container (out, type, name, error_ret); - } else if (type->is_base_type() || type->is_enum()) { + indent(out) << "{" << endl; + indent_up(); + + // declare stack temp variables + out << + indent() << "int32_t Xret;" << endl << + indent() << "int32_t Xoffset = 0;" << endl << + indent() << "char *Xname;" << endl << + indent() << "ThriftType Xftype;" << endl << + indent() << "int16_t Xfid;" << endl << + indent() << "u_int32_t Xi;" << endl; + + if (is_sandesh) { + out << indent() << name << "* " << rname << " = " << + "(" << name << "* ) " << drname << ";" << endl; + } + + for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { + // Ignore auto generated fields + if ((*f_iter)->get_auto_generated()) { + continue; + } + if ((*f_iter)->get_req() == t_field::T_REQUIRED) { + indent(out) << "u_int8_t isset_" << (*f_iter)->get_name() << " = 0;" << endl; + } + } + + // satisfy -Wall in case we don't use some variables + out << + indent() << "/* satisfy -Wall in case these aren't used */" << endl << + indent() << "THRIFT_UNUSED_VAR (Xret);" << endl << + indent() << "THRIFT_UNUSED_VAR (Xname);" << endl << + indent() << "THRIFT_UNUSED_VAR (Xi);" << endl << + indent() << "THRIFT_UNUSED_VAR (" << rname << ");" << endl; + + out << endl; + + // read the beginning of the structure marker + out << + indent() << "/* read the " << sname << " begin marker */" << endl; + generate_struct_begin_reader_from_buffer (out, "Xname", is_sandesh, error_ret); + + // read the struct fields + out << + indent() << "/* read the struct fields */" << endl << + indent() << "while (1)" << endl; + scope_up(out); + + // read beginning field marker + out << + indent() << "/* read the beginning of a field */" << endl; + generate_field_begin_reader_from_buffer (out, "Xftype", "Xfid", error_ret); + out << endl; + + // switch depending on the field type + indent(out) << + "switch (Xfid)" << endl; + + // start switch + scope_up(out); + + // generate deserialization code for known types + for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { + // Ignore auto generated fields + if ((*f_iter)->get_auto_generated()) { + continue; + } indent(out) << - "if ((ret = thrift_protocol_write_"; + "case " << (*f_iter)->get_key() << ":" << endl; + indent_up(); + indent(out) << + "if (Xftype == " << type_to_enum ((*f_iter)->get_type()) << ")" << endl; + indent(out) << + "{" << endl; - if (type->is_base_type()) { - t_base_type::t_base tbase = ((t_base_type *) type)->get_base(); - switch (tbase) { - case t_base_type::TYPE_VOID: - throw "compiler error: cannot serialize void field in a struct: " - + name; - break; + + indent_up(); + // generate deserialize field + generate_deserialize_field_from_buffer (out, *f_iter, rname + "->", "", error_ret); + indent_down(); + + out << + indent() << "} else {" << endl << + indent() << " if ((Xret = thrift_binary_protocol_skip_from_buffer (Xbuf + Xoffset, Xbuflen - Xoffset, Xftype, Xerror)) < 0)" << endl << + indent() << " return " << error_ret << ";" << endl << + indent() << " Xoffset += Xret;" << endl << + indent() << "}" << endl << + indent() << "break;" << endl; + indent_down(); + } + + // create the default case + out << + indent() << "default:" << endl << + indent() << " if ((Xret = thrift_binary_protocol_skip_from_buffer (Xbuf + Xoffset, Xbuflen - Xoffset, Xftype, Xerror)) < 0)" << endl << + indent() << " return " << error_ret << ";" << endl << + indent() << " Xoffset += Xret;" << endl << + indent() << " break;" << endl; + + // end switch + scope_down(out); + + // read field end marker + generate_field_end_reader_from_buffer (out); + + // end while loop + scope_down(out); + out << endl; + + // read the end of the structure + generate_struct_end_reader_from_buffer (out, is_sandesh); + + // if a required field is missing, throw an error + for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { + // Ignore auto generated fields + if ((*f_iter)->get_auto_generated()) { + continue; + } + if ((*f_iter)->get_req() == t_field::T_REQUIRED) { + out << + indent() << "if (!isset_" << (*f_iter)->get_name() << ")" << endl << + indent() << "{" << endl << + indent() << " *error = THRIFT_PROTOCOL_ERROR_INVALID_DATA;" << endl << + indent() << " syslog(LOG_ERROR, \"THRIFT_PROTOCOL_ERROR: missing field\");" << endl << + indent() << " return -1;" << endl << + indent() << "}" << endl << + endl; + } + } + + if (is_function) { + indent(out) << + "return Xoffset;" << endl; + } + + // end the function/structure + indent_down(); + indent(out) << + "}" << endl << + endl; +} + +void t_c_generator::generate_field_begin_writer_to_buffer(ofstream &out, + string key, + string field_type, + int error_ret) { + out << + indent() << "/* thrift_protocol_write_field_begin */" << endl; + generate_buffer_bounds_chk (out, "1", error_ret); + string field_type8(tmp("Xfield_type8")); + scope_up(out); + out << + indent() << "int8_t " << field_type8 << " = (int8_t) " << + field_type << ";" << endl; + generate_write_buffer_memcpy_incr_offset (out, field_type8, + "1", true); + scope_down(out); + generate_buffer_bounds_chk (out, "2", error_ret); + string nkey(tmp("Xnfield_id")); + scope_up(out); + out << + indent() << "int16_t " << nkey << " = htons (" << key << ");" << endl; + generate_write_buffer_memcpy_incr_offset (out, nkey, "2", true); + scope_down(out); + out << endl; +} + +void t_c_generator::generate_field_begin_reader_from_buffer(ofstream &out, + string field_type, + string field_id, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_field_begin */" << endl; + generate_deserialize_byte_from_buffer (out, field_type, error_ret); + // check for field STOP marker + out << + indent() << "/* break if we get a STOP field */" << endl << + indent() << "if (" << field_type << " == T_STOP)" << endl << + indent() << "{" << endl << + indent() << " break;" << endl << + indent() << "}" << endl; + out << + indent() << " else" << endl; + scope_up(out); + generate_deserialize_i16_from_buffer (out, field_id, error_ret); + scope_down(out); +} + +void t_c_generator::generate_field_end_writer_to_buffer(ofstream &out) { + out << + indent() << "/* thrift_protocol_write_field_end */" << endl; +} + +void t_c_generator::generate_field_end_reader_from_buffer(ofstream &out) { + out << + indent() << "/* thrift_protocol_read_field_end */" << endl; +} + +void t_c_generator::generate_serialize_bool_to_buffer(ofstream &out, + string name, + int error_ret) { + string nbool(tmp("Xbool")); + out << + indent() << "/* thrift_protocol_write_bool */" << endl; + scope_up(out); + out << + indent() << "uint8_t " << nbool << " = " << name << " ? 1 : 0;" << endl; + generate_serialize_byte_to_buffer (out, nbool, error_ret); + scope_down(out); +} + +void t_c_generator::generate_deserialize_bool_from_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_bool */" << endl; + scope_up(out); + out << indent() << "void * b[1];" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "b", "1", error_ret, + false); + out << + indent() << name << " = *(int8_t *) b != 0;" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_byte_to_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_write_byte */" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, name, "1", error_ret, + true); +} + +void t_c_generator::generate_deserialize_byte_from_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_byte */" << endl; + scope_up(out); + out << indent() << "void * b[1];" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "b", "1", error_ret, + false); + out << + indent() << name << " = *(int8_t *) b;" << endl; + scope_down(out);; +} + +void t_c_generator::generate_serialize_i16_to_buffer(ofstream &out, + string name, + int error_ret) { + string ni16(tmp("Xni16")); + out << indent() << "/* thrift_protocol_write_i16 */" << endl; + scope_up(out); + out << + indent() << "int16_t " << ni16 << " = htons (" << name << ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, ni16, "2", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_i16_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_i16 */" << endl; + scope_up(out); + out << + indent() << "union {"<< endl << + indent() << " void * b[2];" << endl << + indent() << " int16_t all;" << endl << + indent() << "} bytes;" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "2", error_ret, + false); + out << + indent() << name << " = ntohs (bytes.all);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_u16_to_buffer(ofstream &out, + string name, + int error_ret) { + string nu16(tmp("Xnu16")); + out << indent() << "/* thrift_protocol_write_u16 */" << endl; + scope_up(out); + out << + indent() << "uint16_t " << nu16 << " = htons (" << name << ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, nu16, "2", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_u16_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_u16 */" << endl; + scope_up(out); + out << + indent() << "union {"<< endl << + indent() << " void * b[2];" << endl << + indent() << " uint16_t all;" << endl << + indent() << "} bytes;" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "2", error_ret, + false); + out << + indent() << name << " = ntohs (bytes.all);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_i32_to_buffer(ofstream &out, + string name, + int error_ret) { + string ni32(tmp("Xni32")); + out << indent() << "/* thrift_protocol_write_i32 */" << endl; + scope_up(out); + out << + indent() << "int32_t " << ni32 << " = htonl (" << name << ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, ni32, "4", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_i32_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_i32 */" << endl; + scope_up(out); + out << + indent() << "union {"<< endl << + indent() << " void * b[4];" << endl << + indent() << " int32_t all;" << endl << + indent() << "} bytes;" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "4", error_ret, + false); + out << + indent() << name << " = ntohl (bytes.all);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_u32_to_buffer(ofstream &out, + string name, + int error_ret) { + string nu32(tmp("Xnu32")); + out << indent() << "/* thrift_protocol_write_u32 */" << endl; + scope_up(out); + out << + indent() << "uint32_t " << nu32 << " = htonl (" << name << ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, nu32, "4", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_u32_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_u32 */" << endl; + scope_up(out); + out << + indent() << "union {"<< endl << + indent() << " void * b[4];" << endl << + indent() << " uint32_t all;" << endl << + indent() << "} bytes;" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "bytes.b", "4", error_ret, + false); + out << + indent() << name << " = ntohl (bytes.all);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_i64_to_buffer(ofstream &out, + string name, + int error_ret) { + string ni64(tmp("Xni64")); + out << indent() << "/* thrift_protocol_write_i64 */" << endl; + scope_up(out); + out << + indent() << "int64_t " << ni64 << ";" << endl << + indent() << "os_put_value64((uint8_t *)&" << ni64 << ", " << name << + ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, ni64, "8", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_i64_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_i64 */" << endl; + scope_up(out); + out << + indent() << "void * b[8];" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "b", "8", error_ret, + false); + out << + indent() << name << " = os_get_value64((uint8_t *)b);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_u64_to_buffer(ofstream &out, + string name, + int error_ret) { + string nu64(tmp("Xnu64")); + out << indent() << "/* thrift_protocol_write_u64 */" << endl; + scope_up(out); + out << + indent() << "uint64_t " << nu64 << ";" << endl << + indent() << "os_put_value64((uint8_t *)&" << nu64 << ", " << name << + ");" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, nu64, "8", error_ret, + true); + scope_down(out); +} + +void t_c_generator::generate_deserialize_u64_from_buffer(ofstream &out, + string name, + int error_ret) { + out << indent() << "/* thrift_protocol_read_u64 */" << endl; + scope_up(out); + out << + indent() << "void * b[8];" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, "b", "8", error_ret, + false); + out << + indent() << name << " = os_get_value64((uint8_t *)b);" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_xml_to_buffer(ofstream &out, + string name, + int error_ret) { + generate_serialize_string_to_buffer(out, name, error_ret); +} + +void t_c_generator::generate_deserialize_xml_from_buffer(ofstream &out, + string name, + int error_ret) { + generate_deserialize_string_from_buffer(out, name, error_ret); +} + +void t_c_generator::generate_serialize_string_to_buffer(ofstream &out, + string name, + int error_ret) { + string s_strlen(tmp("Xsstrlen")); + out << indent() << "/* thrift_protocol_write_string */" << endl; + scope_up(out); + out << + indent() << "int32_t " << s_strlen << " = " << name << + " != NULL ? strlen (" << name << ") : 0;" << endl; + generate_write_buffer_binary (out, name, s_strlen, error_ret); + scope_down(out); +} + +void t_c_generator::generate_deserialize_string_from_buffer(ofstream &out, + string name, + int error_ret) { + string s_strlen(tmp("Xread_len")); + out << indent() << "/* thrift_protocol_read_string */" << endl; + scope_up(out); + out << + indent() << "int32_t " << s_strlen << " = 0;" << endl; + generate_deserialize_i32_from_buffer (out, s_strlen, error_ret); + out << + indent() << "if (" << s_strlen << " > 0)" << endl; + scope_up(out); + out << + indent() << "if (Xbuflen < Xoffset + " << s_strlen << ") {" << endl << + indent() << " return " << error_ret << ";" << endl << + indent() << "}" << endl << + indent() << name << " = os_malloc (" << s_strlen << " + 1);" << endl; + generate_read_buffer_memcpy_incr_offset (out, name, s_strlen, false); + out << + indent() << name << "[" << s_strlen << "] = 0;" << endl; + scope_down(out); + out << + indent() << " else" << endl; + scope_up(out); + out << + indent() << name << " = NULL;" << endl; + scope_down(out); + scope_down(out); +} + +void t_c_generator::generate_serialize_ipaddr_to_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_write_ipaddr */" << endl; + string iptype(name + ".iptype"); + out << + indent() << "if (" << iptype << " == AF_INET)" << endl; + scope_up(out); + generate_write_buffer_chk_memcpy_incr_offset (out, iptype, "1", error_ret, + true); + string ipv4(name + ".ipv4"); + generate_write_buffer_chk_memcpy_incr_offset (out, ipv4, "4", error_ret, + true); + scope_down(out); + out << + indent() << " else if (" << iptype << " == AF_INET6)" << endl; + scope_up(out); + generate_write_buffer_chk_memcpy_incr_offset (out, iptype, "1", error_ret, + true); + string ipv6(name + ".ipv6"); + generate_write_buffer_chk_memcpy_incr_offset (out, ipv6, "16", error_ret, + true); + scope_down(out); + out << + indent() << " else" << endl; + scope_up(out); + out << + indent() << "return -1;" << endl; + scope_down(out); +} + +void t_c_generator::generate_deserialize_ipaddr_from_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_ipaddr */" << endl; + string iptype(name + ".iptype"); + generate_deserialize_byte_from_buffer (out, iptype, error_ret); + out << + indent() << "if (" << iptype << " == AF_INET)" << endl; + scope_up(out); + string ipv4(name + ".ipv4"); + generate_read_buffer_chk_memcpy_incr_offset (out, ipv4, "4", error_ret, + true); + scope_down(out); + out << + indent() << "else if (" << iptype << " == AF_INET6)" << endl; + scope_up(out); + string ipv6(name + ".ipv6"); + generate_read_buffer_chk_memcpy_incr_offset (out, ipv6, "16", error_ret, + true); + scope_down(out); + out << + indent() << " else" << endl; + scope_up(out); + out << + indent() << "return -1;" << endl; + scope_down(out); +} + +void t_c_generator::generate_serialize_uuid_t_to_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_write_uuid_t */" << endl; + generate_write_buffer_chk_memcpy_incr_offset (out, name, "16", error_ret, + true); +} + +void t_c_generator::generate_deserialize_uuid_t_from_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_uuid_t */" << endl; + generate_read_buffer_chk_memcpy_incr_offset (out, name, "16", error_ret, + true); +} + +void t_c_generator::generate_serialize_double_to_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_write_double */" << endl << + indent() << "/* NOT SUPPORTED */" << endl << + indent() << "return " << error_ret << ";" << endl; +} + +void t_c_generator::generate_deserialize_double_from_buffer(ofstream &out, + string name, + int error_ret) { + out << + indent() << "/* thrift_protocol_read_double */" << endl << + indent() << "/* NOT SUPPORTED */" << endl << + indent() << "return " << error_ret << ";" << endl; +} + +void t_c_generator::generate_serialize_field(ofstream &out, + t_field *tfield, + string prefix, + string suffix, + int error_ret) { + t_type *type = get_true_type (tfield->get_type()); + string name = prefix + tfield->get_name() + suffix; + + if (type->is_void()) { + throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name; + } + + if (type->is_struct() || type->is_xception()) { + generate_serialize_struct (out, (t_struct *) type, name, error_ret); + } else if (type->is_container()) { + generate_serialize_container (out, type, name, error_ret); + } else if (type->is_base_type() || type->is_enum()) { + indent(out) << + "if ((ret = thrift_protocol_write_"; + + if (type->is_base_type()) { + t_base_type::t_base tbase = ((t_base_type *) type)->get_base(); + switch (tbase) { + case t_base_type::TYPE_VOID: + throw "compiler error: cannot serialize void field in a struct: " + + name; + break; case t_base_type::TYPE_BOOL: out << "bool (protocol, " << name; break; @@ -1411,9 +2278,7 @@ void t_c_generator::generate_serialize_field(ofstream &out, break; case t_base_type::TYPE_STRING: if (((t_base_type *) type)->is_binary()) { - out << "binary (protocol, ((GByteArray *) " << name << - ")->data, ((GByteArray *) " << name << - ")->len"; + throw "CANNOT GENERATE SERIALIZE CODE FOR binary TYPE: " + name; } else { out << "string (protocol, " << name; } @@ -1439,6 +2304,93 @@ void t_c_generator::generate_serialize_field(ofstream &out, } } +void t_c_generator::generate_serialize_field_to_buffer(ofstream &out, + t_field *tfield, + string prefix, + string suffix, + int error_ret) { + t_type *type = get_true_type (tfield->get_type()); + string name = prefix + tfield->get_name() + suffix; + ostringstream key_os; + key_os << tfield->get_key(); + string key(key_os.str()); + string tenum(type_to_enum(tfield->get_type())); + + if (type->is_void()) { + throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name; + } + + if (type->is_struct() || type->is_xception()) { + generate_serialize_struct_to_buffer (out, (t_struct *) type, name, error_ret); + } else if (type->is_container()) { + generate_serialize_container_to_buffer (out, type, name, error_ret); + } else if (type->is_base_type() || type->is_enum()) { + if (type->is_base_type()) { + t_base_type::t_base tbase = ((t_base_type *) type)->get_base(); + switch (tbase) { + case t_base_type::TYPE_VOID: + throw "compiler error: cannot serialize void field in a struct: " + + name; + break; + case t_base_type::TYPE_BOOL: + generate_serialize_bool_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_BYTE: + generate_serialize_byte_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I16: + generate_serialize_i16_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I32: + generate_serialize_i32_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I64: + generate_serialize_i64_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U16: + generate_serialize_u16_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U32: + generate_serialize_u32_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U64: + generate_serialize_u64_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_DOUBLE: + generate_serialize_double_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_IPV4: + generate_serialize_i32_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_IPADDR: + generate_serialize_ipaddr_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_STRING: + if (((t_base_type *) type)->is_binary()) { + throw "CANNOT GENERATE SERIALIZE CODE FOR binary TYPE: " + name; + } else { + generate_serialize_string_to_buffer (out, name, error_ret); + } + break; + case t_base_type::TYPE_XML: + generate_serialize_xml_to_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_UUID: + generate_serialize_uuid_t_to_buffer (out, name, error_ret); + break; + default: + throw "compiler error: no C writer for base type " + + t_base_type::t_base_name (tbase) + name; + } + } else if (type->is_enum()) { + generate_serialize_i32_to_buffer (out, name, error_ret); + } + } else { + printf ("DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n", + name.c_str(), type_name (type).c_str()); + } +} + void t_c_generator::generate_serialize_struct(ofstream &out, t_struct *tstruct, string prefix, @@ -1449,6 +2401,20 @@ void t_c_generator::generate_serialize_struct(ofstream &out, endl; } +void t_c_generator::generate_serialize_struct_to_buffer(ofstream &out, + t_struct *tstruct, + string prefix, + int error_ret) { + out << + indent() << "if ((Xret = " << tstruct->get_name() << + "_write_binary_to_buffer (" << prefix << + ", Xbuf + Xoffset, Xbuflen - Xoffset, Xerror)) < 0)" << endl; + out << + indent() << " return " << error_ret << ";" << endl << + indent() << "Xoffset += Xret;" << endl; +} + + void t_c_generator::generate_serialize_container(ofstream &out, t_type *ttype, string prefix, @@ -1481,26 +2447,65 @@ void t_c_generator::generate_serialize_container(ofstream &out, scope_down(out); } -void t_c_generator::generate_serialize_map_element(ofstream &out, - t_map *tmap, - string key, - string value, - int error_ret) { - t_field kfield (tmap->get_key_type(), key); - generate_serialize_field (out, &kfield, "", "", error_ret); +void t_c_generator::generate_serialize_container_to_buffer(ofstream &out, + t_type *ttype, + string prefix, + int error_ret) { + scope_up(out); + + if (ttype->is_map()) { + throw "compiler error: serialize of map not supported in C"; + } else if (ttype->is_set()) { + throw "compiler error: serialize of set not supported in C"; + } else if (ttype->is_list()) { + string length = prefix + "_size"; + generate_list_begin_writer_to_buffer (out, + type_to_enum (((t_list *) ttype)->get_elem_type()), length, + error_ret); + out << + indent() << "for (Xi = 0; Xi < " << length << "; Xi++)" << endl; + scope_up(out); + generate_serialize_list_element_to_buffer (out, (t_list *) ttype, prefix, "Xi", error_ret); + scope_down(out); + generate_list_end_writer_to_buffer (out); + out << endl; + } + scope_down(out); +} - t_field vfield (tmap->get_val_type(), value); - generate_serialize_field (out, &vfield, "", "", error_ret); +void t_c_generator::generate_list_begin_writer_to_buffer(ofstream &out, + string element_type, + string length, + int error_ret) { + string element_type8(tmp("Xelement_type8")); + out << + indent() << "/* thrift_protocol_write_list_begin */" << endl; + generate_buffer_bounds_chk (out, "1", error_ret); + scope_up(out); + out << + indent() << "int8_t " << element_type8 << " = (int8_t) " << + element_type << ";" << endl; + generate_write_buffer_memcpy_incr_offset (out, element_type8, "1", + true); + scope_down(out); + string nelement_size(tmp("Xnelement_size")); + generate_buffer_bounds_chk (out, "4", error_ret); + scope_up(out); + out << + indent() << "uint32_t " << nelement_size << " = htonl (" << + length << ");" << endl; + generate_write_buffer_memcpy_incr_offset (out, nelement_size, "4", + true); + scope_down(out); + out << endl; } -void t_c_generator::generate_serialize_set_element(ofstream &out, - t_set *tset, - string element, - int error_ret) { - t_field efield (tset->get_elem_type(), element); - generate_serialize_field (out, &efield, "", "", error_ret); +void t_c_generator::generate_list_end_writer_to_buffer(ofstream &out) { + out << + indent() << "/* thrift_protocol_write_list_end */" << endl; } + void t_c_generator::generate_serialize_list_element(ofstream &out, t_list *tlist, string list, @@ -1513,6 +2518,18 @@ void t_c_generator::generate_serialize_list_element(ofstream &out, out << indent() << "xfer += ret;" << endl; } +void t_c_generator::generate_serialize_list_element_to_buffer(ofstream &out, + t_list *tlist, + string list, + string index, + int error_ret) { + t_type *ttype = tlist->get_elem_type(); + string name = list + "[" + index + "]"; + t_field efield (ttype, name); + generate_serialize_field_to_buffer (out, &efield, "", "", error_ret); + out << endl; +} + /* deserializes a field of any type. */ void t_c_generator::generate_deserialize_field(ofstream &out, t_field *tfield, @@ -1618,6 +2635,99 @@ void t_c_generator::generate_deserialize_field(ofstream &out, } } +/* deserializes a field of any type. */ +void t_c_generator::generate_deserialize_field_from_buffer(ofstream &out, + t_field *tfield, + string prefix, + string suffix, + int error_ret) { + t_type *type = get_true_type (tfield->get_type()); + + if (type->is_void()) { + throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + + prefix + tfield->get_name(); + } + + string name = prefix + tfield->get_name() + suffix; + + if (type->is_struct() || type->is_xception()) { + generate_deserialize_struct_from_buffer (out, (t_struct *) type, name, error_ret); + } else if (type->is_container()) { + generate_deserialize_container_from_buffer (out, type, name, error_ret); + } else if (type->is_base_type()) { + t_base_type::t_base tbase = ((t_base_type *) type)->get_base(); + + switch (tbase) { + case t_base_type::TYPE_VOID: + throw "compiler error: cannot serialize void field in a struct: " + name; + break; + case t_base_type::TYPE_STRING: + if (((t_base_type *) type)->is_binary()) { + throw "CANNOT GENERATE DESERIALIZE CODE FOR binary TYPE: " + name; + } else { + generate_deserialize_string_from_buffer (out, name, error_ret); + } + break; + case t_base_type::TYPE_XML: + generate_deserialize_xml_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_BOOL: + generate_deserialize_bool_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_BYTE: + generate_deserialize_byte_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I16: + generate_deserialize_i16_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I32: + generate_deserialize_i32_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_I64: + generate_deserialize_i64_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U16: + generate_deserialize_u16_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U32: + generate_deserialize_u32_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_U64: + generate_deserialize_u64_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_DOUBLE: + generate_deserialize_double_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_IPV4: + generate_deserialize_i32_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_IPADDR: + generate_deserialize_ipaddr_from_buffer (out, name, error_ret); + break; + case t_base_type::TYPE_UUID: + generate_deserialize_uuid_t_from_buffer (out, name, error_ret); + break; + default: + throw "compiler error: no C reader for base type " + + t_base_type::t_base_name (tbase) + name; + } + } else if (type->is_enum()) { + generate_deserialize_i32_from_buffer (out, name, error_ret); + } else { + printf ("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", + tfield->get_name().c_str(), type_name (type).c_str()); + } + + // if the type is not required and this is a thrift struct (no prefix), + // set the isset variable. if the type is required, then set the + // local variable indicating the value was set, so that we can do // validation later. + if (tfield->get_req() != t_field::T_REQUIRED && prefix != "") { + indent(out) << prefix << "__isset_" << tfield->get_name() << suffix << " = 1;" << endl; + } else if (tfield->get_req() == t_field::T_REQUIRED && prefix != "") { + indent(out) << "isset_" << tfield->get_name() << " = 1;" << endl; + } +} + void t_c_generator::generate_deserialize_struct(ofstream &out, t_struct *tstruct, string prefix, @@ -1629,6 +2739,18 @@ void t_c_generator::generate_deserialize_struct(ofstream &out, indent() << "xfer += ret;" << endl; } +void t_c_generator::generate_deserialize_struct_from_buffer(ofstream &out, + t_struct *tstruct, + string prefix, + int error_ret) { + out << + indent() << prefix << " = (" << tstruct->get_name() << "* ) os_zalloc (sizeof(*" << prefix << "));" << endl << + indent() << "if ((Xret = " << tstruct->get_name() << "_read_binary_from_buffer (" << prefix << + ", Xbuf + Xoffset, Xbuflen - Xoffset, Xerror)) < 0)" << endl << + indent() << " return " << error_ret << ";" << endl << + indent() << "Xoffset += Xret;" << endl; +} + void t_c_generator::generate_deserialize_container (ofstream &out, t_type *ttype, string prefix, int error_ret) { scope_up(out); @@ -1667,18 +2789,52 @@ void t_c_generator::generate_deserialize_container (ofstream &out, t_type *ttype scope_down(out); } -void t_c_generator::generate_deserialize_map_element(ofstream &out, - t_map *tmap, - string prefix, - int error_ret) { - // NOT IMPLEMENTED +void t_c_generator::generate_list_begin_reader_from_buffer(ofstream &out, + string element_size, + int error_ret) { + string element_type8(tmp("Xelement_type8")); + out << + indent() << "/* thrift_protocol_read_list_begin */" << endl; + scope_up(out); + out << + indent() << "int8_t " << element_type8 << ";" << endl; + generate_deserialize_byte_from_buffer (out, element_type8, error_ret); + scope_down(out); + generate_deserialize_i32_from_buffer (out, element_size, error_ret); } -void t_c_generator::generate_deserialize_set_element(ofstream &out, - t_set *tset, - string prefix, - int error_ret) { - // NOT IMPLEMENTED +void t_c_generator::generate_list_end_reader_from_buffer(ofstream &out) { + out << + indent() << "/* thrift_protocol_read_list_end */" << endl; +} + +void t_c_generator::generate_deserialize_container_from_buffer (ofstream &out, t_type *ttype, + string prefix, int error_ret) { + scope_up(out); + + if (ttype->is_map()) { + throw "compiler error: deserialize of map not supported in C"; + } else if (ttype->is_set()) { + throw "compiler error: deserialize of set not supported in C"; + } else if (ttype->is_list()) { + string element_size(prefix + "_size"); + generate_list_begin_reader_from_buffer (out, element_size, error_ret); + out << + indent() << prefix << " = (" << type_name(ttype, false, false) << + ") os_zalloc (sizeof(*" << prefix << ") * " << element_size << ");" << endl; + out << + indent() << "/* iterate through list elements */" << endl << + indent() << "for (Xi = 0; Xi < " << element_size << "; Xi++)" << endl; + + scope_up(out); + generate_deserialize_list_element_from_buffer (out, (t_list *) ttype, prefix, "Xi", + error_ret); + scope_down(out); + + generate_list_end_reader_from_buffer (out); + } + + scope_down(out); } void t_c_generator::generate_deserialize_list_element(ofstream &out, @@ -1691,6 +2847,16 @@ void t_c_generator::generate_deserialize_list_element(ofstream &out, generate_deserialize_field (out, &felem, "", "", error_ret); } +void t_c_generator::generate_deserialize_list_element_from_buffer(ofstream &out, + t_list *tlist, + string prefix, + string index, + int error_ret) { + string elem = prefix + "[" + index + "]"; + t_field felem (tlist->get_elem_type(), elem); + generate_deserialize_field_from_buffer (out, &felem, "", "", error_ret); +} + /*************************************** * UTILITY FUNCTIONS * ***************************************/ diff --git a/compiler/main.cc b/compiler/main.cc index d214a7de..9fdc8b85 100644 --- a/compiler/main.cc +++ b/compiler/main.cc @@ -821,7 +821,7 @@ void validate_const_rec(std::string name, t_type* type, t_const_value* value) { break; case t_base_type::TYPE_UUID: if (value->get_type() != t_const_value::CV_STRING) { - throw "type error: const \"" + name + "\" was declared as uuid_t"; + throw "type error: const \"" + name + "\" was declared as ct_uuid_t"; } break; case t_base_type::TYPE_IPADDR: @@ -1385,7 +1385,7 @@ int main(int argc, char** argv) { g_type_ipv4 = new t_base_type("ipv4", t_base_type::TYPE_IPV4); g_type_ipaddr = new t_base_type("ipaddr", t_base_type::TYPE_IPADDR); g_type_xml = new t_base_type("xml", t_base_type::TYPE_XML); - g_type_uuid_t = new t_base_type("uuid_t", t_base_type::TYPE_UUID); + g_type_uuid_t = new t_base_type("ct_uuid_t", t_base_type::TYPE_UUID); g_type_static_const_string = new t_base_type("static const string", t_base_type::TYPE_STATIC_CONST_STRING); g_type_sandesh_system = new t_base_type("system", t_base_type::TYPE_SANDESH_SYSTEM); g_type_sandesh_request = new t_base_type("request", t_base_type::TYPE_SANDESH_REQUEST); diff --git a/compiler/parse/t_base_type.h b/compiler/parse/t_base_type.h index b8f432eb..9da828cb 100644 --- a/compiler/parse/t_base_type.h +++ b/compiler/parse/t_base_type.h @@ -224,7 +224,7 @@ class t_base_type : public t_type { case TYPE_IPV4 : return "ipv4"; break; case TYPE_IPADDR : return "ipaddr"; break; case TYPE_XML : return "xml"; break; - case TYPE_UUID : return "uuid_t"; break; + case TYPE_UUID : return "ct_uuid_t"; break; case TYPE_STATIC_CONST_STRING : return "static const string"; break; case TYPE_SANDESH_SYSTEM : return "system"; break; diff --git a/compiler/sandeshl.ll b/compiler/sandeshl.ll index bae59cd2..406abda3 100644 --- a/compiler/sandeshl.ll +++ b/compiler/sandeshl.ll @@ -160,7 +160,7 @@ format_str (%([1-9]$)?([+-]?[ -#0])?([1-9]+)?(\.[1-9]+)?(hh|h|l|ll|L|z|j|t)?( "string" { return tok_string; } "binary" { return tok_binary; } "xml" { return tok_xml; } -"uuid_t" { return tok_uuid_t; } +"ct_uuid_t" { return tok_uuid_t; } "slist" { return tok_slist; } "senum" { return tok_senum; } "map" { return tok_map; } diff --git a/library/c/SConscript b/library/c/SConscript index 2f64fd51..272f450f 100644 --- a/library/c/SConscript +++ b/library/c/SConscript @@ -15,7 +15,6 @@ libsandeshc = env.Library(target = 'sandesh-c', source = ['sandesh.c', 'protocol/thrift_protocol.c', 'protocol/thrift_binary_protocol.c', - 'transport/thrift_transport.c', 'transport/thrift_memory_buffer.c', 'transport/thrift_fake_transport.c', ]) diff --git a/library/c/protocol/thrift_binary_protocol.c b/library/c/protocol/thrift_binary_protocol.c index e1fd305d..feffefc7 100644 --- a/library/c/protocol/thrift_binary_protocol.c +++ b/library/c/protocol/thrift_binary_protocol.c @@ -53,7 +53,9 @@ thrift_bitwise_cast_double (u_int64_t v) } #endif -static inline int32_t +/* Write methods */ + +int32_t thrift_binary_protocol_write_byte (ThriftProtocol *protocol, const int8_t value, int *error) { @@ -66,7 +68,7 @@ thrift_binary_protocol_write_byte (ThriftProtocol *protocol, const int8_t value, } } -static inline int32_t +int32_t thrift_binary_protocol_write_i16 (ThriftProtocol *protocol, const int16_t value, int *error) { @@ -81,7 +83,7 @@ thrift_binary_protocol_write_i16 (ThriftProtocol *protocol, const int16_t value, } -static inline int32_t +int32_t thrift_binary_protocol_write_u16 (ThriftProtocol *protocol, const u_int16_t value, int *error) { @@ -95,7 +97,7 @@ thrift_binary_protocol_write_u16 (ThriftProtocol *protocol, const u_int16_t valu } } -static inline int32_t +int32_t thrift_binary_protocol_write_i32 (ThriftProtocol *protocol, const int32_t value, int *error) { @@ -109,7 +111,7 @@ thrift_binary_protocol_write_i32 (ThriftProtocol *protocol, const int32_t value, } } -static inline int32_t +int32_t thrift_binary_protocol_write_u32 (ThriftProtocol *protocol, const u_int32_t value, int *error) { @@ -123,7 +125,7 @@ thrift_binary_protocol_write_u32 (ThriftProtocol *protocol, const u_int32_t valu } } -static inline int32_t +int32_t thrift_binary_protocol_write_i64 (ThriftProtocol *protocol, const int64_t value, int *error) { @@ -138,7 +140,7 @@ thrift_binary_protocol_write_i64 (ThriftProtocol *protocol, const int64_t value, } } -static inline int32_t +int32_t thrift_binary_protocol_write_u64 (ThriftProtocol *protocol, const uint64_t value, int *error) { @@ -153,7 +155,7 @@ thrift_binary_protocol_write_u64 (ThriftProtocol *protocol, const uint64_t value } } -static inline int32_t +int32_t thrift_binary_protocol_write_bool (ThriftProtocol *protocol, const u_int8_t value, int *error) { @@ -161,43 +163,6 @@ thrift_binary_protocol_write_bool (ThriftProtocol *protocol, return thrift_binary_protocol_write_byte (protocol, tmp, error); } -int32_t -thrift_binary_protocol_write_message_begin (ThriftProtocol *protocol, - const char *name, const ThriftMessageType message_type, - const int32_t seqid, int *error) -{ - int32_t version = (THRIFT_BINARY_PROTOCOL_VERSION_1) - | ((int32_t) message_type); - int32_t ret; - int32_t xfer = 0; - - if ((ret = thrift_binary_protocol_write_i32 (protocol, version, error)) < 0) - { - return -1; - } - xfer += ret; - if ((ret = thrift_binary_protocol_write_string (protocol, name, error)) < 0) - { - return -1; - } - xfer += ret; - if ((ret = thrift_binary_protocol_write_i32 (protocol, seqid, error)) < 0) - { - return -1; - } - xfer += ret; - return xfer; -} - -int32_t -thrift_binary_protocol_write_message_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_write_sandesh_begin (ThriftProtocol *protocol, const char *name, int *error) @@ -213,35 +178,6 @@ thrift_binary_protocol_write_sandesh_begin (ThriftProtocol *protocol, return xfer; } -int32_t -thrift_binary_protocol_write_sandesh_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - -int32_t -thrift_binary_protocol_write_struct_begin (ThriftProtocol *protocol , - const char *name , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (name); - THRIFT_UNUSED_VAR (error); - return 0; -} - -int32_t -thrift_binary_protocol_write_struct_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_write_field_begin (ThriftProtocol *protocol, const char *name, @@ -266,13 +202,6 @@ thrift_binary_protocol_write_field_begin (ThriftProtocol *protocol, return xfer; } -int32_t -thrift_binary_protocol_write_field_end (ThriftProtocol *protocol , - int *error ) -{ - return 0; -} - int32_t thrift_binary_protocol_write_field_stop (ThriftProtocol *protocol, int *error) @@ -281,45 +210,6 @@ thrift_binary_protocol_write_field_stop (ThriftProtocol *protocol, return thrift_binary_protocol_write_byte (protocol, (int8_t) T_STOP, error); } -int32_t -thrift_binary_protocol_write_map_begin (ThriftProtocol *protocol, - const ThriftType key_type, - const ThriftType value_type, - const u_int32_t size, - int *error) -{ - int32_t ret; - int32_t xfer = 0; - - if ((ret = thrift_binary_protocol_write_byte (protocol, (int8_t) key_type, - error)) < 0) - { - return -1; - } - xfer += ret; - if ((ret = thrift_binary_protocol_write_byte (protocol, (int8_t) value_type, - error)) < 0) - { - return -1; - } - xfer += ret; - if ((ret = thrift_binary_protocol_write_i32 (protocol, (int32_t) size, error)) < 0) - { - return -1; - } - xfer += ret; - return xfer; -} - -int32_t -thrift_binary_protocol_write_map_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_write_list_begin (ThriftProtocol *protocol, const ThriftType element_type, @@ -345,34 +235,6 @@ thrift_binary_protocol_write_list_begin (ThriftProtocol *protocol, return xfer; } -int32_t -thrift_binary_protocol_write_list_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - -int32_t -thrift_binary_protocol_write_set_begin (ThriftProtocol *protocol, - const ThriftType element_type, - const u_int32_t size, - int *error) -{ - return thrift_binary_protocol_write_list_begin (protocol, element_type, - size, error); -} - -int32_t -thrift_binary_protocol_write_set_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_write_ipv4 (ThriftProtocol *protocol, const u_int32_t value, int *error) @@ -419,7 +281,7 @@ thrift_binary_protocol_write_ipaddr (ThriftProtocol *protocol, } int32_t -thrift_binary_protocol_write_uuid_t (ThriftProtocol *protocol, const uuid_t value, +thrift_binary_protocol_write_uuid_t (ThriftProtocol *protocol, const ct_uuid_t value, int *error) { if (thrift_transport_write (protocol->transport, @@ -487,60 +349,7 @@ thrift_binary_protocol_write_binary (ThriftProtocol *protocol, return xfer; } -int32_t -thrift_binary_protocol_read_message_begin (ThriftProtocol *protocol, - char **name, - ThriftMessageType *message_type, - int32_t *seqid, int *error) -{ - int32_t ret; - int32_t xfer = 0; - int32_t sz; - - if ((ret = thrift_binary_protocol_read_i32 (protocol, &sz, error)) < 0) - { - return -1; - } - xfer += ret; - - if (sz < 0) - { - /* check for version */ - u_int32_t version = sz & THRIFT_BINARY_PROTOCOL_VERSION_MASK; - if (version != THRIFT_BINARY_PROTOCOL_VERSION_1) - { - *error = THRIFT_PROTOCOL_ERROR_BAD_VERSION; - os_log(OS_LOG_ERR, "Expected version %d, got %d", - THRIFT_BINARY_PROTOCOL_VERSION_1, version); - return -1; - } - - *message_type = (ThriftMessageType) (sz & 0x000000ff); - - if ((ret = thrift_binary_protocol_read_string (protocol, name, error)) < 0) - { - return -1; - } - xfer += ret; - - if ((ret = thrift_binary_protocol_read_i32 (protocol, seqid, error)) < 0) - { - return -1; - } - xfer += ret; - } - return xfer; -} - -int32_t -thrift_binary_protocol_read_message_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - +/* Read methods */ int32_t thrift_binary_protocol_read_sandesh_begin (ThriftProtocol *protocol, char **name, @@ -557,15 +366,6 @@ thrift_binary_protocol_read_sandesh_begin (ThriftProtocol *protocol, return xfer; } -int32_t -thrift_binary_protocol_read_sandesh_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_read_struct_begin (ThriftProtocol *protocol , char **name, @@ -577,15 +377,6 @@ thrift_binary_protocol_read_struct_begin (ThriftProtocol *protocol , return 0; } -int32_t -thrift_binary_protocol_read_struct_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_read_field_begin (ThriftProtocol *protocol, char **name, @@ -625,58 +416,6 @@ thrift_binary_protocol_read_field_end (ThriftProtocol *protocol , return 0; } -int32_t -thrift_binary_protocol_read_map_begin (ThriftProtocol *protocol, - ThriftType *key_type, - ThriftType *value_type, - u_int32_t *size, - int *error) -{ - int32_t ret; - int32_t xfer = 0; - int8_t k, v; - int32_t sizei; - - if ((ret = thrift_binary_protocol_read_byte (protocol, &k, error)) < 0) - { - return -1; - } - xfer += ret; - *key_type = (ThriftType) k; - - if ((ret = thrift_binary_protocol_read_byte (protocol, &v, error)) < 0) - { - return -1; - } - xfer += ret; - *value_type = (ThriftType) v; - - if ((ret = thrift_binary_protocol_read_i32 (protocol, &sizei, error)) <0) - { - return -1; - } - xfer += ret; - - if (sizei < 0) - { - *error = THRIFT_PROTOCOL_ERROR_NEGATIVE_SIZE; - os_log(OS_LOG_ERR, "Got negative size of %d", sizei); - return -1; - } - - *size = (u_int32_t) sizei; - return xfer; -} - -int32_t -thrift_binary_protocol_read_map_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_read_list_begin (ThriftProtocol *protocol, ThriftType *element_type, @@ -720,25 +459,6 @@ thrift_binary_protocol_read_list_end (ThriftProtocol *protocol , return 0; } -int32_t -thrift_binary_protocol_read_set_begin (ThriftProtocol *protocol, - ThriftType *element_type, - u_int32_t *size, int *error) -{ - - - return thrift_binary_protocol_read_list_begin (protocol, element_type, size, error); -} - -int32_t -thrift_binary_protocol_read_set_end (ThriftProtocol *protocol , - int *error ) -{ - THRIFT_UNUSED_VAR (protocol); - THRIFT_UNUSED_VAR (error); - return 0; -} - int32_t thrift_binary_protocol_read_bool (ThriftProtocol *protocol, u_int8_t *value, int *error) @@ -927,7 +647,7 @@ thrift_binary_protocol_read_ipaddr (ThriftProtocol *protocol, ipaddr_t *value, } int32_t -thrift_binary_protocol_read_uuid_t (ThriftProtocol *protocol, uuid_t *value, +thrift_binary_protocol_read_uuid_t (ThriftProtocol *protocol, ct_uuid_t *value, int *error) { int32_t ret; @@ -1048,8 +768,6 @@ thrift_binary_protocol_read_binary (ThriftProtocol *protocol, void thrift_binary_protocol_init (ThriftBinaryProtocol *protocol) { - protocol->write_message_begin = thrift_binary_protocol_write_message_begin; - protocol->write_message_end = thrift_binary_protocol_write_message_end; protocol->write_sandesh_begin = thrift_binary_protocol_write_sandesh_begin; protocol->write_sandesh_end = thrift_binary_protocol_write_sandesh_end; protocol->write_struct_begin = thrift_binary_protocol_write_struct_begin; @@ -1057,12 +775,8 @@ thrift_binary_protocol_init (ThriftBinaryProtocol *protocol) protocol->write_field_begin = thrift_binary_protocol_write_field_begin; protocol->write_field_end = thrift_binary_protocol_write_field_end; protocol->write_field_stop = thrift_binary_protocol_write_field_stop; - protocol->write_map_begin = thrift_binary_protocol_write_map_begin; - protocol->write_map_end = thrift_binary_protocol_write_map_end; protocol->write_list_begin = thrift_binary_protocol_write_list_begin; protocol->write_list_end = thrift_binary_protocol_write_list_end; - protocol->write_set_begin = thrift_binary_protocol_write_set_begin; - protocol->write_set_end = thrift_binary_protocol_write_set_end; protocol->write_bool = thrift_binary_protocol_write_bool; protocol->write_byte = thrift_binary_protocol_write_byte; protocol->write_i16 = thrift_binary_protocol_write_i16; @@ -1078,20 +792,14 @@ thrift_binary_protocol_init (ThriftBinaryProtocol *protocol) protocol->write_binary = thrift_binary_protocol_write_binary; protocol->write_xml = thrift_binary_protocol_write_string; protocol->write_uuid_t = thrift_binary_protocol_write_uuid_t; - protocol->read_message_begin = thrift_binary_protocol_read_message_begin; - protocol->read_message_end = thrift_binary_protocol_read_message_end; protocol->read_sandesh_begin = thrift_binary_protocol_read_sandesh_begin; protocol->read_sandesh_end = thrift_binary_protocol_read_sandesh_end; protocol->read_struct_begin = thrift_binary_protocol_read_struct_begin; protocol->read_struct_end = thrift_binary_protocol_read_struct_end; protocol->read_field_begin = thrift_binary_protocol_read_field_begin; protocol->read_field_end = thrift_binary_protocol_read_field_end; - protocol->read_map_begin = thrift_binary_protocol_read_map_begin; - protocol->read_map_end = thrift_binary_protocol_read_map_end; protocol->read_list_begin = thrift_binary_protocol_read_list_begin; protocol->read_list_end = thrift_binary_protocol_read_list_end; - protocol->read_set_begin = thrift_binary_protocol_read_set_begin; - protocol->read_set_end = thrift_binary_protocol_read_set_end; protocol->read_bool = thrift_binary_protocol_read_bool; protocol->read_byte = thrift_binary_protocol_read_byte; protocol->read_i16 = thrift_binary_protocol_read_i16; diff --git a/library/c/protocol/thrift_binary_protocol.h b/library/c/protocol/thrift_binary_protocol.h index e548cf3c..22ce353a 100644 --- a/library/c/protocol/thrift_binary_protocol.h +++ b/library/c/protocol/thrift_binary_protocol.h @@ -31,6 +31,74 @@ extern "C" { typedef ThriftProtocol ThriftBinaryProtocol; void thrift_binary_protocol_init (ThriftBinaryProtocol *protocol); +int32_t thrift_binary_protocol_skip_from_buffer (uint8_t *buf, const uint32_t buflen, + ThriftType type, int *error); + +/* Write to ThriftTransport */ +static inline int32_t +thrift_binary_protocol_write_sandesh_end (ThriftProtocol *protocol, int *error) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} + +static inline int32_t +thrift_binary_protocol_write_struct_begin (ThriftProtocol *protocol, + const char *name, + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (name); + THRIFT_UNUSED_VAR (error); + return 0; +} + +static inline int32_t +thrift_binary_protocol_write_struct_end (ThriftProtocol *protocol , + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} + +static inline int32_t +thrift_binary_protocol_write_field_end (ThriftProtocol *protocol , + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} + +static inline int32_t +thrift_binary_protocol_write_list_end (ThriftProtocol *protocol , + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} + +/* Read from ThriftTransport */ +static inline int32_t +thrift_binary_protocol_read_sandesh_end (ThriftProtocol *protocol , + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} + +static inline int32_t +thrift_binary_protocol_read_struct_end (ThriftProtocol *protocol , + int *error ) +{ + THRIFT_UNUSED_VAR (protocol); + THRIFT_UNUSED_VAR (error); + return 0; +} #ifdef __cplusplus } diff --git a/library/c/protocol/thrift_protocol.c b/library/c/protocol/thrift_protocol.c index e952d000..91adf85c 100644 --- a/library/c/protocol/thrift_protocol.c +++ b/library/c/protocol/thrift_protocol.c @@ -36,440 +36,6 @@ thrift_protocol_init (ThriftProtocol *protocol, } } -ThriftProtocolType -thrift_protocol_get_type (ThriftProtocol *protocol) -{ - return protocol->ptype; -} - -int32_t -thrift_protocol_write_message_begin (ThriftProtocol *protocol, - const char *name, - const ThriftMessageType message_type, - const int32_t seqid, int *error) -{ - return protocol->write_message_begin (protocol, name, message_type, seqid, - error); -} - -int32_t -thrift_protocol_write_message_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_message_end (protocol, error); -} - -int32_t -thrift_protocol_write_sandesh_begin (ThriftProtocol *protocol, - const char *name, - int *error) -{ - return protocol->write_sandesh_begin (protocol, name, error); -} - -int32_t -thrift_protocol_write_sandesh_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_sandesh_end (protocol, error); -} - -int32_t -thrift_protocol_write_struct_begin (ThriftProtocol *protocol, const char *name, - int *error) -{ - return protocol->write_struct_begin (protocol, name, error); -} - -int32_t -thrift_protocol_write_struct_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_struct_end (protocol, error); -} - -int32_t -thrift_protocol_write_field_begin (ThriftProtocol *protocol, - const char *name, - const ThriftType field_type, - const int16_t field_id, - int *error) -{ - return protocol->write_field_begin (protocol, name, field_type, - field_id, error); -} - -int32_t -thrift_protocol_write_field_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_field_end (protocol, error); -} - -int32_t -thrift_protocol_write_field_stop (ThriftProtocol *protocol, int *error) -{ - return protocol->write_field_stop (protocol, error); -} - -int32_t -thrift_protocol_write_map_begin (ThriftProtocol *protocol, - const ThriftType key_type, - const ThriftType value_type, - const u_int32_t size, int *error) -{ - return protocol->write_map_begin (protocol, key_type, value_type, - size, error); -} - -int32_t -thrift_protocol_write_map_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_map_end (protocol, error); -} - -int32_t -thrift_protocol_write_list_begin (ThriftProtocol *protocol, - const ThriftType element_type, - const u_int32_t size, int *error) -{ - return protocol->write_list_begin (protocol, element_type, size, error); -} - -int32_t -thrift_protocol_write_list_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_list_end (protocol, error); -} - -int32_t -thrift_protocol_write_set_begin (ThriftProtocol *protocol, - const ThriftType element_type, - const u_int32_t size, int *error) -{ - return protocol->write_set_begin (protocol, element_type, size, error); -} - -int32_t -thrift_protocol_write_set_end (ThriftProtocol *protocol, int *error) -{ - return protocol->write_set_end (protocol, error); -} - -int32_t -thrift_protocol_write_bool (ThriftProtocol *protocol, - const u_int8_t value, int *error) -{ - return protocol->write_bool (protocol, value, error); -} - -int32_t -thrift_protocol_write_byte (ThriftProtocol *protocol, const int8_t value, - int *error) -{ - return protocol->write_byte (protocol, value, error); -} - -int32_t -thrift_protocol_write_i16 (ThriftProtocol *protocol, const int16_t value, - int *error) -{ - return protocol->write_i16 (protocol, value, error); -} - -int32_t -thrift_protocol_write_i32 (ThriftProtocol *protocol, const int32_t value, - int *error) -{ - return protocol->write_i32 (protocol, value, error); -} - -int32_t -thrift_protocol_write_i64 (ThriftProtocol *protocol, const int64_t value, - int *error) -{ - return protocol->write_i64 (protocol, value, error); -} - -int32_t -thrift_protocol_write_u16 (ThriftProtocol *protocol, const u_int16_t value, - int *error) -{ - return protocol->write_u16 (protocol, value, error); -} - -int32_t -thrift_protocol_write_u32 (ThriftProtocol *protocol, const u_int32_t value, - int *error) -{ - return protocol->write_u32 (protocol, value, error); -} - -int32_t -thrift_protocol_write_u64 (ThriftProtocol *protocol, const u_int64_t value, - int *error) -{ - return protocol->write_u64 (protocol, value, error); -} - -int32_t -thrift_protocol_write_ipv4 (ThriftProtocol *protocol, const u_int32_t value, - int *error) -{ - return protocol->write_ipv4 (protocol, value, error); -} - -int32_t -thrift_protocol_write_ipaddr (ThriftProtocol *protocol, const ipaddr_t *value, - int *error) -{ - return protocol->write_ipaddr (protocol, value, error); -} - -int32_t -thrift_protocol_write_double (ThriftProtocol *protocol, - const double value, int *error) -{ - return protocol->write_double (protocol, value, error); -} - -int32_t -thrift_protocol_write_string (ThriftProtocol *protocol, - const char *str, int *error) -{ - return protocol->write_string (protocol, str, error); -} - -int32_t -thrift_protocol_write_binary (ThriftProtocol *protocol, const void *buf, - const u_int32_t len, int *error) -{ - return protocol->write_binary (protocol, buf, len, error); -} - -int32_t -thrift_protocol_write_xml (ThriftProtocol *protocol, - const char *str, int *error) -{ - return protocol->write_xml (protocol, str, error); -} - -int32_t -thrift_protocol_write_uuid_t (ThriftProtocol *protocol, - const uuid_t value, int *error) -{ - return protocol->write_uuid_t (protocol, value, error); -} - -int32_t -thrift_protocol_read_message_begin (ThriftProtocol *protocol, - char **name, - ThriftMessageType *message_type, - int32_t *seqid, int *error) -{ - return protocol->read_message_begin (protocol, name, message_type, - seqid, error); -} - -int32_t -thrift_protocol_read_message_end (ThriftProtocol *protocol, - int *error) -{ - return protocol->read_message_end (protocol, error); -} - -int32_t -thrift_protocol_read_sandesh_begin (ThriftProtocol *protocol, - char **name, - int *error) -{ - return protocol->read_sandesh_begin (protocol, name, error); -} - -int32_t -thrift_protocol_read_sandesh_end (ThriftProtocol *protocol, - int *error) -{ - return protocol->read_sandesh_end (protocol, error); -} - -int32_t -thrift_protocol_read_struct_begin (ThriftProtocol *protocol, - char **name, - int *error) -{ - return protocol->read_struct_begin (protocol, name, error); -} - -int32_t -thrift_protocol_read_struct_end (ThriftProtocol *protocol, int *error) -{ - return protocol->read_struct_end (protocol, error); -} - -int32_t -thrift_protocol_read_field_begin (ThriftProtocol *protocol, - char **name, - ThriftType *field_type, - int16_t *field_id, - int *error) -{ - return protocol->read_field_begin (protocol, name, field_type, field_id, - error); -} - -int32_t -thrift_protocol_read_field_end (ThriftProtocol *protocol, - int *error) -{ - return protocol->read_field_end (protocol, error); -} - -int32_t -thrift_protocol_read_map_begin (ThriftProtocol *protocol, - ThriftType *key_type, - ThriftType *value_type, u_int32_t *size, - int *error) -{ - return protocol->read_map_begin (protocol, key_type, value_type, size, - error); -} - -int32_t -thrift_protocol_read_map_end (ThriftProtocol *protocol, int *error) -{ - return protocol->read_map_end (protocol, error); -} - -int32_t -thrift_protocol_read_list_begin (ThriftProtocol *protocol, - ThriftType *element_type, - u_int32_t *size, int *error) -{ - return protocol->read_list_begin (protocol, element_type, size, error); -} - -int32_t -thrift_protocol_read_list_end (ThriftProtocol *protocol, int *error) -{ - return protocol->read_list_end (protocol, error); -} - -int32_t -thrift_protocol_read_set_begin (ThriftProtocol *protocol, - ThriftType *element_type, - u_int32_t *size, int *error) -{ - return protocol->read_set_begin (protocol, element_type, size, error); -} - -int32_t -thrift_protocol_read_set_end (ThriftProtocol *protocol, int *error) -{ - return protocol->read_set_end (protocol, error); -} - -int32_t -thrift_protocol_read_bool (ThriftProtocol *protocol, u_int8_t *value, - int *error) -{ - return protocol->read_bool (protocol, value, error); -} - -int32_t -thrift_protocol_read_byte (ThriftProtocol *protocol, int8_t *value, - int *error) -{ - return protocol->read_byte (protocol, value, error); -} - -int32_t -thrift_protocol_read_i16 (ThriftProtocol *protocol, int16_t *value, - int *error) -{ - return protocol->read_i16 (protocol, value, error); -} - -int32_t -thrift_protocol_read_i32 (ThriftProtocol *protocol, int32_t *value, - int *error) -{ - return protocol->read_i32 (protocol, value, error); -} - -int32_t -thrift_protocol_read_i64 (ThriftProtocol *protocol, int64_t *value, - int *error) -{ - return protocol->read_i64 (protocol, value, error); -} - -int32_t -thrift_protocol_read_u16 (ThriftProtocol *protocol, u_int16_t *value, - int *error) -{ - return protocol->read_u16 (protocol, value, error); -} - -int32_t -thrift_protocol_read_u32 (ThriftProtocol *protocol, u_int32_t *value, - int *error) -{ - return protocol->read_u32 (protocol, value, error); -} - -int32_t -thrift_protocol_read_u64 (ThriftProtocol *protocol, u_int64_t *value, - int *error) -{ - return protocol->read_u64 (protocol, value, error); -} - -int32_t -thrift_protocol_read_ipv4 (ThriftProtocol *protocol, u_int32_t *value, - int *error) -{ - return protocol->read_ipv4 (protocol, value, error); -} - -int32_t -thrift_protocol_read_ipaddr (ThriftProtocol *protocol, ipaddr_t *value, - int *error) -{ - return protocol->read_ipaddr (protocol, value, error); -} - -int32_t -thrift_protocol_read_double (ThriftProtocol *protocol, - double *value, int *error) -{ - return protocol->read_double (protocol, value, error); -} - -int32_t -thrift_protocol_read_string (ThriftProtocol *protocol, - char **str, int *error) -{ - return protocol->read_string (protocol, str, error); -} - -int32_t -thrift_protocol_read_binary (ThriftProtocol *protocol, void **buf, - u_int32_t *len, int *error) -{ - return protocol->read_binary (protocol, buf, len, error); -} - -int32_t -thrift_protocol_read_xml (ThriftProtocol *protocol, - char **str, int *error) -{ - return protocol->read_xml (protocol, str, error); -} - -int32_t -thrift_protocol_read_uuid_t (ThriftProtocol *protocol, - uuid_t *value, int *error) -{ - return protocol->read_uuid_t (protocol, value, error); -} - int32_t thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, int *error) { @@ -537,7 +103,7 @@ thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, int *error) } case T_UUID: { - uuid_t uuid; + ct_uuid_t uuid; return thrift_protocol_read_uuid_t (protocol, &uuid, error); } case T_STRUCT: @@ -562,32 +128,203 @@ thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, int *error) result += thrift_protocol_read_struct_end (protocol, error); return result; } - case T_MAP: + case T_LIST: { u_int32_t result = 0; ThriftType elem_type; u_int32_t i, size; - result += thrift_protocol_read_set_begin (protocol, &elem_type, &size, - error); + result += thrift_protocol_read_list_begin (protocol, &elem_type, &size, + error); for (i = 0; i < size; i++) { result += thrift_protocol_skip (protocol, elem_type, error); } - result += thrift_protocol_read_set_end (protocol, error); + result += thrift_protocol_read_list_end (protocol, error); + return result; + } + default: + return 0; + } +} + +int32_t +thrift_binary_protocol_skip_from_buffer (uint8_t *buf, const uint32_t buflen, + ThriftType type, int *error) +{ + switch (type) + { + case T_BOOL: + case T_BYTE: + { + if (1 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 1, buflen); + return -1; + } + return 1; + } + case T_I16: + case T_U16: + { + if (2 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 2, buflen); + return -1; + } + return 2; + } + case T_I32: + case T_U32: + case T_IPV4: + { + if (4 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 4, buflen); + return -1; + } + return 4; + } + case T_I64: + case T_U64: + case T_DOUBLE: + { + if (8 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 8, buflen); + return -1; + } + return 8; + } + case T_STRING: + case T_XML: + { + uint32_t result = 0; + union { + void * b[4]; + int32_t all; + } bytes; + int32_t read_len; + + /* read the length into nread_len */ + if (4 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 4, buflen); + return -1; + } + memcpy (bytes.b, buf, 4); + result += 4; + read_len = ntohl (bytes.all); + if (read_len > 0) + { + if (result + read_len > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + result + read_len, buflen); + return -1; + } + result += read_len; + } + return result; + } + case T_UUID: + { + if (16 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 16, buflen); + return -1; + } + return 16; + } + case T_STRUCT: + { + uint32_t result = 0; + void * b[1]; + ThriftType ftype; + + while (1) + { + if (1 > buflen - result) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 1, buflen - result); + return -1; + } + memcpy (b, buf + result, 1); + result += 1; + ftype = *(int8_t *) b; + if (ftype == T_STOP) + { + break; + } + if (2 > buflen - result) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 2, buflen - result); + return -1; + } + result += 2; + result += thrift_binary_protocol_skip_from_buffer (buf + result, + buflen - result, ftype, error); + } return result; } case T_LIST: { - u_int32_t result = 0; + uint32_t result = 0; ThriftType elem_type; - u_int32_t i, size; - result += thrift_protocol_read_list_begin (protocol, &elem_type, &size, - error); + int32_t i, size; + void * b[1]; + union { + void * b[4]; + int32_t all; + } bytes; + + if (1 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + 1, buflen); + return -1; + } + memcpy (b, buf, 1); + result += 1; + elem_type = * (ThriftType *) b; + if (result + 4 > buflen) + { + *error = THRIFT_TRANSPORT_ERROR_RECEIVE; + os_log (OS_LOG_ERR, "Unable to read %d bytes from buffer of length %d", + result + 4, buflen); + return -1; + } + memcpy (bytes.b, buf + result, 4); + result += 4; + size = ntohl (bytes.all); + if (size < 0) + { + *error = THRIFT_PROTOCOL_ERROR_NEGATIVE_SIZE; + os_log (OS_LOG_ERR, "Got negative size of %d", size); + return -1; + } for (i = 0; i < size; i++) { - result += thrift_protocol_skip (protocol, elem_type, error); + result += thrift_binary_protocol_skip_from_buffer (buf + result, + buflen - result, elem_type, error); } - result += thrift_protocol_read_list_end (protocol, error); return result; } default: diff --git a/library/c/protocol/thrift_protocol.h b/library/c/protocol/thrift_protocol.h index 22cfe38e..63954747 100644 --- a/library/c/protocol/thrift_protocol.h +++ b/library/c/protocol/thrift_protocol.h @@ -88,10 +88,6 @@ struct _ThriftProtocol int32_t (*write_sandesh_begin) (struct _ThriftProtocol *protocol, const char *name, int *error); int32_t (*write_sandesh_end) (struct _ThriftProtocol *protocol, int *error); - int32_t (*write_message_begin) (struct _ThriftProtocol *protocol, const char *name, - const ThriftMessageType message_type, - const int32_t seqid, int *error); - int32_t (*write_message_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*write_struct_begin) (struct _ThriftProtocol *protocol, const char *name, int *error); int32_t (*write_struct_end) (struct _ThriftProtocol *protocol, int *error); @@ -100,19 +96,10 @@ struct _ThriftProtocol const int16_t field_id, int *error); int32_t (*write_field_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*write_field_stop) (struct _ThriftProtocol *protocol, int *error); - int32_t (*write_map_begin) (struct _ThriftProtocol *protocol, - const ThriftType key_type, - const ThriftType value_type, - const u_int32_t size, int *error); - int32_t (*write_map_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*write_list_begin) (struct _ThriftProtocol *protocol, const ThriftType element_type, const u_int32_t size, int *error); int32_t (*write_list_end) (struct _ThriftProtocol *protocol, int *error); - int32_t (*write_set_begin) (struct _ThriftProtocol *protocol, - const ThriftType element_type, - const u_int32_t size, int *error); - int32_t (*write_set_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*write_bool) (struct _ThriftProtocol *protocol, const u_int8_t value, int *error); int32_t (*write_byte) (struct _ThriftProtocol *protocol, const int8_t value, @@ -141,16 +128,12 @@ struct _ThriftProtocol const u_int32_t len, int *error); int32_t (*write_xml) (struct _ThriftProtocol *protocol, const char *str, int *error); - int32_t (*write_uuid_t) (struct _ThriftProtocol *protocol, const uuid_t value, + int32_t (*write_uuid_t) (struct _ThriftProtocol *protocol, const ct_uuid_t value, int *error); int32_t (*read_sandesh_begin) (struct _ThriftProtocol *protocol, char **name, int *error); int32_t (*read_sandesh_end) (struct _ThriftProtocol *protocol, int *error); - int32_t (*read_message_begin) (struct _ThriftProtocol *protocol, char **name, - ThriftMessageType *message_type, - int32_t *seqid, int *error); - int32_t (*read_message_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*read_struct_begin) (struct _ThriftProtocol *protocol, char **name, int *error); int32_t (*read_struct_end) (struct _ThriftProtocol *protocol, int *error); @@ -158,16 +141,9 @@ struct _ThriftProtocol ThriftType *field_type, int16_t *field_id, int *error); int32_t (*read_field_end) (struct _ThriftProtocol *protocol, int *error); - int32_t (*read_map_begin) (struct _ThriftProtocol *protocol, ThriftType *key_type, - ThriftType *value_type, u_int32_t *size, - int *error); - int32_t (*read_map_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*read_list_begin) (struct _ThriftProtocol *protocol, ThriftType *element_type, u_int32_t *size, int *error); int32_t (*read_list_end) (struct _ThriftProtocol *protocol, int *error); - int32_t (*read_set_begin) (struct _ThriftProtocol *protocol, ThriftType *element_type, - u_int32_t *size, int *error); - int32_t (*read_set_end) (struct _ThriftProtocol *protocol, int *error); int32_t (*read_bool) (struct _ThriftProtocol *protocol, u_int8_t *value, int *error); int32_t (*read_byte) (struct _ThriftProtocol *protocol, int8_t *value, int *error); @@ -185,211 +161,354 @@ struct _ThriftProtocol int32_t (*read_binary) (struct _ThriftProtocol *protocol, void **buf, u_int32_t *len, int *error); int32_t (*read_xml) (struct _ThriftProtocol *protocol, char **str, int *error); - int32_t (*read_uuid_t) (struct _ThriftProtocol *protocol, uuid_t *value, int *error); + int32_t (*read_uuid_t) (struct _ThriftProtocol *protocol, ct_uuid_t *value, int *error); }; typedef struct _ThriftProtocol ThriftProtocol; /* used by THRIFT_TYPE_PROTOCOL */ -ThriftProtocolType thrift_protocol_get_type (ThriftProtocol *protocol); +static inline ThriftProtocolType thrift_protocol_get_type ( + ThriftProtocol *protocol) +{ + return protocol->ptype; +} /* virtual public methods */ -int32_t thrift_protocol_write_message_begin (ThriftProtocol *protocol, - const char *name, const ThriftMessageType message_type, - const int32_t seqid, int *error); - -int32_t thrift_protocol_write_message_end (ThriftProtocol *protocol, - int *error); - -int32_t thrift_protocol_write_sandesh_begin (ThriftProtocol *protocol, - const char *name, int *error); - -int32_t thrift_protocol_write_sandesh_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_write_sandesh_begin ( + ThriftProtocol *protocol, + const char *name, int *error) +{ + return protocol->write_sandesh_begin (protocol, name, error); +} -int32_t thrift_protocol_write_message_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_write_sandesh_end ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->write_sandesh_end (protocol, error); +} -int32_t thrift_protocol_write_struct_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_write_struct_begin ( + ThriftProtocol *protocol, const char *name, - int *error); + int *error) +{ + return protocol->write_struct_begin (protocol, name, error); +} -int32_t thrift_protocol_write_struct_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_write_struct_end ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->write_struct_end (protocol, error); +} -int32_t thrift_protocol_write_field_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_write_field_begin ( + ThriftProtocol *protocol, const char *name, const ThriftType field_type, const int16_t field_id, - int *error); - -int32_t thrift_protocol_write_field_end (ThriftProtocol *protocol, - int *error); - -int32_t thrift_protocol_write_field_stop (ThriftProtocol *protocol, - int *error); + int *error) +{ + return protocol->write_field_begin (protocol, name, field_type, field_id, + error); +} -int32_t thrift_protocol_write_map_begin (ThriftProtocol *protocol, - const ThriftType key_type, - const ThriftType value_type, - const u_int32_t size, int *error); +static inline int32_t thrift_protocol_write_field_end ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->write_field_end (protocol, error); +} -int32_t thrift_protocol_write_map_end (ThriftProtocol *protocol, int *error); +static inline int32_t thrift_protocol_write_field_stop ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->write_field_stop (protocol, error); +} -int32_t thrift_protocol_write_list_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_write_list_begin ( + ThriftProtocol *protocol, const ThriftType element_type, - const u_int32_t size, int *error); - -int32_t thrift_protocol_write_list_end (ThriftProtocol *protocol, - int *error); - -int32_t thrift_protocol_write_set_begin (ThriftProtocol *protocol, - const ThriftType element_type, - const u_int32_t size, int *error); - -int32_t thrift_protocol_write_set_end (ThriftProtocol *protocol, - int *error); - -int32_t thrift_protocol_write_bool (ThriftProtocol *protocol, - const u_int8_t value, int *error); + const u_int32_t size, int *error) +{ + return protocol->write_list_begin (protocol, element_type, size, error); +} -int32_t thrift_protocol_write_byte (ThriftProtocol *protocol, const int8_t value, - int *error); +static inline int32_t thrift_protocol_write_list_end (ThriftProtocol *protocol, + int *error) +{ + return protocol->write_list_end (protocol, error); +} -int32_t thrift_protocol_write_i16 (ThriftProtocol *protocol, const int16_t value, - int *error); +static inline int32_t thrift_protocol_write_bool (ThriftProtocol *protocol, + const u_int8_t value, + int *error) +{ + return protocol->write_bool (protocol, value, error); +} -int32_t thrift_protocol_write_i32 (ThriftProtocol *protocol, const int32_t value, - int *error); +static inline int32_t thrift_protocol_write_byte (ThriftProtocol *protocol, + const int8_t value, + int *error) +{ + return protocol->write_byte (protocol, value, error); +} -int32_t thrift_protocol_write_i64 (ThriftProtocol *protocol, const int64_t value, - int *error); +static inline int32_t thrift_protocol_write_i16 (ThriftProtocol *protocol, + const int16_t value, + int *error) +{ + return protocol->write_i16 (protocol, value, error); +} -int32_t thrift_protocol_write_u16 (ThriftProtocol *protocol, const u_int16_t value, - int *error); +static inline int32_t thrift_protocol_write_i32 (ThriftProtocol *protocol, + const int32_t value, + int *error) +{ + return protocol->write_i32 (protocol, value, error); +} -int32_t thrift_protocol_write_u32 (ThriftProtocol *protocol, const u_int32_t value, - int *error); +static inline int32_t thrift_protocol_write_i64 (ThriftProtocol *protocol, + const int64_t value, + int *error) +{ + return protocol->write_i64 (protocol, value, error); +} -int32_t thrift_protocol_write_u64 (ThriftProtocol *protocol, const u_int64_t value, - int *error); +static inline int32_t thrift_protocol_write_u16 (ThriftProtocol *protocol, + const u_int16_t value, + int *error) +{ + return protocol->write_u16 (protocol, value, error); +} -int32_t thrift_protocol_write_ipv4 (ThriftProtocol *protocol, const u_int32_t value, - int *error); +static inline int32_t thrift_protocol_write_u32 (ThriftProtocol *protocol, + const u_int32_t value, + int *error) +{ + return protocol->write_u32 (protocol, value, error); +} -int32_t thrift_protocol_write_ipaddr (ThriftProtocol *protocol, - const ipaddr_t *value, int *error); +static inline int32_t thrift_protocol_write_u64 (ThriftProtocol *protocol, + const u_int64_t value, + int *error) +{ + return protocol->write_u64 (protocol, value, error); +} -int32_t thrift_protocol_write_double (ThriftProtocol *protocol, - const double value, int *error); +static inline int32_t thrift_protocol_write_ipv4 (ThriftProtocol *protocol, + const u_int32_t value, + int *error) +{ + return protocol->write_ipv4 (protocol, value, error); +} -int32_t thrift_protocol_write_string (ThriftProtocol *protocol, - const char *str, int *error); +static inline int32_t thrift_protocol_write_ipaddr (ThriftProtocol *protocol, + const ipaddr_t *value, + int *error) +{ + return protocol->write_ipaddr (protocol, value, error); +} -int32_t thrift_protocol_write_binary (ThriftProtocol *protocol, - const void *buf, - const u_int32_t len, int *error); +static inline int32_t thrift_protocol_write_double (ThriftProtocol *protocol, + const double value, + int *error) +{ + return protocol->write_double (protocol, value, error); +} -int32_t thrift_protocol_write_xml (ThriftProtocol *protocol, - const char *str, int *error); +static inline int32_t thrift_protocol_write_string (ThriftProtocol *protocol, + const char *str, + int *error) +{ + return protocol->write_string (protocol, str, error); +} -int32_t thrift_protocol_write_uuid_t (ThriftProtocol *protocol, - const uuid_t value, int *error); +static inline int32_t thrift_protocol_write_binary (ThriftProtocol *protocol, + const void *buf, + const u_int32_t len, + int *error) +{ + return protocol->write_binary (protocol, buf, len, error); +} -int32_t thrift_protocol_read_message_begin (ThriftProtocol *thrift_protocol, - char **name, - ThriftMessageType *message_type, - int32_t *seqid, int *error); +static inline int32_t thrift_protocol_write_xml (ThriftProtocol *protocol, + const char *str, + int *error) +{ + return protocol->write_xml (protocol, str, error); +} -int32_t thrift_protocol_read_message_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_write_uuid_t (ThriftProtocol *protocol, + const ct_uuid_t value, + int *error) +{ + return protocol->write_uuid_t (protocol, value, error); +} -int32_t thrift_protocol_read_sandesh_begin (ThriftProtocol *thrift_protocol, +static inline int32_t thrift_protocol_read_sandesh_begin ( + ThriftProtocol *protocol, char **name, - int *error); + int *error) +{ + return protocol->read_sandesh_begin (protocol, name, error); +} -int32_t thrift_protocol_read_sandesh_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_read_sandesh_end ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->read_sandesh_end (protocol, error); +} -int32_t thrift_protocol_read_struct_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_read_struct_begin ( + ThriftProtocol *protocol, char **name, - int *error); + int *error) +{ + return protocol->read_struct_begin (protocol, name, error); +} -int32_t thrift_protocol_read_struct_end (ThriftProtocol *protocol, - int *error); +static inline int32_t thrift_protocol_read_struct_end ( + ThriftProtocol *protocol, + int *error) +{ + return protocol->read_struct_end (protocol, error); +} -int32_t thrift_protocol_read_field_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_read_field_begin ( + ThriftProtocol *protocol, char **name, ThriftType *field_type, int16_t *field_id, - int *error); - -int32_t thrift_protocol_read_field_end (ThriftProtocol *protocol, - int *error); - -int32_t thrift_protocol_read_map_begin (ThriftProtocol *protocol, - ThriftType *key_type, - ThriftType *value_type, u_int32_t *size, - int *error); + int *error) +{ + return protocol->read_field_begin (protocol, name, field_type, field_id, + error); +} -int32_t thrift_protocol_read_map_end (ThriftProtocol *protocol, int *error); +static inline int32_t thrift_protocol_read_field_end (ThriftProtocol *protocol, + int *error) +{ + return protocol->read_field_end (protocol, error); +} -int32_t thrift_protocol_read_list_begin (ThriftProtocol *protocol, +static inline int32_t thrift_protocol_read_list_begin ( + ThriftProtocol *protocol, ThriftType *element_type, - u_int32_t *size, int *error); - -int32_t thrift_protocol_read_list_end (ThriftProtocol *protocol, int *error); + u_int32_t *size, int *error) +{ + return protocol->read_list_begin (protocol, element_type, size, error); +} -int32_t thrift_protocol_read_set_begin (ThriftProtocol *protocol, - ThriftType *element_type, - u_int32_t *size, int *error); +static inline int32_t thrift_protocol_read_list_end (ThriftProtocol *protocol, + int *error) +{ + return protocol->read_list_end (protocol, error); +} -int32_t thrift_protocol_read_set_end (ThriftProtocol *protocol, int *error); +static inline int32_t thrift_protocol_read_bool (ThriftProtocol *protocol, + u_int8_t *value, + int *error) +{ + return protocol->read_bool (protocol, value, error); +} -int32_t thrift_protocol_read_bool (ThriftProtocol *protocol, u_int8_t *value, - int *error); +static inline int32_t thrift_protocol_read_byte (ThriftProtocol *protocol, + int8_t *value, + int *error) +{ + return protocol->read_byte (protocol, value, error); +} -int32_t thrift_protocol_read_byte (ThriftProtocol *protocol, int8_t *value, - int *error); +static inline int32_t thrift_protocol_read_i16 (ThriftProtocol *protocol, + int16_t *value, + int *error) +{ + return protocol->read_i16 (protocol, value, error); +} -int32_t thrift_protocol_read_i16 (ThriftProtocol *protocol, int16_t *value, - int *error); +static inline int32_t thrift_protocol_read_i32 (ThriftProtocol *protocol, + int32_t *value, + int *error) +{ + return protocol->read_i32 (protocol, value, error); +} -int32_t thrift_protocol_read_i32 (ThriftProtocol *protocol, int32_t *value, - int *error); +static inline int32_t thrift_protocol_read_i64 (ThriftProtocol *protocol, + int64_t *value, + int *error) +{ + return protocol->read_i64 (protocol, value, error); +} -int32_t thrift_protocol_read_i64 (ThriftProtocol *protocol, int64_t *value, - int *error); +static inline int32_t thrift_protocol_read_u16 (ThriftProtocol *protocol, + u_int16_t *value, + int *error) +{ + return protocol->read_u16 (protocol, value, error); +} -int32_t thrift_protocol_read_u16 (ThriftProtocol *protocol, u_int16_t *value, - int *error); +static inline int32_t thrift_protocol_read_u32 (ThriftProtocol *protocol, + u_int32_t *value, + int *error) +{ + return protocol->read_u32 (protocol, value, error); +} -int32_t thrift_protocol_read_u32 (ThriftProtocol *protocol, u_int32_t *value, - int *error); -int32_t thrift_protocol_read_u64 (ThriftProtocol *protocol, u_int64_t *value, - int *error); +static inline int32_t thrift_protocol_read_u64 (ThriftProtocol *protocol, + u_int64_t *value, + int *error) +{ + return protocol->read_u64 (protocol, value, error); +} -int32_t thrift_protocol_read_ipv4 (ThriftProtocol *protocol, u_int32_t *value, - int *error); +static inline int32_t thrift_protocol_read_ipv4 (ThriftProtocol *protocol, + u_int32_t *value, + int *error) +{ + return protocol->read_ipv4 (protocol, value, error); +} -int32_t thrift_protocol_read_ipaddr (ThriftProtocol *protocol, ipaddr_t *value, - int *error); +static inline int32_t thrift_protocol_read_ipaddr (ThriftProtocol *protocol, + ipaddr_t *value, + int *error) +{ + return protocol->read_ipaddr (protocol, value, error); +} -int32_t thrift_protocol_read_double (ThriftProtocol *protocol, - double *value, int *error); +static inline int32_t thrift_protocol_read_double (ThriftProtocol *protocol, + double *value, int *error) +{ + return protocol->read_double (protocol, value, error); +} -int32_t thrift_protocol_read_string (ThriftProtocol *protocol, - char **str, int *error); +static inline int32_t thrift_protocol_read_string (ThriftProtocol *protocol, + char **str, int *error) +{ + return protocol->read_string (protocol, str, error); +} -int32_t thrift_protocol_read_binary (ThriftProtocol *protocol, - void **buf, u_int32_t *len, - int *error); +static inline int32_t thrift_protocol_read_binary (ThriftProtocol *protocol, + void **buf, u_int32_t *len, + int *error) +{ + return protocol->read_binary (protocol, buf, len, error); +} -int32_t thrift_protocol_read_xml (ThriftProtocol *protocol, - char **str, int *error); +static inline int32_t thrift_protocol_read_xml (ThriftProtocol *protocol, + char **str, int *error) +{ + return protocol->read_xml (protocol, str, error); +} -int32_t thrift_protocol_read_uuid_t (ThriftProtocol *protocol, - uuid_t *value, int *error); +static inline int32_t thrift_protocol_read_uuid_t (ThriftProtocol *protocol, + ct_uuid_t *value, int *error) +{ + return protocol->read_uuid_t (protocol, value, error); +} int32_t thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, int *error); diff --git a/library/c/sandesh.c b/library/c/sandesh.c index a1ca6621..846c0cf5 100644 --- a/library/c/sandesh.c +++ b/library/c/sandesh.c @@ -35,24 +35,38 @@ sandesh_find_info (sandesh_info_t *infos, const char *name) { static int32_t sandesh_decode_one (u_int8_t *buf, u_int32_t buf_len, sandesh_find_info_fn sinfo_find_fn, int *error) { - int32_t rxfer, ret; + int32_t rxfer = 0, ret, sread_len = 0; char *sname = NULL; - ThriftBinaryProtocol protocol; - ThriftMemoryBuffer transport; sandesh_info_t *sinfo; void *sandesh; - - /* Initialize the transport and protocol */ - thrift_memory_buffer_init(&transport, buf, buf_len); - thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); - thrift_memory_buffer_wrote_bytes(&transport, buf_len); + union { + void * b[4]; + int32_t all; + } bytes4; /* Read the sandesh name */ - rxfer = thrift_protocol_read_sandesh_begin(&protocol, - &sname, - error); - if (rxfer < 0) { - os_log(OS_LOG_ERR, "Read sandesh begin FAILED (%d)", *error); + if (buf_len < rxfer + 4) { + os_log(OS_LOG_ERR, "Read sandesh begin FAILED"); + ret = -1; + goto exit; + } + memcpy(bytes4.b, buf + rxfer, 4); + rxfer += 4; + sread_len = ntohl(bytes4.all); + if (sread_len > 0) { + if (buf_len < rxfer + sread_len) { + os_log(OS_LOG_ERR, "Read sandesh begin: Length(%d) FAILED", + sread_len); + ret = -1; + goto exit; + } + sname = os_malloc(sread_len + 1); + memcpy(sname, buf + rxfer, sread_len); + sname[sread_len] = 0; + rxfer += sread_len; + } else { + os_log(OS_LOG_ERR, "Read sandesh begin: Length(%d) INVALID", + sread_len); ret = -1; goto exit; } @@ -75,17 +89,8 @@ sandesh_decode_one (u_int8_t *buf, u_int32_t buf_len, sandesh_find_info_fn sinfo goto exit; } - /* - * Reinitialize the transport and protocol, this is needed so that - * the sandesh name read above is done again in the sandesh type specific - * read function - */ - thrift_memory_buffer_init(&transport, buf, buf_len); - thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); - thrift_memory_buffer_wrote_bytes(&transport, buf_len); - /* Now, read the sandesh */ - rxfer = sinfo->read(sandesh, &protocol, error); + rxfer = sinfo->read_binary_from_buffer(sandesh, buf, buf_len, error); if (rxfer < 0) { os_log(OS_LOG_ERR, "%s read FAILED (%d)", sname, *error); sinfo->free(sandesh); @@ -145,8 +150,6 @@ int32_t sandesh_encode (void *sandesh, const char *sname, sandesh_find_info_fn sinfo_find_fn, u_int8_t *buf, u_int32_t buf_len, int *error) { - ThriftBinaryProtocol protocol; - ThriftMemoryBuffer transport; sandesh_info_t *sinfo; int32_t wxfer; @@ -158,12 +161,8 @@ sandesh_encode (void *sandesh, const char *sname, return -1; } - /* Initialize the transport and protocol */ - thrift_memory_buffer_init(&transport, buf, buf_len); - thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); - /* Now, write the sandesh */ - wxfer = sinfo->write(sandesh, &protocol, error); + wxfer = sinfo->write_binary_to_buffer(sandesh, buf, buf_len, error); if (wxfer < 0) { #ifdef __KERNEL__ if (vrouter_dbg) { diff --git a/library/c/sandesh.h b/library/c/sandesh.h index 661b83c6..9dde1800 100644 --- a/library/c/sandesh.h +++ b/library/c/sandesh.h @@ -54,7 +54,7 @@ MALLOC_DECLARE(M_VROUTER); #endif /* __FreeBSD__ */ extern int vrouter_dbg; -#else +#else /* __KERNEL__ */ #include #include @@ -73,10 +73,10 @@ extern int vrouter_dbg; #define os_log(level, format, arg...) syslog(level, format, ##arg) #endif /* __KERNEL__ */ -typedef unsigned char uuid_t[16]; +typedef unsigned char ct_uuid_t[16]; typedef struct ipaddr_s { - uint16_t iptype; // AF_INET or AF_INET6 + uint8_t iptype; // AF_INET or AF_INET6 union { struct in_addr ipv4; struct in6_addr ipv6; @@ -112,7 +112,9 @@ typedef struct sandesh_info_s { const char *name; u_int32_t size; int32_t (*read) (void *, ThriftProtocol *, int *); + int32_t (*read_binary_from_buffer) (void *, uint8_t *, const size_t, int *); int32_t (*write) (void *, ThriftProtocol *, int *); + int32_t (*write_binary_to_buffer) (void *, uint8_t *, const size_t, int *); void (*process) (void *); void (*free) (void *); } sandesh_info_t ; diff --git a/library/c/test/SConscript b/library/c/test/SConscript index b2dcc852..fde94dbd 100644 --- a/library/c/test/SConscript +++ b/library/c/test/SConscript @@ -70,7 +70,7 @@ env.Alias('src/sandesh:sandesh_pre_c_test', sandesh_pre_c_test) sandesh_c_test = env.UnitTest('sandesh_c_test', SandeshCTestGenSrcs + - env['SandeshBufferTestGenCObjs'] + + env['SandeshBufferTestGenCObjs'] + ['sandesh_c_test.cc'] ) diff --git a/library/c/test/sandesh_c_test.cc b/library/c/test/sandesh_c_test.cc index 826aab30..f61af3b1 100644 --- a/library/c/test/sandesh_c_test.cc +++ b/library/c/test/sandesh_c_test.cc @@ -46,36 +46,132 @@ class SandeshCTest : public ::testing::Test { int SandeshCTest::num_decoded_ = 0; int SandeshCTest::num_encoded_ = 0; +static int32_t inner_a(3); +static int16_t inner_b(4); +static int64_t inner_c(5); +static abc_enum inner_d(ABC_ENUM_VAL2); +static uint16_t inner_e(65535); +static uint32_t inner_f(4294967295u); +static uint64_t inner_g(18446744073709551615ull); +static std::string inner_h("abc"); +static uint32_t inner_i(4294967295u); +static ct_uuid_t inner_j = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, + 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; +static std::string inner_k("20.1.1.2"); +static std::string inner_l("2001:abce::4"); +static std::string inner_m("def"); + +static void PopulateAbcInner(abc_inner *inner) { + inner->inner_a = inner_a; + inner->inner_b = inner_b; + inner->inner_c = inner_c; + inner->inner_d = inner_d; + inner->inner_e = inner_e; + inner->inner_f = inner_f; + inner->inner_g = inner_g; + inner->inner_h = const_cast(inner_h.c_str()); + inner->inner_i = inner_i; + memcpy(inner->inner_j, inner_j, sizeof(inner_j)); + inner->inner_k.iptype = AF_INET; + int success(inet_pton(AF_INET, inner_k.c_str(), &inner->inner_k.ipv4)); + EXPECT_EQ(success, 1); + inner->inner_l.iptype = AF_INET6; + success = inet_pton(AF_INET6, inner_l.c_str(), &inner->inner_l.ipv6); + EXPECT_EQ(success, 1); + inner->inner_m = const_cast(inner_m.c_str()); + inner->inner_n = NULL; +} + +static void ValidateAbcInner(abc_inner *inner) { + EXPECT_EQ(inner->inner_a, inner_a); + EXPECT_EQ(inner->inner_b, inner_b); + EXPECT_EQ(inner->inner_c, inner_c); + EXPECT_EQ(inner->inner_d, inner_d); + EXPECT_EQ(inner->inner_e, inner_e); + EXPECT_EQ(inner->inner_f, inner_f); + EXPECT_EQ(inner->inner_g, inner_g); + EXPECT_STREQ(inner->inner_h, inner_h.c_str()); + EXPECT_EQ(inner->inner_i, inner_i); + EXPECT_EQ(0, memcmp(inner->inner_j, + inner_j, sizeof(ct_uuid_t))); + ipaddr_t inner_k_v4; + inner_k_v4.iptype = AF_INET; + int success(inet_pton(AF_INET, inner_k.c_str(), &inner_k_v4.ipv4)); + EXPECT_EQ(success, 1); + EXPECT_EQ(inner->inner_k.iptype, inner_k_v4.iptype); + EXPECT_EQ(0, memcmp(&inner->inner_k.ipv4, + &inner_k_v4.ipv4, 4)); + ipaddr_t inner_l_v6; + inner_l_v6.iptype = AF_INET6; + success = inet_pton(AF_INET6, inner_l.c_str(), &inner_l_v6.ipv6); + EXPECT_EQ(success, 1); + EXPECT_EQ(inner->inner_l.iptype, inner_l_v6.iptype); + EXPECT_EQ(0, memcmp(&inner->inner_l.ipv6, + &inner_l_v6.ipv6, 16)); + EXPECT_STREQ(inner->inner_m, inner_m.c_str()); + EXPECT_TRUE(inner->inner_n == NULL); +} + +static void ValidateAbc(abc *abc) { + ValidateAbcInner(abc->nested); +} + +static void ValidateAbcSandesh(abc_sandesh *abc_sandesh) { + ValidateAbc(abc_sandesh->elem); + EXPECT_EQ(abc_sandesh->rwinfo_size, 5); + for (int i = 0; i < abc_sandesh->rwinfo_size; i++) { + EXPECT_EQ(abc_sandesh->rwinfo[i], i); + } +} + +static void CompareAbcInner(abc_inner *actual, abc_inner *expected) { + EXPECT_EQ(actual->inner_a, expected->inner_a); + EXPECT_EQ(actual->inner_b, expected->inner_b); + EXPECT_EQ(actual->inner_c, expected->inner_c); + EXPECT_EQ(actual->inner_d, expected->inner_d); + EXPECT_EQ(actual->inner_e, expected->inner_e); + EXPECT_EQ(actual->inner_f, expected->inner_f); + EXPECT_EQ(actual->inner_g, expected->inner_g); + EXPECT_STREQ(actual->inner_h, expected->inner_h); + EXPECT_EQ(actual->inner_i, expected->inner_i); + EXPECT_EQ(0, memcmp(actual->inner_j, + expected->inner_j, sizeof(ct_uuid_t))); + EXPECT_EQ(actual->inner_k.iptype, expected->inner_k.iptype); + EXPECT_EQ(0, memcmp(&actual->inner_k.ipv4, + &expected->inner_k.ipv4, 4)); + EXPECT_EQ(actual->inner_l.iptype, expected->inner_l.iptype); + EXPECT_EQ(0, memcmp(&actual->inner_l.ipv6, + &expected->inner_l.ipv6, 16)); + EXPECT_STREQ(actual->inner_m, expected->inner_m); + EXPECT_STREQ(actual->inner_n, expected->inner_n); +} + +static void CompareAbc(abc *actual, abc *expected) { + CompareAbcInner(actual->nested, expected->nested); +} + +static bool CompareAbcSandesh(abc_sandesh *actual, abc_sandesh *expected) { + CompareAbc(actual->elem, expected->elem); + EXPECT_EQ(actual->rwinfo_size, expected->rwinfo_size); + for (int i = 0; i < actual->rwinfo_size; i++) { + EXPECT_EQ(actual->rwinfo[i], expected->rwinfo[i]); + } +} + TEST_F(SandeshCTest, ReadWrite) { abc wabc, rabc; u_int8_t buf[256]; ThriftBinaryProtocol protocol; ThriftMemoryBuffer transport; int error = 0, wxfer, rxfer; - std::string inner_h("abc"); - uuid_t uuid_test = - {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, - 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; /* Initialize the transport and protocol */ - memset(buf, 0, sizeof(buf)); thrift_memory_buffer_init(&transport, buf, sizeof(buf)); thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); /* Write the struct */ wabc.nested = (abc_inner *)calloc(1, sizeof(*wabc.nested)); - wabc.nested->inner_a = 3; - wabc.nested->inner_b = 4; - wabc.nested->inner_c = 5; - wabc.nested->inner_d = ABC_ENUM_VAL2; - wabc.nested->inner_e = 65535; - wabc.nested->inner_f = 4294967295u; - wabc.nested->inner_g = 18446744073709551615ull; - wabc.nested->inner_h = const_cast(inner_h.c_str()); - wabc.nested->inner_i = 4294967295u; - memcpy(wabc.nested->inner_j, uuid_test, sizeof(uuid_t)); - wabc.nested->inner_k.iptype = AF_INET; - inet_pton(AF_INET, "20.1.1.2", &wabc.nested->inner_k.ipv4); + PopulateAbcInner(wabc.nested); wxfer = abc_write(&wabc, &protocol, &error); EXPECT_GT(wxfer, 0); @@ -85,27 +181,11 @@ TEST_F(SandeshCTest, ReadWrite) { /* Verify */ EXPECT_EQ(wxfer, rxfer); - EXPECT_EQ(wabc.nested->inner_a, rabc.nested->inner_a); - EXPECT_EQ(wabc.nested->inner_b, rabc.nested->inner_b); - EXPECT_EQ(wabc.nested->inner_c, rabc.nested->inner_c); - EXPECT_EQ(wabc.nested->inner_d, rabc.nested->inner_d); - EXPECT_EQ(wabc.nested->inner_e, rabc.nested->inner_e); - EXPECT_EQ(wabc.nested->inner_f, rabc.nested->inner_f); - EXPECT_EQ(wabc.nested->inner_g, rabc.nested->inner_g); - EXPECT_STREQ(wabc.nested->inner_h, rabc.nested->inner_h); - EXPECT_EQ(wabc.nested->inner_i, rabc.nested->inner_i); - EXPECT_EQ(0, memcmp(wabc.nested->inner_j, - rabc.nested->inner_j, sizeof(uuid_t))); - EXPECT_EQ(wabc.nested->inner_k.iptype, rabc.nested->inner_k.iptype); - EXPECT_EQ(0, memcmp(&wabc.nested->inner_k.ipv4, - &rabc.nested->inner_k.ipv4, 4)); + CompareAbcInner(rabc.nested, wabc.nested); free(wabc.nested); wabc.nested = NULL; - free(rabc.nested->inner_h); - rabc.nested->inner_h = NULL; - free(rabc.nested); - rabc.nested = NULL; + abc_free(&rabc); } TEST_F(SandeshCTest, EncodeDecode) { @@ -113,27 +193,11 @@ TEST_F(SandeshCTest, EncodeDecode) { int error = 0, wxfer, wxfer1, rxfer; u_int32_t i; u_int8_t buf[512]; - std::string inner_h("abc"); - uuid_t uuid_test = - {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, - 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; memset(buf, 0, sizeof(buf)); wabc_sandesh.elem = (abc *)calloc(1, sizeof(*wabc_sandesh.elem)); wabc_sandesh.elem->nested = (abc_inner *)calloc(1, sizeof(*wabc_sandesh.elem->nested)); - wabc_sandesh.elem->nested->inner_a = 3; - wabc_sandesh.elem->nested->inner_b = 4; - wabc_sandesh.elem->nested->inner_c = 5; - wabc_sandesh.elem->nested->inner_d = ABC_ENUM_VAL1; - wabc_sandesh.elem->nested->inner_e = 65535; - wabc_sandesh.elem->nested->inner_f = 4294967295u; - wabc_sandesh.elem->nested->inner_g = 18446744073709551615ull; - wabc_sandesh.elem->nested->inner_h = const_cast(inner_h.c_str()); - wabc_sandesh.elem->nested->inner_i = 4294967295u; - memcpy(wabc_sandesh.elem->nested->inner_j, uuid_test, sizeof(uuid_t)); - wabc_sandesh.elem->nested->inner_k.iptype = AF_INET6; - inet_pton(AF_INET6, "2001:abce::4", - &wabc_sandesh.elem->nested->inner_k.ipv6); + PopulateAbcInner(wabc_sandesh.elem->nested); wabc_sandesh.rwinfo_size = 5; wabc_sandesh.rwinfo = (int8_t *)calloc(1, sizeof(*wabc_sandesh.rwinfo) * wabc_sandesh.rwinfo_size); @@ -203,28 +267,12 @@ TEST_F(SandeshCTest, GetEncodeLength) { int error = 0, wxfer, exfer; u_int32_t i; u_int8_t buf[256]; - std::string inner_h("abc"); - uuid_t uuid_test = - {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, - 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; memset(buf, 0, sizeof(buf)); wabc_sandesh.elem = (abc *)calloc(1, sizeof(*wabc_sandesh.elem)); wabc_sandesh.elem->nested = (abc_inner *)calloc(1, sizeof(*wabc_sandesh.elem->nested)); - wabc_sandesh.elem->nested->inner_a = 3; - wabc_sandesh.elem->nested->inner_b = 4; - wabc_sandesh.elem->nested->inner_c = 5; - wabc_sandesh.elem->nested->inner_d = ABC_ENUM_VAL1; - wabc_sandesh.elem->nested->inner_e = 65535; - wabc_sandesh.elem->nested->inner_f = 4294967295u; - wabc_sandesh.elem->nested->inner_g = 18446744073709551615ull; - wabc_sandesh.elem->nested->inner_h = const_cast(inner_h.c_str()); - wabc_sandesh.elem->nested->inner_i = 4294967295u; - memcpy(wabc_sandesh.elem->nested->inner_j, uuid_test, sizeof(uuid_t)); - wabc_sandesh.elem->nested->inner_k.iptype = AF_INET; - inet_pton(AF_INET, "192.168.10.1", - &wabc_sandesh.elem->nested->inner_k.ipv4); + PopulateAbcInner(wabc_sandesh.elem->nested); wabc_sandesh.rwinfo_size = 5; wabc_sandesh.rwinfo = (int8_t *)calloc(1, sizeof(*wabc_sandesh.rwinfo) * wabc_sandesh.rwinfo_size); @@ -253,28 +301,192 @@ TEST_F(SandeshCTest, GetEncodeLength) { free(wabc_sandesh.rwinfo); wabc_sandesh.rwinfo = NULL; } + +TEST_F(SandeshCTest, SandeshEncodePerf) { + abc_inner inner; + PopulateAbcInner(&inner); + u_int8_t buf[512]; + ThriftBinaryProtocol protocol; + ThriftMemoryBuffer transport; + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); + for (int i = 0; i < 1000000; i++) { + /* Initialize the transport and protocol */ + thrift_memory_buffer_init(&transport, buf, sizeof(buf)); + int error(0); + int32_t xfer(abc_inner_write(&inner, &protocol, &error)); + EXPECT_EQ(error, 0); + int32_t xfer1(abc_inner_write(&inner, &protocol, &error)); + EXPECT_EQ(error, 0); + } +} + +TEST_F(SandeshCTest, SandeshDecodePerf) { + abc_inner winner; + PopulateAbcInner(&winner); + u_int8_t buf[256]; + ThriftBinaryProtocol protocol; + ThriftMemoryBuffer transport; + thrift_memory_buffer_init(&transport, buf, sizeof(buf)); + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); + int error(0); + int32_t wxfer(abc_inner_write(&winner, &protocol, &error)); + ASSERT_GT(wxfer, 0); + ASSERT_EQ(error, 0); + + for (int i = 0; i < 10; i++) { + abc_inner rinner; + thrift_memory_buffer_init(&transport, buf, sizeof(buf)); + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, (ThriftTransport *)&transport); + thrift_memory_buffer_wrote_bytes(&transport, wxfer); + int32_t rxfer(abc_inner_read(&rinner, &protocol, &error)); + ASSERT_EQ(error, 0); + ASSERT_EQ(rxfer, wxfer); + abc_inner_free(&rinner); + } +} + +TEST_F(SandeshCTest, SandeshEncodeBufferPerf) { + abc_inner inner; + PopulateAbcInner(&inner); + u_int8_t buf[512]; + for (int i = 0; i < 1000000; i++) { + int error(0); + int32_t offset(abc_inner_write_binary_to_buffer(&inner, buf, + sizeof(buf), &error)); + EXPECT_GT(offset, 0); + int32_t offset1(abc_inner_write_binary_to_buffer(&inner, buf + offset, + sizeof(buf) - offset, &error)); + EXPECT_GT(offset1, 0); + } +} + +TEST_F(SandeshCTest, SandeshDecodeBufferPerf) { + abc_inner winner; + PopulateAbcInner(&winner); + u_int8_t buf[256]; + int error(0); + int32_t wxfer(abc_inner_write_binary_to_buffer(&winner, buf, sizeof(buf), + &error)); + ASSERT_GT(wxfer, 0); + ASSERT_EQ(error, 0); + + for (int i = 0; i < 10; i++) { + abc_inner rinner; + int32_t rxfer(abc_inner_read_binary_from_buffer(&rinner, buf, wxfer, + &error)); + ASSERT_EQ(error, 0); + ASSERT_EQ(rxfer, wxfer); + abc_inner_free(&rinner); + } +} + +TEST_F(SandeshCTest, SandeshGenBufferValidate) { + // abc_inner + // Encode + abc_inner inner; + PopulateAbcInner(&inner); + u_int8_t buf[512]; + memset(buf, 0, sizeof(buf)); + int error(0); + int32_t offset(abc_inner_write_binary_to_buffer(&inner, buf, sizeof(buf), + &error)); + EXPECT_EQ(error, 0); + u_int8_t buf1[512]; + memset(buf1, 0, sizeof(buf1)); + ThriftBinaryProtocol protocol; + ThriftMemoryBuffer transport; + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, + (ThriftTransport *)&transport); + thrift_memory_buffer_init(&transport, buf1, sizeof(buf1)); + int error1(0); + int32_t offset1(abc_inner_write(&inner, &protocol, &error1)); + EXPECT_EQ(error1, 0); + EXPECT_EQ(offset, offset1); + EXPECT_EQ(0, memcmp(buf, buf1, sizeof(buf))); + // Decode + abc_inner rinner; + int32_t roffset(abc_inner_read_binary_from_buffer(&rinner, buf, offset, + &error)); + EXPECT_EQ(error, 0); + EXPECT_EQ(roffset, offset); + ValidateAbcInner(&rinner); + CompareAbcInner(&rinner, &inner); + abc_inner_free(&rinner); + // abc + abc wabc; + wabc.nested = (abc_inner *)calloc(1, sizeof(*wabc.nested)); + PopulateAbcInner(wabc.nested); + memset(buf, 0, sizeof(buf)); + offset = abc_write_binary_to_buffer(&wabc, buf, sizeof(buf), &error); + EXPECT_EQ(error, 0); + memset(buf1, 0, sizeof(buf1)); + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, + (ThriftTransport *)&transport); + thrift_memory_buffer_init(&transport, buf1, sizeof(buf1)); + offset1 = abc_write(&wabc, &protocol, &error1); + EXPECT_EQ(error1, 0); + EXPECT_EQ(offset, offset1); + EXPECT_EQ(0, memcmp(buf, buf1, sizeof(buf))); + // Decode + abc rabc; + roffset = abc_read_binary_from_buffer(&rabc, buf1, offset1, &error1); + EXPECT_EQ(error1, 0); + EXPECT_EQ(roffset, offset1); + ValidateAbc(&rabc); + CompareAbc(&rabc, &wabc); + abc_free(&rabc); + free(wabc.nested); + wabc.nested = NULL; + // abc_sandesh + abc_sandesh wabc_sandesh; + int i; + wabc_sandesh.elem = (abc *)calloc(1, sizeof(*wabc_sandesh.elem)); + wabc_sandesh.elem->nested = (abc_inner *)calloc(1, + sizeof(*wabc_sandesh.elem->nested)); + PopulateAbcInner(wabc_sandesh.elem->nested); + wabc_sandesh.rwinfo_size = 5; + wabc_sandesh.rwinfo = (int8_t *)calloc(1, sizeof(*wabc_sandesh.rwinfo) * + wabc_sandesh.rwinfo_size); + for (i = 0; i < wabc_sandesh.rwinfo_size; i++) { + wabc_sandesh.rwinfo[i] = i; + } + memset(buf, 0, sizeof(buf)); + offset = abc_sandesh_write_binary_to_buffer(&wabc_sandesh, buf, + sizeof(buf), &error); + EXPECT_EQ(error, 0); + memset(buf1, 0, sizeof(buf1)); + thrift_protocol_init(&protocol, T_PROTOCOL_BINARY, + (ThriftTransport *)&transport); + thrift_memory_buffer_init(&transport, buf1, sizeof(buf1)); + offset1 = abc_sandesh_write(&wabc_sandesh, &protocol, &error1); + EXPECT_EQ(error1, 0); + EXPECT_EQ(offset, offset1); + EXPECT_EQ(0, memcmp(buf, buf1, sizeof(buf))); + // Decode + abc_sandesh rabc_sandesh; + roffset = abc_sandesh_read_binary_from_buffer(&rabc_sandesh, buf, offset, + &error); + EXPECT_EQ(error, 0); + EXPECT_EQ(roffset, offset); + ValidateAbcSandesh(&rabc_sandesh); + CompareAbcSandesh(&rabc_sandesh, &wabc_sandesh); + abc_sandesh_free(&rabc_sandesh); + free(wabc_sandesh.elem->nested); + wabc_sandesh.elem->nested = NULL; + free(wabc_sandesh.elem); + wabc_sandesh.elem = NULL; + free(wabc_sandesh.rwinfo); + wabc_sandesh.rwinfo = NULL; +} + } // namespace void abc_sandesh_process (void *pabc) { u_int32_t i; - uuid_t uuid_value = - {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, - 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; abc_sandesh *psandesh = (abc_sandesh *)pabc; assert(psandesh); - EXPECT_EQ(psandesh->elem->nested->inner_a, 3); - EXPECT_EQ(psandesh->elem->nested->inner_b, 4); - EXPECT_EQ(psandesh->elem->nested->inner_c, 5); - EXPECT_EQ(psandesh->elem->nested->inner_d, ABC_ENUM_VAL1); - EXPECT_EQ(psandesh->elem->nested->inner_e, 65535); - EXPECT_EQ(psandesh->elem->nested->inner_f, 4294967295u); - EXPECT_EQ(psandesh->elem->nested->inner_g, 18446744073709551615ull); - EXPECT_STREQ(psandesh->elem->nested->inner_h, "abc"); - EXPECT_EQ(psandesh->elem->nested->inner_i, 4294967295u); - EXPECT_EQ(0, memcmp(psandesh->elem->nested->inner_j, - uuid_value, sizeof(uuid_t))); - + ValidateAbcInner(psandesh->elem->nested); for (i = 0; i < psandesh->rwinfo_size; i++) { EXPECT_EQ(psandesh->rwinfo[i], i); } @@ -284,7 +496,7 @@ abc_sandesh_process (void *pabc) { void buffer_test_process (void *ptr) { int i; - uuid_t uuid_value = + ct_uuid_t uuid_value = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; BufferTest *psandesh = (BufferTest *)ptr; @@ -295,18 +507,18 @@ buffer_test_process (void *ptr) { for (i = 0; i < 5; i++) { EXPECT_EQ(psandesh->listElem1[i], i); EXPECT_EQ(psandesh->listElem2[i], i); - uuid_t uuid_temp = + ct_uuid_t uuid_temp = {0x00+i,0x00+i,0x01+i,0x01+i,0x02+i,0x02+i,0x03+i,0x03+i, 0x04+i,0x04+i,0x05+i,0x05+i,0x06+i,0x06+i,0x07+i,0x07+i}; EXPECT_EQ(0, memcmp(psandesh->listElem3[i], - uuid_temp, sizeof(uuid_t))); + uuid_temp, sizeof(ct_uuid_t))); } EXPECT_EQ(psandesh->u16Elem1, 65535); EXPECT_EQ(psandesh->u32Elem1, 4294967295u); EXPECT_EQ(psandesh->u64Elem1, 18446744073709551615ull); EXPECT_STREQ(psandesh->xmlElem1, "xmlElem1"); EXPECT_EQ(psandesh->ipv4Elem1, 4294967295u); - EXPECT_EQ(0, memcmp(psandesh->uuidElem1, uuid_value, sizeof(uuid_t))); + EXPECT_EQ(0, memcmp(psandesh->uuidElem1, uuid_value, sizeof(ct_uuid_t))); ipaddr_t ipaddr1; inet_pton(AF_INET, "11.11.11.3", &ipaddr1.ipv4); EXPECT_EQ(psandesh->ipaddrElem1.iptype, AF_INET); diff --git a/library/c/test/sandesh_c_test.sandesh b/library/c/test/sandesh_c_test.sandesh index 3531d341..67763fd6 100644 --- a/library/c/test/sandesh_c_test.sandesh +++ b/library/c/test/sandesh_c_test.sandesh @@ -24,15 +24,18 @@ struct abc_inner { 7:u64 inner_g; 8:xml inner_h; 9:ipv4 inner_i; - 10:uuid_t inner_j; + 10:ct_uuid_t inner_j; 11:ipaddr inner_k; + 12:ipaddr inner_l; + 13:string inner_m; + 14:string inner_n; } struct abc { 1:abc_inner nested; } -systemlog sandesh abc_sandesh { +buffer sandesh abc_sandesh { 1:abc elem; 2:list rwinfo; } diff --git a/library/c/thrift.h b/library/c/thrift.h index e7fd93d5..1bdd8d7d 100644 --- a/library/c/thrift.h +++ b/library/c/thrift.h @@ -24,10 +24,6 @@ extern "C" { #endif -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - /* this macro is called to satisfy -Wall hardcore compilation */ #ifndef THRIFT_UNUSED_VAR # define THRIFT_UNUSED_VAR(x) ((void) x) diff --git a/library/c/transport/thrift_fake_transport.c b/library/c/transport/thrift_fake_transport.c index fe3c8d84..8caddab2 100644 --- a/library/c/transport/thrift_fake_transport.c +++ b/library/c/transport/thrift_fake_transport.c @@ -10,34 +10,6 @@ #include "sandesh.h" -/* implements thrift_transport_is_open */ -u_int8_t -thrift_fake_transport_is_open (ThriftTransport *transport) -{ - THRIFT_UNUSED_VAR (transport); - return 1; -} - -/* implements thrift_transport_open */ -u_int8_t -thrift_fake_transport_open (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - -/* implements thrift_transport_close */ -u_int8_t -thrift_fake_transport_close (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - /* implements thrift_transport_read */ int32_t thrift_fake_transport_read (ThriftTransport *transport, void *buf, @@ -51,17 +23,6 @@ thrift_fake_transport_read (ThriftTransport *transport, void *buf, return -1; } -/* implements thrift_transport_read_end - * called when read is complete. nothing to do on our end. */ -u_int8_t -thrift_fake_transport_read_end (ThriftTransport *transport, - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - /* implements thrift_transport_write */ u_int8_t thrift_fake_transport_write (ThriftTransport *transport, @@ -76,38 +37,11 @@ thrift_fake_transport_write (ThriftTransport *transport, return 1; } -/* implements thrift_transport_write_end - * called when write is complete. nothing to do on our end. */ -u_int8_t -thrift_fake_transport_write_end (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - -/* implements thrift_transport_flush */ -u_int8_t -thrift_fake_transport_flush (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - /* initializes the instance */ void thrift_fake_transport_init (ThriftFakeTransport *transport) { transport->tt.ttype = T_TRANSPORT_FAKE; - transport->tt.is_open = thrift_fake_transport_is_open; - transport->tt.open = thrift_fake_transport_open; - transport->tt.close = thrift_fake_transport_close; transport->tt.read = thrift_fake_transport_read; - transport->tt.read_end = thrift_fake_transport_read_end; transport->tt.write = thrift_fake_transport_write; - transport->tt.write_end = thrift_fake_transport_write_end; - transport->tt.flush = thrift_fake_transport_flush; } diff --git a/library/c/transport/thrift_memory_buffer.c b/library/c/transport/thrift_memory_buffer.c index 7bd709a3..78a2fc20 100644 --- a/library/c/transport/thrift_memory_buffer.c +++ b/library/c/transport/thrift_memory_buffer.c @@ -90,34 +90,6 @@ thrift_memory_buffer_wrote_bytes (ThriftMemoryBuffer *t, u_int32_t len) return 0; } -/* implements thrift_transport_is_open */ -u_int8_t -thrift_memory_buffer_is_open (ThriftTransport *transport) -{ - THRIFT_UNUSED_VAR (transport); - return 1; -} - -/* implements thrift_transport_open */ -u_int8_t -thrift_memory_buffer_open (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - -/* implements thrift_transport_close */ -u_int8_t -thrift_memory_buffer_close (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - /* implements thrift_transport_read */ int32_t thrift_memory_buffer_read (ThriftTransport *transport, void *buf, @@ -189,39 +161,12 @@ thrift_memory_buffer_write (ThriftTransport *transport, return 1; } -/* implements thrift_transport_write_end - * called when write is complete. nothing to do on our end. */ -u_int8_t -thrift_memory_buffer_write_end (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - -/* implements thrift_transport_flush */ -u_int8_t -thrift_memory_buffer_flush (ThriftTransport *transport , - int *error) -{ - THRIFT_UNUSED_VAR (transport); - THRIFT_UNUSED_VAR (error); - return 1; -} - static void thrift_memory_buffer_transport_init (ThriftTransport *transport) { transport->ttype = T_TRANSPORT_MEMORY_BUFFER; - transport->is_open = thrift_memory_buffer_is_open; - transport->open = thrift_memory_buffer_open; - transport->close = thrift_memory_buffer_close; transport->read = thrift_memory_buffer_read; - transport->read_end = thrift_memory_buffer_read_end; transport->write = thrift_memory_buffer_write; - transport->write_end = thrift_memory_buffer_write_end; - transport->flush = thrift_memory_buffer_flush; } /* initializes the instance */ diff --git a/library/c/transport/thrift_transport.c b/library/c/transport/thrift_transport.c index defab759..e69de29b 100644 --- a/library/c/transport/thrift_transport.c +++ b/library/c/transport/thrift_transport.c @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include "sandesh.h" - -#if 0 - -u_int8_t -thrift_transport_is_open (ThriftTransport *transport) -{ - return transport->is_open (transport); -} - -u_int8_t -thrift_transport_open (ThriftTransport *transport, int *error) -{ - return transport->open (transport, error); -} - -u_int8_t -thrift_transport_close (ThriftTransport *transport, int *error) -{ - return transport->close (transport, error); -} - -int32_t -thrift_transport_read (ThriftTransport *transport, void *buf, - u_int32_t len, int *error) -{ - return transport->read (transport, buf, len, error); -} - -u_int8_t -thrift_transport_read_end (ThriftTransport *transport, int *error) -{ - return transport->read_end (transport, error); -} - -u_int8_t -thrift_transport_write (ThriftTransport *transport, const void *buf, - const u_int32_t len, int *error) -{ - return transport->write (transport, buf, len, error); -} - -u_int8_t -thrift_transport_write_end (ThriftTransport *transport, int *error) -{ - return transport->write_end (transport, error); -} - -u_int8_t -thrift_transport_flush (ThriftTransport *transport, int *error) -{ - return transport->flush (transport, error); -} - -#endif diff --git a/library/c/transport/thrift_transport.h b/library/c/transport/thrift_transport.h index 6eb57a60..804f64bc 100644 --- a/library/c/transport/thrift_transport.h +++ b/library/c/transport/thrift_transport.h @@ -39,16 +39,10 @@ struct _ThriftTransport { ThriftTransportType ttype; /* vtable */ - u_int8_t (*is_open) (struct _ThriftTransport *transport); - u_int8_t (*open) (struct _ThriftTransport *transport, int *error); - u_int8_t (*close) (struct _ThriftTransport *transport, int *error); int32_t (*read) (struct _ThriftTransport *transport, void *buf, u_int32_t len, int *error); - u_int8_t (*read_end) (struct _ThriftTransport *transport, int *error); u_int8_t (*write) (struct _ThriftTransport *transport, const void *buf, const u_int32_t len, int *error); - u_int8_t (*write_end) (struct _ThriftTransport *transport, int *error); - u_int8_t (*flush) (struct _ThriftTransport *transport, int *error); }; typedef struct _ThriftTransport ThriftTransport; @@ -57,36 +51,6 @@ ThriftTransportType thrift_transport_get_type (void); /* virtual public methods */ -/*! - * Checks if this transport is opened. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_is_open (ThriftTransport *transport) -{ - return transport->is_open (transport); -} - -/*! - * Open the transport for reading and writing. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_open (ThriftTransport *transport, int *error) -{ - return transport->open (transport, error); -} - -/*! - * Close the transport. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_close (ThriftTransport *transport, int *error) -{ - return transport->close (transport, error); -} - /*! * Read some data into the buffer buf. * \public \memberof ThriftTransportInterface @@ -98,16 +62,6 @@ thrift_transport_read (ThriftTransport *transport, void *buf, return transport->read (transport, buf, len, error); } -/*! - * Called when read is completed. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_read_end (ThriftTransport *transport, int *error) -{ - return transport->read_end (transport, error); -} - /*! * Writes data from a buffer to the transport. * \public \memberof ThriftTransportInterface @@ -119,28 +73,6 @@ thrift_transport_write (ThriftTransport *transport, const void *buf, return transport->write (transport, buf, len, error); } -/*! - * Called when write is completed. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_write_end (ThriftTransport *transport, int *error) -{ - return transport->write_end (transport, error); -} - -/*! - * Flushes any pending data to be written. Typically used with buffered - * transport mechanisms. - * \public \memberof ThriftTransportInterface - */ -static inline u_int8_t -thrift_transport_flush (ThriftTransport *transport, int *error) -{ - return transport->flush (transport, error); -} - - /* define error/exception types */ typedef enum { diff --git a/library/common/test/sandesh_buffer_test.sandesh b/library/common/test/sandesh_buffer_test.sandesh index 09b282ae..662b5219 100644 --- a/library/common/test/sandesh_buffer_test.sandesh +++ b/library/common/test/sandesh_buffer_test.sandesh @@ -19,8 +19,8 @@ buffer sandesh BufferTest { 8: u64 u64Elem1; 9: xml xmlElem1; 10: ipv4 ipv4Elem1; - 11: uuid_t uuidElem1; - 12: list listElem3; + 11: ct_uuid_t uuidElem1; + 12: list listElem3; 13: ipaddr ipaddrElem1; 14: list listElem4; } diff --git a/library/cpp/derived_stats_algo.h b/library/cpp/derived_stats_algo.h index 347b801e..b0ef5832 100644 --- a/library/cpp/derived_stats_algo.h +++ b/library/cpp/derived_stats_algo.h @@ -274,7 +274,7 @@ class DSSum { // We want each time bucket to represent between // 1/128th and 1/256th of the entire range // This ensures that there will never be more than 256 buckets - while ((1 << (shifter_ + 8)) < range_usecs_) shifter_++; + while ((uint64_t)(1 << (shifter_ + 8)) < range_usecs_) shifter_++; } } uint64_t samples_; diff --git a/library/cpp/protocol/TXMLProtocol.h b/library/cpp/protocol/TXMLProtocol.h index b4826e1e..b87b5c8a 100644 --- a/library/cpp/protocol/TXMLProtocol.h +++ b/library/cpp/protocol/TXMLProtocol.h @@ -10,6 +10,7 @@ #include #include +#include namespace contrail { namespace sandesh { namespace protocol { diff --git a/library/cpp/sandesh_uve.h b/library/cpp/sandesh_uve.h index 523e3893..12608776 100644 --- a/library/cpp/sandesh_uve.h +++ b/library/cpp/sandesh_uve.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/library/cpp/test/sandesh_http_test.sandesh b/library/cpp/test/sandesh_http_test.sandesh index 6f89763b..e7cb45f3 100644 --- a/library/cpp/test/sandesh_http_test.sandesh +++ b/library/cpp/test/sandesh_http_test.sandesh @@ -25,7 +25,7 @@ response sandesh SandeshHttpTestResp { 3: string teststring1; 4: string teststring2; 5: ipaddr testIpaddr1; - 6: uuid_t testUuid1; + 6: ct_uuid_t testUuid1; } request sandesh SandeshHttpTestRequest { @@ -34,5 +34,5 @@ request sandesh SandeshHttpTestRequest { 3: string teststring1; 4: string teststring2; 5: ipaddr testIpaddr1; - 6: uuid_t testUuid1; + 6: ct_uuid_t testUuid1; } diff --git a/library/cpp/test/sandesh_rw_test.sandesh b/library/cpp/test/sandesh_rw_test.sandesh index dc33f268..26f53e28 100644 --- a/library/cpp/test/sandesh_rw_test.sandesh +++ b/library/cpp/test/sandesh_rw_test.sandesh @@ -27,9 +27,9 @@ struct SandeshStructTest { 12: xml xmlTest2; 13: xml xmlTest3; 14: ipv4 ipv4Test; - 15: uuid_t uuidTest; - 16: list uuidListTest; - 17: uuid_t uuidDefaultTest = "00010203-0405-0607-0423-023434265323"; + 15: ct_uuid_t uuidTest; + 16: list uuidListTest; + 17: ct_uuid_t uuidDefaultTest = "00010203-0405-0607-0423-023434265323"; 18: ipaddr ipaddrv4Test 19: ipaddr ipaddrv6Test 20: list ipaddrListTest diff --git a/library/cpp/test/sandesh_send_queue_test.sandesh b/library/cpp/test/sandesh_send_queue_test.sandesh index dbd6e7bc..be31e907 100644 --- a/library/cpp/test/sandesh_send_queue_test.sandesh +++ b/library/cpp/test/sandesh_send_queue_test.sandesh @@ -27,9 +27,9 @@ struct SandeshStructTest { 12: xml xmlTest2; 13: xml xmlTest3; 14: ipv4 ipv4Test; - 15: uuid_t uuidTest; - 16: list uuidListTest; - 17: uuid_t uuidDefaultTest = "00010203-0405-0607-0423-023434265323"; + 15: ct_uuid_t uuidTest; + 16: list uuidListTest; + 17: ct_uuid_t uuidDefaultTest = "00010203-0405-0607-0423-023434265323"; } response sandesh SandeshResponseTest { diff --git a/library/python/pysandesh/test/encode_decode_test.sandesh b/library/python/pysandesh/test/encode_decode_test.sandesh index bcc57424..2a30d79c 100644 --- a/library/python/pysandesh/test/encode_decode_test.sandesh +++ b/library/python/pysandesh/test/encode_decode_test.sandesh @@ -25,7 +25,7 @@ systemlog sandesh BasicTypesTest { 14: xml xml_3; 15: xml xml_4; 16: ipv4 ipv4_1; - 17: uuid_t uuid_1; + 17: ct_uuid_t uuid_1; 18: ipaddr ipaddr_1; 19: ipaddr ipaddr_2; } @@ -43,8 +43,8 @@ struct StructBasicTypes { 10: xml xml_3; 11: xml xml_4; 12: ipv4 ipv4_1; - 13: uuid_t uuid_1; - 14: uuid_t uuid_2 = "00010203-0405-0607-0423-023434265323"; + 13: ct_uuid_t uuid_1; + 14: ct_uuid_t uuid_2 = "00010203-0405-0607-0423-023434265323"; } objectlog sandesh StructTest { @@ -56,7 +56,7 @@ struct StructContainerTypes { 1: list li32_1; 2: list lstring_1; 3: list lbst_1; - 4: list luuid_1; + 4: list luuid_1; 5: list lipaddr_1; } diff --git a/library/python/pysandesh/test/msg_test.sandesh b/library/python/pysandesh/test/msg_test.sandesh index 29d383bc..b9fa0a12 100644 --- a/library/python/pysandesh/test/msg_test.sandesh +++ b/library/python/pysandesh/test/msg_test.sandesh @@ -12,7 +12,7 @@ systemlog sandesh SystemLogTest { 1: "Async Test"; 2: list list1; 3: map map1; - 4: uuid_t uuid1; + 4: ct_uuid_t uuid1; } struct StructObject { diff --git a/library/python/pysandesh/test/sandesh_http_test.sandesh b/library/python/pysandesh/test/sandesh_http_test.sandesh index f0e8772b..f01640b1 100644 --- a/library/python/pysandesh/test/sandesh_http_test.sandesh +++ b/library/python/pysandesh/test/sandesh_http_test.sandesh @@ -11,6 +11,6 @@ request sandesh SandeshHttpRequest { 1: i32 testID; 2: string teststring1; 3: string teststring2; - 4: uuid_t testUuid1; + 4: ct_uuid_t testUuid1; 5: ipaddr testIpaddr1; }