From 843c2ba4c1498aae2448b1cd9fd433aa6eabd1ae Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 19:08:24 -0400 Subject: [PATCH 1/8] initial commit --- Jason/lab3/CMakeList.txt | 22 +++ Jason/lab3/default.nix | 9 + Jason/lab3/flake.nix | 43 +++++ Jason/lab3/proto/message.pb.cc | 310 ++++++++++++++++++++++++++++++ Jason/lab3/proto/message.pb.h | 337 +++++++++++++++++++++++++++++++++ Jason/lab3/proto/message.proto | 8 + Jason/lab3/src/UDPSocket.cpp | 70 +++++++ Jason/lab3/src/UDPSocket.hpp | 24 +++ Jason/lab3/src/client.cpp | 28 +++ Jason/lab3/src/server.cpp | 27 +++ 10 files changed, 878 insertions(+) create mode 100644 Jason/lab3/CMakeList.txt create mode 100644 Jason/lab3/default.nix create mode 100644 Jason/lab3/flake.nix create mode 100644 Jason/lab3/proto/message.pb.cc create mode 100644 Jason/lab3/proto/message.pb.h create mode 100644 Jason/lab3/proto/message.proto create mode 100644 Jason/lab3/src/UDPSocket.cpp create mode 100644 Jason/lab3/src/UDPSocket.hpp create mode 100644 Jason/lab3/src/client.cpp create mode 100644 Jason/lab3/src/server.cpp diff --git a/Jason/lab3/CMakeList.txt b/Jason/lab3/CMakeList.txt new file mode 100644 index 0000000..64c0c94 --- /dev/null +++ b/Jason/lab3/CMakeList.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) +project(Lab3) + +find_package(Protobuf REQUIRED) + +# Define the library +add_library(msg STATIC proto/message.pb.cc) + +#Link the necessary targets +target_link_libraries(msg PUBLIC protobuf::libprotobuf) + +# Specify include directories for this library +target_include_directories(hello_lib PUBLIC include) + + +# Add the server executable +add_executable(server src/server.cpp) + +# Add the client executable +add_executable(client src/client.cpp ) + +install (TARGETS server client DESTINATION ${CMAKE_INSTALL_BINDIR}) \ No newline at end of file diff --git a/Jason/lab3/default.nix b/Jason/lab3/default.nix new file mode 100644 index 0000000..58c7164 --- /dev/null +++ b/Jason/lab3/default.nix @@ -0,0 +1,9 @@ +{ stdenv, cmake, protobuf }: + +stdenv.mkDerivation rec { + pname = "hello_lib"; + version = "0.1.0"; + src = ./.; + nativeBuildInputs = [ cmake ]; + buildInputs = [ protobuf ]; +} \ No newline at end of file diff --git a/Jason/lab3/flake.nix b/Jason/lab3/flake.nix new file mode 100644 index 0000000..0c12065 --- /dev/null +++ b/Jason/lab3/flake.nix @@ -0,0 +1,43 @@ +{ + description = "Lab 3 flake"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + nixutils.url = "github:numtide/flake-utils"; + }; + + outputs = { + self, nixpkgs, nixutils + }: + + let hello_world_overlay = final: prev: { + hello_world = final.callPackage ./default.nix { }; + }; + + my_overlays = [ hello_world_overlay ]; + pkgs = import nixpkgs { + system = "aarch64-darwin"; + overlays = [ self.overlays.default ]; + }; + + in { + overlays.default = nixpkgs.lib.composeManyExtensions my_overlays; + + packages.aarch64-darwin = + rec { + hello_world = pkgs.hello_world; + default = hello_world; + }; + + devShells.aarch64-darwin.default = + pkgs.mkShell rec { + name = "nix-devshell"; + packages = with pkgs; [ + cmake + hello_world + ]; + shellHook = '' + export PS1="$(echo -e '\uF121') {\[$(tput sgr0)\]\[\033[38;5;228m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]} (${name}) \\$ \[$(tput sgr0)\]" + ''; + }; + }; +} diff --git a/Jason/lab3/proto/message.pb.cc b/Jason/lab3/proto/message.pb.cc new file mode 100644 index 0000000..d43af16 --- /dev/null +++ b/Jason/lab3/proto/message.pb.cc @@ -0,0 +1,310 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: message.proto + +#include "message.pb.h" + +#include +#include "google/protobuf/io/coded_stream.h" +#include "google/protobuf/extension_set.h" +#include "google/protobuf/wire_format_lite.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/generated_message_reflection.h" +#include "google/protobuf/reflection_ops.h" +#include "google/protobuf/wire_format.h" +#include "google/protobuf/generated_message_tctable_impl.h" +// @@protoc_insertion_point(includes) + +// Must be included last. +#include "google/protobuf/port_def.inc" +PROTOBUF_PRAGMA_INIT_SEG +namespace _pb = ::google::protobuf; +namespace _pbi = ::google::protobuf::internal; +namespace _fl = ::google::protobuf::internal::field_layout; +namespace example { + +inline constexpr SimpleMessage::Impl_::Impl_( + ::_pbi::ConstantInitialized) noexcept + : text_( + &::google::protobuf::internal::fixed_address_empty_string, + ::_pbi::ConstantInitialized()), + _cached_size_{0} {} + +template +PROTOBUF_CONSTEXPR SimpleMessage::SimpleMessage(::_pbi::ConstantInitialized) + : _impl_(::_pbi::ConstantInitialized()) {} +struct SimpleMessageDefaultTypeInternal { + PROTOBUF_CONSTEXPR SimpleMessageDefaultTypeInternal() : _instance(::_pbi::ConstantInitialized{}) {} + ~SimpleMessageDefaultTypeInternal() {} + union { + SimpleMessage _instance; + }; +}; + +PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT + PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 SimpleMessageDefaultTypeInternal _SimpleMessage_default_instance_; +} // namespace example +static ::_pb::Metadata file_level_metadata_message_2eproto[1]; +static constexpr const ::_pb::EnumDescriptor** + file_level_enum_descriptors_message_2eproto = nullptr; +static constexpr const ::_pb::ServiceDescriptor** + file_level_service_descriptors_message_2eproto = nullptr; +const ::uint32_t TableStruct_message_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( + protodesc_cold) = { + ~0u, // no _has_bits_ + PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _internal_metadata_), + ~0u, // no _extensions_ + ~0u, // no _oneof_case_ + ~0u, // no _weak_field_map_ + ~0u, // no _inlined_string_donated_ + ~0u, // no _split_ + ~0u, // no sizeof(Split) + PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _impl_.text_), +}; + +static const ::_pbi::MigrationSchema + schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + {0, -1, -1, sizeof(::example::SimpleMessage)}, +}; + +static const ::_pb::Message* const file_default_instances[] = { + &::example::_SimpleMessage_default_instance_._instance, +}; +const char descriptor_table_protodef_message_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + "\n\rmessage.proto\022\007example\"\035\n\rSimpleMessag" + "e\022\014\n\004text\030\001 \001(\tb\006proto3" +}; +static ::absl::once_flag descriptor_table_message_2eproto_once; +const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { + false, + false, + 63, + descriptor_table_protodef_message_2eproto, + "message.proto", + &descriptor_table_message_2eproto_once, + nullptr, + 0, + 1, + schemas, + file_default_instances, + TableStruct_message_2eproto::offsets, + file_level_metadata_message_2eproto, + file_level_enum_descriptors_message_2eproto, + file_level_service_descriptors_message_2eproto, +}; + +// This function exists to be marked as weak. +// It can significantly speed up compilation by breaking up LLVM's SCC +// in the .pb.cc translation units. Large translation units see a +// reduction of more than 35% of walltime for optimized builds. Without +// the weak attribute all the messages in the file, including all the +// vtables and everything they use become part of the same SCC through +// a cycle like: +// GetMetadata -> descriptor table -> default instances -> +// vtables -> GetMetadata +// By adding a weak function here we break the connection from the +// individual vtables back into the descriptor table. +PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_message_2eproto_getter() { + return &descriptor_table_message_2eproto; +} +// Force running AddDescriptors() at dynamic initialization time. +PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 +static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_message_2eproto(&descriptor_table_message_2eproto); +namespace example { +// =================================================================== + +class SimpleMessage::_Internal { + public: +}; + +SimpleMessage::SimpleMessage(::google::protobuf::Arena* arena) + : ::google::protobuf::Message(arena) { + SharedCtor(arena); + // @@protoc_insertion_point(arena_constructor:example.SimpleMessage) +} +inline PROTOBUF_NDEBUG_INLINE SimpleMessage::Impl_::Impl_( + ::google::protobuf::internal::InternalVisibility visibility, ::google::protobuf::Arena* arena, + const Impl_& from) + : text_(arena, from.text_), + _cached_size_{0} {} + +SimpleMessage::SimpleMessage( + ::google::protobuf::Arena* arena, + const SimpleMessage& from) + : ::google::protobuf::Message(arena) { + SimpleMessage* const _this = this; + (void)_this; + _internal_metadata_.MergeFrom<::google::protobuf::UnknownFieldSet>( + from._internal_metadata_); + new (&_impl_) Impl_(internal_visibility(), arena, from._impl_); + + // @@protoc_insertion_point(copy_constructor:example.SimpleMessage) +} +inline PROTOBUF_NDEBUG_INLINE SimpleMessage::Impl_::Impl_( + ::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena) + : text_(arena), + _cached_size_{0} {} + +inline void SimpleMessage::SharedCtor(::_pb::Arena* arena) { + new (&_impl_) Impl_(internal_visibility(), arena); +} +SimpleMessage::~SimpleMessage() { + // @@protoc_insertion_point(destructor:example.SimpleMessage) + _internal_metadata_.Delete<::google::protobuf::UnknownFieldSet>(); + SharedDtor(); +} +inline void SimpleMessage::SharedDtor() { + ABSL_DCHECK(GetArena() == nullptr); + _impl_.text_.Destroy(); + _impl_.~Impl_(); +} + +PROTOBUF_NOINLINE void SimpleMessage::Clear() { +// @@protoc_insertion_point(message_clear_start:example.SimpleMessage) + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ::uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + + _impl_.text_.ClearToEmpty(); + _internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>(); +} + +const char* SimpleMessage::_InternalParse( + const char* ptr, ::_pbi::ParseContext* ctx) { + ptr = ::_pbi::TcParser::ParseLoop(this, ptr, ctx, &_table_.header); + return ptr; +} + + +PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 +const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { + { + 0, // no _has_bits_ + 0, // no _extensions_ + 1, 0, // max_field_number, fast_idx_mask + offsetof(decltype(_table_), field_lookup_table), + 4294967294, // skipmap + offsetof(decltype(_table_), field_entries), + 1, // num_field_entries + 0, // num_aux_entries + offsetof(decltype(_table_), field_names), // no aux_entries + &_SimpleMessage_default_instance_._instance, + ::_pbi::TcParser::GenericFallback, // fallback + }, {{ + // string text = 1; + {::_pbi::TcParser::FastUS1, + {10, 63, 0, PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_.text_)}}, + }}, {{ + 65535, 65535 + }}, {{ + // string text = 1; + {PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_.text_), 0, 0, + (0 | ::_fl::kFcSingular | ::_fl::kUtf8String | ::_fl::kRepAString)}, + }}, + // no aux_entries + {{ + "\25\4\0\0\0\0\0\0" + "example.SimpleMessage" + "text" + }}, +}; + +::uint8_t* SimpleMessage::_InternalSerialize( + ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) const { + // @@protoc_insertion_point(serialize_to_array_start:example.SimpleMessage) + ::uint32_t cached_has_bits = 0; + (void)cached_has_bits; + + // string text = 1; + if (!this->_internal_text().empty()) { + const std::string& _s = this->_internal_text(); + ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + _s.data(), static_cast(_s.length()), ::google::protobuf::internal::WireFormatLite::SERIALIZE, "example.SimpleMessage.text"); + target = stream->WriteStringMaybeAliased(1, _s, target); + } + + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = + ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance), target, stream); + } + // @@protoc_insertion_point(serialize_to_array_end:example.SimpleMessage) + return target; +} + +::size_t SimpleMessage::ByteSizeLong() const { +// @@protoc_insertion_point(message_byte_size_start:example.SimpleMessage) + ::size_t total_size = 0; + + ::uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + + // string text = 1; + if (!this->_internal_text().empty()) { + total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( + this->_internal_text()); + } + + return MaybeComputeUnknownFieldsSize(total_size, &_impl_._cached_size_); +} + +const ::google::protobuf::Message::ClassData SimpleMessage::_class_data_ = { + SimpleMessage::MergeImpl, + nullptr, // OnDemandRegisterArenaDtor +}; +const ::google::protobuf::Message::ClassData* SimpleMessage::GetClassData() const { + return &_class_data_; +} + +void SimpleMessage::MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg) { + auto* const _this = static_cast(&to_msg); + auto& from = static_cast(from_msg); + // @@protoc_insertion_point(class_specific_merge_from_start:example.SimpleMessage) + ABSL_DCHECK_NE(&from, _this); + ::uint32_t cached_has_bits = 0; + (void) cached_has_bits; + + if (!from._internal_text().empty()) { + _this->_internal_set_text(from._internal_text()); + } + _this->_internal_metadata_.MergeFrom<::google::protobuf::UnknownFieldSet>(from._internal_metadata_); +} + +void SimpleMessage::CopyFrom(const SimpleMessage& from) { +// @@protoc_insertion_point(class_specific_copy_from_start:example.SimpleMessage) + if (&from == this) return; + Clear(); + MergeFrom(from); +} + +PROTOBUF_NOINLINE bool SimpleMessage::IsInitialized() const { + return true; +} + +::_pbi::CachedSize* SimpleMessage::AccessCachedSize() const { + return &_impl_._cached_size_; +} +void SimpleMessage::InternalSwap(SimpleMessage* PROTOBUF_RESTRICT other) { + using std::swap; + auto* arena = GetArena(); + ABSL_DCHECK_EQ(arena, other->GetArena()); + _internal_metadata_.InternalSwap(&other->_internal_metadata_); + ::_pbi::ArenaStringPtr::InternalSwap(&_impl_.text_, &other->_impl_.text_, arena); +} + +::google::protobuf::Metadata SimpleMessage::GetMetadata() const { + return ::_pbi::AssignDescriptors( + &descriptor_table_message_2eproto_getter, &descriptor_table_message_2eproto_once, + file_level_metadata_message_2eproto[0]); +} +// @@protoc_insertion_point(namespace_scope) +} // namespace example +namespace google { +namespace protobuf { +} // namespace protobuf +} // namespace google +// @@protoc_insertion_point(global_scope) +#include "google/protobuf/port_undef.inc" diff --git a/Jason/lab3/proto/message.pb.h b/Jason/lab3/proto/message.pb.h new file mode 100644 index 0000000..7a48d5e --- /dev/null +++ b/Jason/lab3/proto/message.pb.h @@ -0,0 +1,337 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: message.proto +// Protobuf C++ Version: 4.25.4 + +#ifndef GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh +#define GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh + +#include +#include +#include +#include + +#include "google/protobuf/port_def.inc" +#if PROTOBUF_VERSION < 4025000 +#error "This file was generated by a newer version of protoc which is" +#error "incompatible with your Protocol Buffer headers. Please update" +#error "your headers." +#endif // PROTOBUF_VERSION + +#if 4025004 < PROTOBUF_MIN_PROTOC_VERSION +#error "This file was generated by an older version of protoc which is" +#error "incompatible with your Protocol Buffer headers. Please" +#error "regenerate this file with a newer version of protoc." +#endif // PROTOBUF_MIN_PROTOC_VERSION +#include "google/protobuf/port_undef.inc" +#include "google/protobuf/io/coded_stream.h" +#include "google/protobuf/arena.h" +#include "google/protobuf/arenastring.h" +#include "google/protobuf/generated_message_tctable_decl.h" +#include "google/protobuf/generated_message_util.h" +#include "google/protobuf/metadata_lite.h" +#include "google/protobuf/generated_message_reflection.h" +#include "google/protobuf/message.h" +#include "google/protobuf/repeated_field.h" // IWYU pragma: export +#include "google/protobuf/extension_set.h" // IWYU pragma: export +#include "google/protobuf/unknown_field_set.h" +// @@protoc_insertion_point(includes) + +// Must be included last. +#include "google/protobuf/port_def.inc" + +#define PROTOBUF_INTERNAL_EXPORT_message_2eproto + +namespace google { +namespace protobuf { +namespace internal { +class AnyMetadata; +} // namespace internal +} // namespace protobuf +} // namespace google + +// Internal implementation detail -- do not use these members. +struct TableStruct_message_2eproto { + static const ::uint32_t offsets[]; +}; +extern const ::google::protobuf::internal::DescriptorTable + descriptor_table_message_2eproto; +namespace example { +class SimpleMessage; +struct SimpleMessageDefaultTypeInternal; +extern SimpleMessageDefaultTypeInternal _SimpleMessage_default_instance_; +} // namespace example +namespace google { +namespace protobuf { +} // namespace protobuf +} // namespace google + +namespace example { + +// =================================================================== + + +// ------------------------------------------------------------------- + +class SimpleMessage final : + public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:example.SimpleMessage) */ { + public: + inline SimpleMessage() : SimpleMessage(nullptr) {} + ~SimpleMessage() override; + template + explicit PROTOBUF_CONSTEXPR SimpleMessage(::google::protobuf::internal::ConstantInitialized); + + inline SimpleMessage(const SimpleMessage& from) + : SimpleMessage(nullptr, from) {} + SimpleMessage(SimpleMessage&& from) noexcept + : SimpleMessage() { + *this = ::std::move(from); + } + + inline SimpleMessage& operator=(const SimpleMessage& from) { + CopyFrom(from); + return *this; + } + inline SimpleMessage& operator=(SimpleMessage&& from) noexcept { + if (this == &from) return *this; + if (GetArena() == from.GetArena() + #ifdef PROTOBUF_FORCE_COPY_IN_MOVE + && GetArena() != nullptr + #endif // !PROTOBUF_FORCE_COPY_IN_MOVE + ) { + InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + + inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const + ABSL_ATTRIBUTE_LIFETIME_BOUND { + return _internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance); + } + inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() + ABSL_ATTRIBUTE_LIFETIME_BOUND { + return _internal_metadata_.mutable_unknown_fields<::google::protobuf::UnknownFieldSet>(); + } + + static const ::google::protobuf::Descriptor* descriptor() { + return GetDescriptor(); + } + static const ::google::protobuf::Descriptor* GetDescriptor() { + return default_instance().GetMetadata().descriptor; + } + static const ::google::protobuf::Reflection* GetReflection() { + return default_instance().GetMetadata().reflection; + } + static const SimpleMessage& default_instance() { + return *internal_default_instance(); + } + static inline const SimpleMessage* internal_default_instance() { + return reinterpret_cast( + &_SimpleMessage_default_instance_); + } + static constexpr int kIndexInFileMessages = + 0; + + friend void swap(SimpleMessage& a, SimpleMessage& b) { + a.Swap(&b); + } + inline void Swap(SimpleMessage* other) { + if (other == this) return; + #ifdef PROTOBUF_FORCE_COPY_IN_SWAP + if (GetArena() != nullptr && + GetArena() == other->GetArena()) { + #else // PROTOBUF_FORCE_COPY_IN_SWAP + if (GetArena() == other->GetArena()) { + #endif // !PROTOBUF_FORCE_COPY_IN_SWAP + InternalSwap(other); + } else { + ::google::protobuf::internal::GenericSwap(this, other); + } + } + void UnsafeArenaSwap(SimpleMessage* other) { + if (other == this) return; + ABSL_DCHECK(GetArena() == other->GetArena()); + InternalSwap(other); + } + + // implements Message ---------------------------------------------- + + SimpleMessage* New(::google::protobuf::Arena* arena = nullptr) const final { + return CreateMaybeMessage(arena); + } + using ::google::protobuf::Message::CopyFrom; + void CopyFrom(const SimpleMessage& from); + using ::google::protobuf::Message::MergeFrom; + void MergeFrom( const SimpleMessage& from) { + SimpleMessage::MergeImpl(*this, from); + } + private: + static void MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg); + public: + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; + bool IsInitialized() const final; + + ::size_t ByteSizeLong() const final; + const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; + ::uint8_t* _InternalSerialize( + ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; + int GetCachedSize() const { return _impl_._cached_size_.Get(); } + + private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; + void SharedCtor(::google::protobuf::Arena* arena); + void SharedDtor(); + void InternalSwap(SimpleMessage* other); + + private: + friend class ::google::protobuf::internal::AnyMetadata; + static ::absl::string_view FullMessageName() { + return "example.SimpleMessage"; + } + protected: + explicit SimpleMessage(::google::protobuf::Arena* arena); + SimpleMessage(::google::protobuf::Arena* arena, const SimpleMessage& from); + public: + + static const ClassData _class_data_; + const ::google::protobuf::Message::ClassData*GetClassData() const final; + + ::google::protobuf::Metadata GetMetadata() const final; + + // nested types ---------------------------------------------------- + + // accessors ------------------------------------------------------- + + enum : int { + kTextFieldNumber = 1, + }; + // string text = 1; + void clear_text() ; + const std::string& text() const; + template + void set_text(Arg_&& arg, Args_... args); + std::string* mutable_text(); + PROTOBUF_NODISCARD std::string* release_text(); + void set_allocated_text(std::string* value); + + private: + const std::string& _internal_text() const; + inline PROTOBUF_ALWAYS_INLINE void _internal_set_text( + const std::string& value); + std::string* _internal_mutable_text(); + + public: + // @@protoc_insertion_point(class_scope:example.SimpleMessage) + private: + class _Internal; + + friend class ::google::protobuf::internal::TcParser; + static const ::google::protobuf::internal::TcParseTable< + 0, 1, 0, + 34, 2> + _table_; + friend class ::google::protobuf::MessageLite; + friend class ::google::protobuf::Arena; + template + friend class ::google::protobuf::Arena::InternalHelper; + using InternalArenaConstructable_ = void; + using DestructorSkippable_ = void; + struct Impl_ { + + inline explicit constexpr Impl_( + ::google::protobuf::internal::ConstantInitialized) noexcept; + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena); + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena, const Impl_& from); + ::google::protobuf::internal::ArenaStringPtr text_; + mutable ::google::protobuf::internal::CachedSize _cached_size_; + PROTOBUF_TSAN_DECLARE_MEMBER + }; + union { Impl_ _impl_; }; + friend struct ::TableStruct_message_2eproto; +}; + +// =================================================================== + + + + +// =================================================================== + + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif // __GNUC__ +// ------------------------------------------------------------------- + +// SimpleMessage + +// string text = 1; +inline void SimpleMessage::clear_text() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + _impl_.text_.ClearToEmpty(); +} +inline const std::string& SimpleMessage::text() const + ABSL_ATTRIBUTE_LIFETIME_BOUND { + // @@protoc_insertion_point(field_get:example.SimpleMessage.text) + return _internal_text(); +} +template +inline PROTOBUF_ALWAYS_INLINE void SimpleMessage::set_text(Arg_&& arg, + Args_... args) { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + _impl_.text_.Set(static_cast(arg), args..., GetArena()); + // @@protoc_insertion_point(field_set:example.SimpleMessage.text) +} +inline std::string* SimpleMessage::mutable_text() ABSL_ATTRIBUTE_LIFETIME_BOUND { + std::string* _s = _internal_mutable_text(); + // @@protoc_insertion_point(field_mutable:example.SimpleMessage.text) + return _s; +} +inline const std::string& SimpleMessage::_internal_text() const { + PROTOBUF_TSAN_READ(&_impl_._tsan_detect_race); + return _impl_.text_.Get(); +} +inline void SimpleMessage::_internal_set_text(const std::string& value) { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + _impl_.text_.Set(value, GetArena()); +} +inline std::string* SimpleMessage::_internal_mutable_text() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + return _impl_.text_.Mutable( GetArena()); +} +inline std::string* SimpleMessage::release_text() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + // @@protoc_insertion_point(field_release:example.SimpleMessage.text) + return _impl_.text_.Release(); +} +inline void SimpleMessage::set_allocated_text(std::string* value) { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + _impl_.text_.SetAllocated(value, GetArena()); + #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING + if (_impl_.text_.IsDefault()) { + _impl_.text_.Set("", GetArena()); + } + #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING + // @@protoc_insertion_point(field_set_allocated:example.SimpleMessage.text) +} + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif // __GNUC__ + +// @@protoc_insertion_point(namespace_scope) +} // namespace example + + +// @@protoc_insertion_point(global_scope) + +#include "google/protobuf/port_undef.inc" + +#endif // GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh diff --git a/Jason/lab3/proto/message.proto b/Jason/lab3/proto/message.proto new file mode 100644 index 0000000..3c7c817 --- /dev/null +++ b/Jason/lab3/proto/message.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package example; + +// Define a simple message with a single string field +message SimpleMessage { + string text = 1; +} \ No newline at end of file diff --git a/Jason/lab3/src/UDPSocket.cpp b/Jason/lab3/src/UDPSocket.cpp new file mode 100644 index 0000000..350a2a6 --- /dev/null +++ b/Jason/lab3/src/UDPSocket.cpp @@ -0,0 +1,70 @@ +#include "UDPSocket.hpp" +#include +#include +#include + +UDPSocket::UDPSocket() { + // Create the socket using IPv4 and UDP + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + std::cerr << "Error creating socket\n"; + } + + // Initialize local and remote address structs + memset(&localAddr, 0, sizeof(localAddr)); + memset(&remoteAddr, 0, sizeof(remoteAddr)); +} + +UDPSocket::~UDPSocket() { + closeSocket(); +} + +bool UDPSocket::bindSocket(const std::string& ip, int port) { + localAddr.sin_family = AF_INET; + localAddr.sin_port = htons(port); + localAddr.sin_addr.s_addr = inet_addr(ip.c_str()); + + // Bind the socket to the specified IP and port + if (bind(sockfd, (struct sockaddr*)&localAddr, sizeof(localAddr)) < 0) { + std::cerr << "Error binding socket to " << ip << ":" << port << "\n"; + return false; + } + + return true; +} + +ssize_t UDPSocket::sendTo(const std::string& message, const std::string& ip, int port) { + remoteAddr.sin_family = AF_INET; + remoteAddr.sin_port = htons(port); + remoteAddr.sin_addr.s_addr = inet_addr(ip.c_str()); + + ssize_t bytesSent = sendto(sockfd, message.c_str(), message.length(), 0, + (struct sockaddr*)&remoteAddr, sizeof(remoteAddr)); + if (bytesSent < 0) { + std::cerr << "Error sending message\n"; + } + return bytesSent; +} + +ssize_t UDPSocket::receiveFrom(std::string& message, std::string& senderIp, int& senderPort) { + char buffer[1024]; + socklen_t addrLen = sizeof(remoteAddr); + ssize_t bytesReceived = recvfrom(sockfd, buffer, sizeof(buffer), 0, + (struct sockaddr*)&remoteAddr, &addrLen); + + if (bytesReceived > 0) { + message = std::string(buffer, bytesReceived); + senderIp = inet_ntoa(remoteAddr.sin_addr); + senderPort = ntohs(remoteAddr.sin_port); + } else { + std::cerr << "Error receiving message\n"; + } + + return bytesReceived; +} + +void UDPSocket::closeSocket() { + if (sockfd >= 0) { + close(sockfd); + } +} diff --git a/Jason/lab3/src/UDPSocket.hpp b/Jason/lab3/src/UDPSocket.hpp new file mode 100644 index 0000000..b5d95a2 --- /dev/null +++ b/Jason/lab3/src/UDPSocket.hpp @@ -0,0 +1,24 @@ +#ifndef UDP_SOCKET_H +#define UDP_SOCKET_H + +#include +#include + +class UDPSocket { +public: + UDPSocket(); + ~UDPSocket(); + + bool bindSocket(const std::string& ip, int port); + ssize_t sendTo(const std::string& message, const std::string& ip, int port); + ssize_t receiveFrom(std::string& message, std::string& senderIp, int& senderPort); + +private: + int sockfd; + struct sockaddr_in localAddr; + struct sockaddr_in remoteAddr; + + void closeSocket(); +}; + +#endif // UDP_SOCKET_H diff --git a/Jason/lab3/src/client.cpp b/Jason/lab3/src/client.cpp new file mode 100644 index 0000000..0612a71 --- /dev/null +++ b/Jason/lab3/src/client.cpp @@ -0,0 +1,28 @@ +#include "UDPSocket.hpp" +#include +#include + +int main() { + UDPSocket udpSocket; + + // Define server IP and port + std::string serverIp = "127.0.0.1"; + int serverPort = 8080; + + // Send the message to the server + udpSocket.sendTo("Test Message", serverIp, serverPort); + std::cout << "Sent message to server: \n"; + + // Receive the response from the server + std::string responseMessage, serverIpReceived; + int serverPortReceived; + ssize_t bytesReceived = udpSocket.receiveFrom(responseMessage, serverIpReceived, serverPortReceived); + + if (bytesReceived > 0) { + std::cout << "Received message from " << serverIp << ":" << serverPort << ": " << responseMessage << "\n"; + } else { + std::cerr << "Error receiving message from server\n"; + } + + return 0; +} diff --git a/Jason/lab3/src/server.cpp b/Jason/lab3/src/server.cpp new file mode 100644 index 0000000..7bcd19b --- /dev/null +++ b/Jason/lab3/src/server.cpp @@ -0,0 +1,27 @@ +#include "UDPSocket.hpp" +#include + +int main() { + UDPSocket udpSocket; + + // Bind the socket to a local IP and port + if (!udpSocket.bindSocket("127.0.0.1", 8080)) { + std::cerr << "Failed to bind socket\n"; + return -1; + } + + while (true) { + // Receive a message + std::string serializedMessage, senderIp; + int senderPort; + ssize_t bytesReceived = udpSocket.receiveFrom(serializedMessage, senderIp, senderPort); + + if (bytesReceived > 0) { + std::cout << "Received message from " << senderIp << ":" << senderPort << ": " << serializedMessage << "\n"; + } else { + std::cerr << "Error receiving message\n"; + } + } + + return 0; +} From caa40ecd64905b3e9ebc9348df9e7463680e6857 Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 19:12:47 -0400 Subject: [PATCH 2/8] fixed CMakeLists --- Jason/lab3/{CMakeList.txt => CMakeLists.txt} | 0 Jason/lab3/flake.lock | 60 ++++++++++++++++++++ Jason/lab3/flake.nix | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) rename Jason/lab3/{CMakeList.txt => CMakeLists.txt} (100%) create mode 100644 Jason/lab3/flake.lock diff --git a/Jason/lab3/CMakeList.txt b/Jason/lab3/CMakeLists.txt similarity index 100% rename from Jason/lab3/CMakeList.txt rename to Jason/lab3/CMakeLists.txt diff --git a/Jason/lab3/flake.lock b/Jason/lab3/flake.lock new file mode 100644 index 0000000..1d70de3 --- /dev/null +++ b/Jason/lab3/flake.lock @@ -0,0 +1,60 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1726182458, + "narHash": "sha256-5sRn295EmC2WRV6stG9eUA0x8t2p1tgCxg1b1qeN3MQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "445b95ccac602dd5f420dab2e335cd0756e50cc1", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixutils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "nixutils": "nixutils" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/Jason/lab3/flake.nix b/Jason/lab3/flake.nix index 0c12065..b1217cc 100644 --- a/Jason/lab3/flake.nix +++ b/Jason/lab3/flake.nix @@ -40,4 +40,4 @@ ''; }; }; -} +} \ No newline at end of file From e688bf5d40a82b0c410fad7b636d3ce22d5afcbb Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 21:16:01 -0400 Subject: [PATCH 3/8] making progress I hope --- Jason/lab3/CMakeLists.txt | 10 +- Jason/lab3/default.nix | 2 +- Jason/lab3/flake.lock | 6 +- Jason/lab3/flake.nix | 13 +- Jason/lab3/src/client.cpp | 1 + Jason/lab3/{ => src}/proto/message.pb.cc | 247 +++++++++++++---------- Jason/lab3/{ => src}/proto/message.pb.h | 169 ++++++++-------- Jason/lab3/{ => src}/proto/message.proto | 0 Jason/lab3/src/server.cpp | 2 + 9 files changed, 242 insertions(+), 208 deletions(-) rename Jason/lab3/{ => src}/proto/message.pb.cc (52%) rename Jason/lab3/{ => src}/proto/message.pb.h (69%) rename Jason/lab3/{ => src}/proto/message.proto (100%) diff --git a/Jason/lab3/CMakeLists.txt b/Jason/lab3/CMakeLists.txt index 64c0c94..7ecb585 100644 --- a/Jason/lab3/CMakeLists.txt +++ b/Jason/lab3/CMakeLists.txt @@ -4,19 +4,19 @@ project(Lab3) find_package(Protobuf REQUIRED) # Define the library -add_library(msg STATIC proto/message.pb.cc) +add_library(msg STATIC src/proto/message.pb.cc) #Link the necessary targets target_link_libraries(msg PUBLIC protobuf::libprotobuf) -# Specify include directories for this library -target_include_directories(hello_lib PUBLIC include) - - # Add the server executable add_executable(server src/server.cpp) +target_link_libraries(server PUBLIC msg) +target_link_libraries(server PUBLIC src/UDPSocket.cpp) # Add the client executable add_executable(client src/client.cpp ) +target_link_libraries(client PUBLIC msg) +target_link_libraries(client PUBLIC src/UDPSocket.cpp) install (TARGETS server client DESTINATION ${CMAKE_INSTALL_BINDIR}) \ No newline at end of file diff --git a/Jason/lab3/default.nix b/Jason/lab3/default.nix index 58c7164..4995284 100644 --- a/Jason/lab3/default.nix +++ b/Jason/lab3/default.nix @@ -1,7 +1,7 @@ { stdenv, cmake, protobuf }: stdenv.mkDerivation rec { - pname = "hello_lib"; + pname = "lab3"; version = "0.1.0"; src = ./.; nativeBuildInputs = [ cmake ]; diff --git a/Jason/lab3/flake.lock b/Jason/lab3/flake.lock index 1d70de3..fba1f5e 100644 --- a/Jason/lab3/flake.lock +++ b/Jason/lab3/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1726182458, - "narHash": "sha256-5sRn295EmC2WRV6stG9eUA0x8t2p1tgCxg1b1qeN3MQ=", + "lastModified": 1726188729, + "narHash": "sha256-7Q3KfkL7omryydTOZTj8KxMVl+B8P6XASkLSHmk8A7c=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "445b95ccac602dd5f420dab2e335cd0756e50cc1", + "rev": "c3dabc54aaee299aa23483c293950eeae55bbf03", "type": "github" }, "original": { diff --git a/Jason/lab3/flake.nix b/Jason/lab3/flake.nix index b1217cc..42e18f2 100644 --- a/Jason/lab3/flake.nix +++ b/Jason/lab3/flake.nix @@ -9,11 +9,11 @@ self, nixpkgs, nixutils }: - let hello_world_overlay = final: prev: { - hello_world = final.callPackage ./default.nix { }; + let lab3_overlay = final: prev: { + lab3 = final.callPackage ./default.nix { }; }; - my_overlays = [ hello_world_overlay ]; + my_overlays = [ lab3_overlay ]; pkgs = import nixpkgs { system = "aarch64-darwin"; overlays = [ self.overlays.default ]; @@ -24,8 +24,8 @@ packages.aarch64-darwin = rec { - hello_world = pkgs.hello_world; - default = hello_world; + lab3 = pkgs.lab3; + default = lab3; }; devShells.aarch64-darwin.default = @@ -33,7 +33,8 @@ name = "nix-devshell"; packages = with pkgs; [ cmake - hello_world + protobuf + lab3 ]; shellHook = '' export PS1="$(echo -e '\uF121') {\[$(tput sgr0)\]\[\033[38;5;228m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]} (${name}) \\$ \[$(tput sgr0)\]" diff --git a/Jason/lab3/src/client.cpp b/Jason/lab3/src/client.cpp index 0612a71..5de1e2d 100644 --- a/Jason/lab3/src/client.cpp +++ b/Jason/lab3/src/client.cpp @@ -1,6 +1,7 @@ #include "UDPSocket.hpp" #include #include +#include "proto/message.pb.h" int main() { UDPSocket udpSocket; diff --git a/Jason/lab3/proto/message.pb.cc b/Jason/lab3/src/proto/message.pb.cc similarity index 52% rename from Jason/lab3/proto/message.pb.cc rename to Jason/lab3/src/proto/message.pb.cc index d43af16..3cf519e 100644 --- a/Jason/lab3/proto/message.pb.cc +++ b/Jason/lab3/src/proto/message.pb.cc @@ -1,17 +1,20 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! +// NO CHECKED-IN PROTOBUF GENCODE // source: message.proto +// Protobuf C++ Version: 5.28.0 #include "message.pb.h" #include +#include #include "google/protobuf/io/coded_stream.h" +#include "google/protobuf/generated_message_tctable_impl.h" #include "google/protobuf/extension_set.h" #include "google/protobuf/wire_format_lite.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/generated_message_reflection.h" #include "google/protobuf/reflection_ops.h" #include "google/protobuf/wire_format.h" -#include "google/protobuf/generated_message_tctable_impl.h" // @@protoc_insertion_point(includes) // Must be included last. @@ -31,7 +34,13 @@ inline constexpr SimpleMessage::Impl_::Impl_( template PROTOBUF_CONSTEXPR SimpleMessage::SimpleMessage(::_pbi::ConstantInitialized) - : _impl_(::_pbi::ConstantInitialized()) {} +#if defined(PROTOBUF_CUSTOM_VTABLE) + : ::google::protobuf::Message(_class_data_.base()), +#else // PROTOBUF_CUSTOM_VTABLE + : ::google::protobuf::Message(), +#endif // PROTOBUF_CUSTOM_VTABLE + _impl_(::_pbi::ConstantInitialized()) { +} struct SimpleMessageDefaultTypeInternal { PROTOBUF_CONSTEXPR SimpleMessageDefaultTypeInternal() : _instance(::_pbi::ConstantInitialized{}) {} ~SimpleMessageDefaultTypeInternal() {} @@ -43,38 +52,38 @@ struct SimpleMessageDefaultTypeInternal { PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 SimpleMessageDefaultTypeInternal _SimpleMessage_default_instance_; } // namespace example -static ::_pb::Metadata file_level_metadata_message_2eproto[1]; static constexpr const ::_pb::EnumDescriptor** file_level_enum_descriptors_message_2eproto = nullptr; static constexpr const ::_pb::ServiceDescriptor** file_level_service_descriptors_message_2eproto = nullptr; -const ::uint32_t TableStruct_message_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( - protodesc_cold) = { - ~0u, // no _has_bits_ - PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _internal_metadata_), - ~0u, // no _extensions_ - ~0u, // no _oneof_case_ - ~0u, // no _weak_field_map_ - ~0u, // no _inlined_string_donated_ - ~0u, // no _split_ - ~0u, // no sizeof(Split) - PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _impl_.text_), +const ::uint32_t + TableStruct_message_2eproto::offsets[] ABSL_ATTRIBUTE_SECTION_VARIABLE( + protodesc_cold) = { + ~0u, // no _has_bits_ + PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _internal_metadata_), + ~0u, // no _extensions_ + ~0u, // no _oneof_case_ + ~0u, // no _weak_field_map_ + ~0u, // no _inlined_string_donated_ + ~0u, // no _split_ + ~0u, // no sizeof(Split) + PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _impl_.text_), }; static const ::_pbi::MigrationSchema - schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + schemas[] ABSL_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { {0, -1, -1, sizeof(::example::SimpleMessage)}, }; - static const ::_pb::Message* const file_default_instances[] = { &::example::_SimpleMessage_default_instance_._instance, }; -const char descriptor_table_protodef_message_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { +const char descriptor_table_protodef_message_2eproto[] ABSL_ATTRIBUTE_SECTION_VARIABLE( + protodesc_cold) = { "\n\rmessage.proto\022\007example\"\035\n\rSimpleMessag" "e\022\014\n\004text\030\001 \001(\tb\006proto3" }; static ::absl::once_flag descriptor_table_message_2eproto_once; -const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { +PROTOBUF_CONSTINIT const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { false, false, 63, @@ -87,28 +96,9 @@ const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { schemas, file_default_instances, TableStruct_message_2eproto::offsets, - file_level_metadata_message_2eproto, file_level_enum_descriptors_message_2eproto, file_level_service_descriptors_message_2eproto, }; - -// This function exists to be marked as weak. -// It can significantly speed up compilation by breaking up LLVM's SCC -// in the .pb.cc translation units. Large translation units see a -// reduction of more than 35% of walltime for optimized builds. Without -// the weak attribute all the messages in the file, including all the -// vtables and everything they use become part of the same SCC through -// a cycle like: -// GetMetadata -> descriptor table -> default instances -> -// vtables -> GetMetadata -// By adding a weak function here we break the connection from the -// individual vtables back into the descriptor table. -PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_message_2eproto_getter() { - return &descriptor_table_message_2eproto; -} -// Force running AddDescriptors() at dynamic initialization time. -PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 -static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_message_2eproto(&descriptor_table_message_2eproto); namespace example { // =================================================================== @@ -117,25 +107,33 @@ class SimpleMessage::_Internal { }; SimpleMessage::SimpleMessage(::google::protobuf::Arena* arena) +#if defined(PROTOBUF_CUSTOM_VTABLE) + : ::google::protobuf::Message(arena, _class_data_.base()) { +#else // PROTOBUF_CUSTOM_VTABLE : ::google::protobuf::Message(arena) { +#endif // PROTOBUF_CUSTOM_VTABLE SharedCtor(arena); // @@protoc_insertion_point(arena_constructor:example.SimpleMessage) } inline PROTOBUF_NDEBUG_INLINE SimpleMessage::Impl_::Impl_( ::google::protobuf::internal::InternalVisibility visibility, ::google::protobuf::Arena* arena, - const Impl_& from) + const Impl_& from, const ::example::SimpleMessage& from_msg) : text_(arena, from.text_), _cached_size_{0} {} SimpleMessage::SimpleMessage( ::google::protobuf::Arena* arena, const SimpleMessage& from) +#if defined(PROTOBUF_CUSTOM_VTABLE) + : ::google::protobuf::Message(arena, _class_data_.base()) { +#else // PROTOBUF_CUSTOM_VTABLE : ::google::protobuf::Message(arena) { +#endif // PROTOBUF_CUSTOM_VTABLE SimpleMessage* const _this = this; (void)_this; _internal_metadata_.MergeFrom<::google::protobuf::UnknownFieldSet>( from._internal_metadata_); - new (&_impl_) Impl_(internal_visibility(), arena, from._impl_); + new (&_impl_) Impl_(internal_visibility(), arena, from._impl_, from); // @@protoc_insertion_point(copy_constructor:example.SimpleMessage) } @@ -159,24 +157,34 @@ inline void SimpleMessage::SharedDtor() { _impl_.~Impl_(); } -PROTOBUF_NOINLINE void SimpleMessage::Clear() { -// @@protoc_insertion_point(message_clear_start:example.SimpleMessage) - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); - ::uint32_t cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; - - _impl_.text_.ClearToEmpty(); - _internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>(); -} - -const char* SimpleMessage::_InternalParse( - const char* ptr, ::_pbi::ParseContext* ctx) { - ptr = ::_pbi::TcParser::ParseLoop(this, ptr, ctx, &_table_.header); - return ptr; +PROTOBUF_CONSTINIT +PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 +const ::google::protobuf::MessageLite::ClassDataFull + SimpleMessage::_class_data_ = { + ::google::protobuf::Message::ClassData{ + &_SimpleMessage_default_instance_._instance, + &_table_.header, + nullptr, // OnDemandRegisterArenaDtor + nullptr, // IsInitialized + &SimpleMessage::MergeImpl, +#if defined(PROTOBUF_CUSTOM_VTABLE) + ::google::protobuf::Message::GetDeleteImpl(), + ::google::protobuf::Message::GetNewImpl(), + ::google::protobuf::Message::GetClearImpl(), &SimpleMessage::ByteSizeLong, + &SimpleMessage::_InternalSerialize, +#endif // PROTOBUF_CUSTOM_VTABLE + PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_._cached_size_), + false, + }, + &SimpleMessage::kDescriptorMethods, + &descriptor_table_message_2eproto, + nullptr, // tracker +}; +const ::google::protobuf::MessageLite::ClassData* SimpleMessage::GetClassData() const { + ::google::protobuf::internal::PrefetchToLocalCache(&_class_data_); + ::google::protobuf::internal::PrefetchToLocalCache(_class_data_.tc_table); + return _class_data_.base(); } - - PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { { @@ -189,8 +197,12 @@ const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { 1, // num_field_entries 0, // num_aux_entries offsetof(decltype(_table_), field_names), // no aux_entries - &_SimpleMessage_default_instance_._instance, + _class_data_.base(), + nullptr, // post_loop_handler ::_pbi::TcParser::GenericFallback, // fallback + #ifdef PROTOBUF_PREFETCH_PARSE_TABLE + ::_pbi::TcParser::GetTable<::example::SimpleMessage>(), // to_prefetch + #endif // PROTOBUF_PREFETCH_PARSE_TABLE }, {{ // string text = 1; {::_pbi::TcParser::FastUS1, @@ -210,56 +222,75 @@ const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { }}, }; -::uint8_t* SimpleMessage::_InternalSerialize( - ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:example.SimpleMessage) - ::uint32_t cached_has_bits = 0; - (void)cached_has_bits; - - // string text = 1; - if (!this->_internal_text().empty()) { - const std::string& _s = this->_internal_text(); - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - _s.data(), static_cast(_s.length()), ::google::protobuf::internal::WireFormatLite::SERIALIZE, "example.SimpleMessage.text"); - target = stream->WriteStringMaybeAliased(1, _s, target); - } - - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = - ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( - _internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance), target, stream); - } - // @@protoc_insertion_point(serialize_to_array_end:example.SimpleMessage) - return target; -} - -::size_t SimpleMessage::ByteSizeLong() const { -// @@protoc_insertion_point(message_byte_size_start:example.SimpleMessage) - ::size_t total_size = 0; - +PROTOBUF_NOINLINE void SimpleMessage::Clear() { +// @@protoc_insertion_point(message_clear_start:example.SimpleMessage) + ::google::protobuf::internal::TSanWrite(&_impl_); ::uint32_t cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - // string text = 1; - if (!this->_internal_text().empty()) { - total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( - this->_internal_text()); - } - - return MaybeComputeUnknownFieldsSize(total_size, &_impl_._cached_size_); -} - -const ::google::protobuf::Message::ClassData SimpleMessage::_class_data_ = { - SimpleMessage::MergeImpl, - nullptr, // OnDemandRegisterArenaDtor -}; -const ::google::protobuf::Message::ClassData* SimpleMessage::GetClassData() const { - return &_class_data_; + _impl_.text_.ClearToEmpty(); + _internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>(); } -void SimpleMessage::MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg) { +#if defined(PROTOBUF_CUSTOM_VTABLE) + ::uint8_t* SimpleMessage::_InternalSerialize( + const MessageLite& base, ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) { + const SimpleMessage& this_ = static_cast(base); +#else // PROTOBUF_CUSTOM_VTABLE + ::uint8_t* SimpleMessage::_InternalSerialize( + ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) const { + const SimpleMessage& this_ = *this; +#endif // PROTOBUF_CUSTOM_VTABLE + // @@protoc_insertion_point(serialize_to_array_start:example.SimpleMessage) + ::uint32_t cached_has_bits = 0; + (void)cached_has_bits; + + // string text = 1; + if (!this_._internal_text().empty()) { + const std::string& _s = this_._internal_text(); + ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + _s.data(), static_cast(_s.length()), ::google::protobuf::internal::WireFormatLite::SERIALIZE, "example.SimpleMessage.text"); + target = stream->WriteStringMaybeAliased(1, _s, target); + } + + if (PROTOBUF_PREDICT_FALSE(this_._internal_metadata_.have_unknown_fields())) { + target = + ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( + this_._internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance), target, stream); + } + // @@protoc_insertion_point(serialize_to_array_end:example.SimpleMessage) + return target; + } + +#if defined(PROTOBUF_CUSTOM_VTABLE) + ::size_t SimpleMessage::ByteSizeLong(const MessageLite& base) { + const SimpleMessage& this_ = static_cast(base); +#else // PROTOBUF_CUSTOM_VTABLE + ::size_t SimpleMessage::ByteSizeLong() const { + const SimpleMessage& this_ = *this; +#endif // PROTOBUF_CUSTOM_VTABLE + // @@protoc_insertion_point(message_byte_size_start:example.SimpleMessage) + ::size_t total_size = 0; + + ::uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void)cached_has_bits; + + { + // string text = 1; + if (!this_._internal_text().empty()) { + total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( + this_._internal_text()); + } + } + return this_.MaybeComputeUnknownFieldsSize(total_size, + &this_._impl_._cached_size_); + } + +void SimpleMessage::MergeImpl(::google::protobuf::MessageLite& to_msg, const ::google::protobuf::MessageLite& from_msg) { auto* const _this = static_cast(&to_msg); auto& from = static_cast(from_msg); // @@protoc_insertion_point(class_specific_merge_from_start:example.SimpleMessage) @@ -280,13 +311,7 @@ void SimpleMessage::CopyFrom(const SimpleMessage& from) { MergeFrom(from); } -PROTOBUF_NOINLINE bool SimpleMessage::IsInitialized() const { - return true; -} -::_pbi::CachedSize* SimpleMessage::AccessCachedSize() const { - return &_impl_._cached_size_; -} void SimpleMessage::InternalSwap(SimpleMessage* PROTOBUF_RESTRICT other) { using std::swap; auto* arena = GetArena(); @@ -296,9 +321,7 @@ void SimpleMessage::InternalSwap(SimpleMessage* PROTOBUF_RESTRICT other) { } ::google::protobuf::Metadata SimpleMessage::GetMetadata() const { - return ::_pbi::AssignDescriptors( - &descriptor_table_message_2eproto_getter, &descriptor_table_message_2eproto_once, - file_level_metadata_message_2eproto[0]); + return ::google::protobuf::Message::GetMetadataImpl(GetClassData()->full()); } // @@protoc_insertion_point(namespace_scope) } // namespace example @@ -307,4 +330,8 @@ namespace protobuf { } // namespace protobuf } // namespace google // @@protoc_insertion_point(global_scope) +PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 static ::std::false_type + _static_init2_ PROTOBUF_UNUSED = + (::_pbi::AddDescriptors(&descriptor_table_message_2eproto), + ::std::false_type{}); #include "google/protobuf/port_undef.inc" diff --git a/Jason/lab3/proto/message.pb.h b/Jason/lab3/src/proto/message.pb.h similarity index 69% rename from Jason/lab3/proto/message.pb.h rename to Jason/lab3/src/proto/message.pb.h index 7a48d5e..02ee48e 100644 --- a/Jason/lab3/proto/message.pb.h +++ b/Jason/lab3/src/proto/message.pb.h @@ -1,6 +1,7 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! +// NO CHECKED-IN PROTOBUF GENCODE // source: message.proto -// Protobuf C++ Version: 4.25.4 +// Protobuf C++ Version: 5.28.0 #ifndef GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh #define GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh @@ -10,19 +11,12 @@ #include #include -#include "google/protobuf/port_def.inc" -#if PROTOBUF_VERSION < 4025000 -#error "This file was generated by a newer version of protoc which is" -#error "incompatible with your Protocol Buffer headers. Please update" -#error "your headers." -#endif // PROTOBUF_VERSION - -#if 4025004 < PROTOBUF_MIN_PROTOC_VERSION -#error "This file was generated by an older version of protoc which is" -#error "incompatible with your Protocol Buffer headers. Please" -#error "regenerate this file with a newer version of protoc." -#endif // PROTOBUF_MIN_PROTOC_VERSION -#include "google/protobuf/port_undef.inc" +#include "google/protobuf/runtime_version.h" +#if PROTOBUF_VERSION != 5028000 +#error "Protobuf C++ gencode is built with an incompatible version of" +#error "Protobuf C++ headers/runtime. See" +#error "https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp" +#endif #include "google/protobuf/io/coded_stream.h" #include "google/protobuf/arena.h" #include "google/protobuf/arenastring.h" @@ -72,21 +66,18 @@ namespace example { // ------------------------------------------------------------------- -class SimpleMessage final : - public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:example.SimpleMessage) */ { +class SimpleMessage final : public ::google::protobuf::Message +/* @@protoc_insertion_point(class_definition:example.SimpleMessage) */ { public: inline SimpleMessage() : SimpleMessage(nullptr) {} - ~SimpleMessage() override; - template - explicit PROTOBUF_CONSTEXPR SimpleMessage(::google::protobuf::internal::ConstantInitialized); - - inline SimpleMessage(const SimpleMessage& from) - : SimpleMessage(nullptr, from) {} - SimpleMessage(SimpleMessage&& from) noexcept - : SimpleMessage() { - *this = ::std::move(from); - } - + ~SimpleMessage() PROTOBUF_FINAL; + template + explicit PROTOBUF_CONSTEXPR SimpleMessage( + ::google::protobuf::internal::ConstantInitialized); + + inline SimpleMessage(const SimpleMessage& from) : SimpleMessage(nullptr, from) {} + inline SimpleMessage(SimpleMessage&& from) noexcept + : SimpleMessage(nullptr, std::move(from)) {} inline SimpleMessage& operator=(const SimpleMessage& from) { CopyFrom(from); return *this; @@ -94,9 +85,9 @@ class SimpleMessage final : inline SimpleMessage& operator=(SimpleMessage&& from) noexcept { if (this == &from) return *this; if (GetArena() == from.GetArena() - #ifdef PROTOBUF_FORCE_COPY_IN_MOVE +#ifdef PROTOBUF_FORCE_COPY_IN_MOVE && GetArena() != nullptr - #endif // !PROTOBUF_FORCE_COPY_IN_MOVE +#endif // !PROTOBUF_FORCE_COPY_IN_MOVE ) { InternalSwap(&from); } else { @@ -128,22 +119,17 @@ class SimpleMessage final : } static inline const SimpleMessage* internal_default_instance() { return reinterpret_cast( - &_SimpleMessage_default_instance_); - } - static constexpr int kIndexInFileMessages = - 0; - - friend void swap(SimpleMessage& a, SimpleMessage& b) { - a.Swap(&b); + &_SimpleMessage_default_instance_); } + static constexpr int kIndexInFileMessages = 0; + friend void swap(SimpleMessage& a, SimpleMessage& b) { a.Swap(&b); } inline void Swap(SimpleMessage* other) { if (other == this) return; - #ifdef PROTOBUF_FORCE_COPY_IN_SWAP - if (GetArena() != nullptr && - GetArena() == other->GetArena()) { - #else // PROTOBUF_FORCE_COPY_IN_SWAP +#ifdef PROTOBUF_FORCE_COPY_IN_SWAP + if (GetArena() != nullptr && GetArena() == other->GetArena()) { +#else // PROTOBUF_FORCE_COPY_IN_SWAP if (GetArena() == other->GetArena()) { - #endif // !PROTOBUF_FORCE_COPY_IN_SWAP +#endif // !PROTOBUF_FORCE_COPY_IN_SWAP InternalSwap(other); } else { ::google::protobuf::internal::GenericSwap(this, other); @@ -157,52 +143,69 @@ class SimpleMessage final : // implements Message ---------------------------------------------- - SimpleMessage* New(::google::protobuf::Arena* arena = nullptr) const final { - return CreateMaybeMessage(arena); + SimpleMessage* New(::google::protobuf::Arena* arena = nullptr) const PROTOBUF_FINAL { + return ::google::protobuf::Message::DefaultConstruct(arena); } using ::google::protobuf::Message::CopyFrom; void CopyFrom(const SimpleMessage& from); using ::google::protobuf::Message::MergeFrom; - void MergeFrom( const SimpleMessage& from) { - SimpleMessage::MergeImpl(*this, from); - } + void MergeFrom(const SimpleMessage& from) { SimpleMessage::MergeImpl(*this, from); } + private: - static void MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg); + static void MergeImpl( + ::google::protobuf::MessageLite& to_msg, + const ::google::protobuf::MessageLite& from_msg); + public: - PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; - bool IsInitialized() const final; + bool IsInitialized() const { + return true; + } + ABSL_ATTRIBUTE_REINITIALIZES void Clear() PROTOBUF_FINAL; + #if defined(PROTOBUF_CUSTOM_VTABLE) + private: + static ::size_t ByteSizeLong(const ::google::protobuf::MessageLite& msg); + static ::uint8_t* _InternalSerialize( + const MessageLite& msg, ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream); + public: + ::size_t ByteSizeLong() const { return ByteSizeLong(*this); } + ::uint8_t* _InternalSerialize( + ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) const { + return _InternalSerialize(*this, target, stream); + } + #else // PROTOBUF_CUSTOM_VTABLE ::size_t ByteSizeLong() const final; - const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( - ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; + ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) const final; + #endif // PROTOBUF_CUSTOM_VTABLE int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: - ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); void InternalSwap(SimpleMessage* other); - - private: + private: friend class ::google::protobuf::internal::AnyMetadata; - static ::absl::string_view FullMessageName() { - return "example.SimpleMessage"; - } - protected: + static ::absl::string_view FullMessageName() { return "example.SimpleMessage"; } + + protected: explicit SimpleMessage(::google::protobuf::Arena* arena); SimpleMessage(::google::protobuf::Arena* arena, const SimpleMessage& from); - public: - - static const ClassData _class_data_; - const ::google::protobuf::Message::ClassData*GetClassData() const final; - - ::google::protobuf::Metadata GetMetadata() const final; + SimpleMessage(::google::protobuf::Arena* arena, SimpleMessage&& from) noexcept + : SimpleMessage(arena) { + *this = ::std::move(from); + } + const ::google::protobuf::Message::ClassData* GetClassData() const PROTOBUF_FINAL; + static const ::google::protobuf::Message::ClassDataFull _class_data_; + public: + ::google::protobuf::Metadata GetMetadata() const; // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- - enum : int { kTextFieldNumber = 1, }; @@ -225,12 +228,15 @@ class SimpleMessage final : // @@protoc_insertion_point(class_scope:example.SimpleMessage) private: class _Internal; - friend class ::google::protobuf::internal::TcParser; static const ::google::protobuf::internal::TcParseTable< 0, 1, 0, 34, 2> _table_; + + static constexpr const void* _raw_default_instance_ = + &_SimpleMessage_default_instance_; + friend class ::google::protobuf::MessageLite; friend class ::google::protobuf::Arena; template @@ -238,13 +244,13 @@ class SimpleMessage final : using InternalArenaConstructable_ = void; using DestructorSkippable_ = void; struct Impl_ { - - inline explicit constexpr Impl_( - ::google::protobuf::internal::ConstantInitialized) noexcept; - inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, - ::google::protobuf::Arena* arena); - inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, - ::google::protobuf::Arena* arena, const Impl_& from); + inline explicit constexpr Impl_( + ::google::protobuf::internal::ConstantInitialized) noexcept; + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena); + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena, const Impl_& from, + const SimpleMessage& from_msg); ::google::protobuf::internal::ArenaStringPtr text_; mutable ::google::protobuf::internal::CachedSize _cached_size_; PROTOBUF_TSAN_DECLARE_MEMBER @@ -271,7 +277,7 @@ class SimpleMessage final : // string text = 1; inline void SimpleMessage::clear_text() { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ::google::protobuf::internal::TSanWrite(&_impl_); _impl_.text_.ClearToEmpty(); } inline const std::string& SimpleMessage::text() const @@ -282,8 +288,7 @@ inline const std::string& SimpleMessage::text() const template inline PROTOBUF_ALWAYS_INLINE void SimpleMessage::set_text(Arg_&& arg, Args_... args) { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); - ; + ::google::protobuf::internal::TSanWrite(&_impl_); _impl_.text_.Set(static_cast(arg), args..., GetArena()); // @@protoc_insertion_point(field_set:example.SimpleMessage.text) } @@ -293,26 +298,24 @@ inline std::string* SimpleMessage::mutable_text() ABSL_ATTRIBUTE_LIFETIME_BOUND return _s; } inline const std::string& SimpleMessage::_internal_text() const { - PROTOBUF_TSAN_READ(&_impl_._tsan_detect_race); + ::google::protobuf::internal::TSanRead(&_impl_); return _impl_.text_.Get(); } inline void SimpleMessage::_internal_set_text(const std::string& value) { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); - ; + ::google::protobuf::internal::TSanWrite(&_impl_); _impl_.text_.Set(value, GetArena()); } inline std::string* SimpleMessage::_internal_mutable_text() { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); - ; + ::google::protobuf::internal::TSanWrite(&_impl_); return _impl_.text_.Mutable( GetArena()); } inline std::string* SimpleMessage::release_text() { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ::google::protobuf::internal::TSanWrite(&_impl_); // @@protoc_insertion_point(field_release:example.SimpleMessage.text) return _impl_.text_.Release(); } inline void SimpleMessage::set_allocated_text(std::string* value) { - PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ::google::protobuf::internal::TSanWrite(&_impl_); _impl_.text_.SetAllocated(value, GetArena()); #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING if (_impl_.text_.IsDefault()) { diff --git a/Jason/lab3/proto/message.proto b/Jason/lab3/src/proto/message.proto similarity index 100% rename from Jason/lab3/proto/message.proto rename to Jason/lab3/src/proto/message.proto diff --git a/Jason/lab3/src/server.cpp b/Jason/lab3/src/server.cpp index 7bcd19b..cc1ed2b 100644 --- a/Jason/lab3/src/server.cpp +++ b/Jason/lab3/src/server.cpp @@ -1,5 +1,7 @@ #include "UDPSocket.hpp" #include +#include +#include "proto/message.pb.h" int main() { UDPSocket udpSocket; From 0d6b4d564f32b711acf70e2c5cc02aa0697eb987 Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 21:54:44 -0400 Subject: [PATCH 4/8] did stuff --- Jason/lab3/CMakeLists.txt | 20 +- Jason/lab3/app/client_driver.cpp | 42 +++ Jason/lab3/app/server_driver.cpp | 36 +++ Jason/lab3/{src => include}/UDPSocket.hpp | 0 Jason/lab3/include/client.hpp | 25 ++ Jason/lab3/include/server.hpp | 26 ++ Jason/lab3/result | 1 + Jason/lab3/src/client.cpp | 84 ++++-- Jason/lab3/src/proto/message.pb.cc | 349 ++++++++++------------ Jason/lab3/src/proto/message.pb.h | 287 +++++++++--------- Jason/lab3/src/proto/message.proto | 6 +- Jason/lab3/src/server.cpp | 93 ++++-- 12 files changed, 584 insertions(+), 385 deletions(-) create mode 100644 Jason/lab3/app/client_driver.cpp create mode 100644 Jason/lab3/app/server_driver.cpp rename Jason/lab3/{src => include}/UDPSocket.hpp (100%) create mode 100644 Jason/lab3/include/client.hpp create mode 100644 Jason/lab3/include/server.hpp create mode 120000 Jason/lab3/result diff --git a/Jason/lab3/CMakeLists.txt b/Jason/lab3/CMakeLists.txt index 7ecb585..d60ee1f 100644 --- a/Jason/lab3/CMakeLists.txt +++ b/Jason/lab3/CMakeLists.txt @@ -1,22 +1,26 @@ cmake_minimum_required(VERSION 3.10) project(Lab3) + find_package(Protobuf REQUIRED) -# Define the library +# Define the libraries add_library(msg STATIC src/proto/message.pb.cc) +add_library(server STATIC src/server.cpp) +add_library(client STATIC src/client.cpp) #Link the necessary targets target_link_libraries(msg PUBLIC protobuf::libprotobuf) +target_link_libraries(client PUBLIC msg) +target_link_libraries(server PUBLIC msg) # Add the server executable -add_executable(server src/server.cpp) -target_link_libraries(server PUBLIC msg) -target_link_libraries(server PUBLIC src/UDPSocket.cpp) +add_executable(server_exe app/server_driver.cpp) +target_link_libraries(server_exe PUBLIC server) + # Add the client executable -add_executable(client src/client.cpp ) -target_link_libraries(client PUBLIC msg) -target_link_libraries(client PUBLIC src/UDPSocket.cpp) +add_executable(client_exe app/client_driver.cpp) +target_link_libraries(client_exe PUBLIC client) -install (TARGETS server client DESTINATION ${CMAKE_INSTALL_BINDIR}) \ No newline at end of file +install (TARGETS server_exe client_exe DESTINATION ${CMAKE_INSTALL_BINDIR}) \ No newline at end of file diff --git a/Jason/lab3/app/client_driver.cpp b/Jason/lab3/app/client_driver.cpp new file mode 100644 index 0000000..4753c99 --- /dev/null +++ b/Jason/lab3/app/client_driver.cpp @@ -0,0 +1,42 @@ +#include "client.hpp" +#include "server.hpp" +#include + +/** + * @brief Executable that initiates Client + * + * This files boots up the Client + */ +int main() { + // Initialize the client with server IP and port + Client client("127.0.0.1", 8080); // Replace with the server's IP address AKA the car + + // Create and send a message with the input '1' + message::data message; + message.set_sender_name("Msg From Client"); + + std::string input; + std::cout << "Press Enter to send a message: "; + std::getline(std::cin, input); + + if (input.empty()) { + // Send the message + if (client.SendMessage(message)) { + std::cout << "Message sent successfully" << std::endl; + } else { + std::cerr << "Failed to send message" << std::endl; + } + + // Receive and print the response! + message::data responseMessage; + if (client.ReceiveMessage(responseMessage)) { + ; + } else { + std::cerr << "Failed to receive response" << std::endl; + } + } else { + std::cout << "No message sent." << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/Jason/lab3/app/server_driver.cpp b/Jason/lab3/app/server_driver.cpp new file mode 100644 index 0000000..04ce02d --- /dev/null +++ b/Jason/lab3/app/server_driver.cpp @@ -0,0 +1,36 @@ +#include "client.hpp" // Update include path +#include "server.hpp" +#include + +/** + * @brief Executable that initiates Server + * + * This files boots up the Server + */ +int main() { + // Initiate Server + Server server("0.0.0.0", 8080); + + std::cout << "Server is running and listening on port 8080..." << std::endl; + // Be on the constant lookout for messages + while (true) { + message::data receivedMessage; + + // Receive message from client + if (server.ReceiveMessage(receivedMessage)) { + // Prepare and send a response to client + message::data responseMessage; + responseMessage.set_sender_name("Message from Server"); + // Send message + if (server.SendMessage(responseMessage)) { + std::cout << "Response sent successfully" << std::endl; + } else { + std::cerr << "Failed to send response" << std::endl; + } + } else { + std::cerr << "Failed to receive message" << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/Jason/lab3/src/UDPSocket.hpp b/Jason/lab3/include/UDPSocket.hpp similarity index 100% rename from Jason/lab3/src/UDPSocket.hpp rename to Jason/lab3/include/UDPSocket.hpp diff --git a/Jason/lab3/include/client.hpp b/Jason/lab3/include/client.hpp new file mode 100644 index 0000000..25f237d --- /dev/null +++ b/Jason/lab3/include/client.hpp @@ -0,0 +1,25 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include // For sockaddr_in +#include // For socket functions +#include // For close + +#include "src/proto/message.pb.h" + +class Client { +public: + Client(const std::string& server_ip, uint16_t server_port); + ~Client(); + + bool SendMessage(const message::data& message); + bool ReceiveMessage(message::data& message); + +private: + int _sockfd; + sockaddr_in _server_addr; + static const int _BUFFER_SIZE = 1024; +}; + +#endif // CLIENT_H \ No newline at end of file diff --git a/Jason/lab3/include/server.hpp b/Jason/lab3/include/server.hpp new file mode 100644 index 0000000..d531fc0 --- /dev/null +++ b/Jason/lab3/include/server.hpp @@ -0,0 +1,26 @@ +#ifndef SERVER_H +#define SERVER_H + +#include +#include // For sockaddr_in +#include // For socket functions +#include // For close + +#include "src/proto/message.pb.h" + +class Server { +public: + Server(const std::string& server_ip, uint16_t server_port); + ~Server(); + + bool SendMessage(const message::data& message); + bool ReceiveMessage(message::data& message); + +private: + int _sock; + sockaddr_in _server_addr; + sockaddr_in _client_addr; + static const int _BUFFER_SIZE = 1024; +}; + +#endif // SERVER_H \ No newline at end of file diff --git a/Jason/lab3/result b/Jason/lab3/result new file mode 120000 index 0000000..8c6ee39 --- /dev/null +++ b/Jason/lab3/result @@ -0,0 +1 @@ +/nix/store/aik936ldfgrx0g16g441wfqpv6bljydz-lab3-0.1.0 \ No newline at end of file diff --git a/Jason/lab3/src/client.cpp b/Jason/lab3/src/client.cpp index 5de1e2d..28a8039 100644 --- a/Jason/lab3/src/client.cpp +++ b/Jason/lab3/src/client.cpp @@ -1,29 +1,73 @@ -#include "UDPSocket.hpp" +#include "client.hpp" #include -#include +#include // For memset + #include "proto/message.pb.h" -int main() { - UDPSocket udpSocket; +/** + * @brief Class Client + * + * Set up socket + */ +Client::Client(const std::string& server_ip, uint16_t server_port) { + // Create socket + _sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (_sockfd < 0) { + std::cerr << "Failed to create socket" << std::endl; + exit(1); + } - // Define server IP and port - std::string serverIp = "127.0.0.1"; - int serverPort = 8080; + // Setup server address + memset(&_server_addr, 0, sizeof(_server_addr)); + _server_addr.sin_family = AF_INET; + _server_addr.sin_port = htons(server_port); + inet_pton(AF_INET, server_ip.c_str(), &_server_addr.sin_addr); +} + +Client::~Client() { + close(_sockfd); +} +/** + * @brief Class Client + * + * sends message to the server + */ +bool Client::SendMessage(const message::data& message) { + std::string serialized_message; + if (!message.SerializeToString(&serialized_message)) { + std::cerr << "Failed to serialize message" << std::endl; + return false; + } - // Send the message to the server - udpSocket.sendTo("Test Message", serverIp, serverPort); - std::cout << "Sent message to server: \n"; + if (sendto(_sockfd, serialized_message.data(), serialized_message.size(), 0, + (struct sockaddr*)&_server_addr, sizeof(_server_addr)) < 0) { + std::cerr << "Failed to send message" << std::endl; + return false; + } + return true; +} +/** + * @brief Class Client + * + * sends response to the server + */ +bool Client::ReceiveMessage(message::data& message) { + char buffer[_BUFFER_SIZE]; + sockaddr_in from_addr; + socklen_t from_addr_len = sizeof(from_addr); - // Receive the response from the server - std::string responseMessage, serverIpReceived; - int serverPortReceived; - ssize_t bytesReceived = udpSocket.receiveFrom(responseMessage, serverIpReceived, serverPortReceived); + ssize_t received_bytes = recvfrom(_sockfd, buffer, _BUFFER_SIZE, 0, + (struct sockaddr*)&from_addr, &from_addr_len); + if (received_bytes < 0) { + std::cerr << "Failed to receive message" << std::endl; + return false; + } - if (bytesReceived > 0) { - std::cout << "Received message from " << serverIp << ":" << serverPort << ": " << responseMessage << "\n"; - } else { - std::cerr << "Error receiving message from server\n"; + if (!message.ParseFromArray(buffer, received_bytes)) { + std::cerr << "Failed to parse message" << std::endl; + return false; } - return 0; -} + std::cout << "Received message from " << message.sender_name() << std::endl; + return true; +} \ No newline at end of file diff --git a/Jason/lab3/src/proto/message.pb.cc b/Jason/lab3/src/proto/message.pb.cc index 3cf519e..98cfd43 100644 --- a/Jason/lab3/src/proto/message.pb.cc +++ b/Jason/lab3/src/proto/message.pb.cc @@ -1,20 +1,17 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! -// NO CHECKED-IN PROTOBUF GENCODE // source: message.proto -// Protobuf C++ Version: 5.28.0 #include "message.pb.h" #include -#include #include "google/protobuf/io/coded_stream.h" -#include "google/protobuf/generated_message_tctable_impl.h" #include "google/protobuf/extension_set.h" #include "google/protobuf/wire_format_lite.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/generated_message_reflection.h" #include "google/protobuf/reflection_ops.h" #include "google/protobuf/wire_format.h" +#include "google/protobuf/generated_message_tctable_impl.h" // @@protoc_insertion_point(includes) // Must be included last. @@ -23,70 +20,64 @@ PROTOBUF_PRAGMA_INIT_SEG namespace _pb = ::google::protobuf; namespace _pbi = ::google::protobuf::internal; namespace _fl = ::google::protobuf::internal::field_layout; -namespace example { +namespace message { -inline constexpr SimpleMessage::Impl_::Impl_( +inline constexpr data::Impl_::Impl_( ::_pbi::ConstantInitialized) noexcept - : text_( + : sender_name_( &::google::protobuf::internal::fixed_address_empty_string, ::_pbi::ConstantInitialized()), _cached_size_{0} {} template -PROTOBUF_CONSTEXPR SimpleMessage::SimpleMessage(::_pbi::ConstantInitialized) -#if defined(PROTOBUF_CUSTOM_VTABLE) - : ::google::protobuf::Message(_class_data_.base()), -#else // PROTOBUF_CUSTOM_VTABLE - : ::google::protobuf::Message(), -#endif // PROTOBUF_CUSTOM_VTABLE - _impl_(::_pbi::ConstantInitialized()) { -} -struct SimpleMessageDefaultTypeInternal { - PROTOBUF_CONSTEXPR SimpleMessageDefaultTypeInternal() : _instance(::_pbi::ConstantInitialized{}) {} - ~SimpleMessageDefaultTypeInternal() {} +PROTOBUF_CONSTEXPR data::data(::_pbi::ConstantInitialized) + : _impl_(::_pbi::ConstantInitialized()) {} +struct dataDefaultTypeInternal { + PROTOBUF_CONSTEXPR dataDefaultTypeInternal() : _instance(::_pbi::ConstantInitialized{}) {} + ~dataDefaultTypeInternal() {} union { - SimpleMessage _instance; + data _instance; }; }; PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT - PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 SimpleMessageDefaultTypeInternal _SimpleMessage_default_instance_; -} // namespace example + PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 dataDefaultTypeInternal _data_default_instance_; +} // namespace message +static ::_pb::Metadata file_level_metadata_message_2eproto[1]; static constexpr const ::_pb::EnumDescriptor** file_level_enum_descriptors_message_2eproto = nullptr; static constexpr const ::_pb::ServiceDescriptor** file_level_service_descriptors_message_2eproto = nullptr; -const ::uint32_t - TableStruct_message_2eproto::offsets[] ABSL_ATTRIBUTE_SECTION_VARIABLE( - protodesc_cold) = { - ~0u, // no _has_bits_ - PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _internal_metadata_), - ~0u, // no _extensions_ - ~0u, // no _oneof_case_ - ~0u, // no _weak_field_map_ - ~0u, // no _inlined_string_donated_ - ~0u, // no _split_ - ~0u, // no sizeof(Split) - PROTOBUF_FIELD_OFFSET(::example::SimpleMessage, _impl_.text_), +const ::uint32_t TableStruct_message_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE( + protodesc_cold) = { + ~0u, // no _has_bits_ + PROTOBUF_FIELD_OFFSET(::message::data, _internal_metadata_), + ~0u, // no _extensions_ + ~0u, // no _oneof_case_ + ~0u, // no _weak_field_map_ + ~0u, // no _inlined_string_donated_ + ~0u, // no _split_ + ~0u, // no sizeof(Split) + PROTOBUF_FIELD_OFFSET(::message::data, _impl_.sender_name_), }; static const ::_pbi::MigrationSchema - schemas[] ABSL_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { - {0, -1, -1, sizeof(::example::SimpleMessage)}, + schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + {0, -1, -1, sizeof(::message::data)}, }; + static const ::_pb::Message* const file_default_instances[] = { - &::example::_SimpleMessage_default_instance_._instance, + &::message::_data_default_instance_._instance, }; -const char descriptor_table_protodef_message_2eproto[] ABSL_ATTRIBUTE_SECTION_VARIABLE( - protodesc_cold) = { - "\n\rmessage.proto\022\007example\"\035\n\rSimpleMessag" - "e\022\014\n\004text\030\001 \001(\tb\006proto3" +const char descriptor_table_protodef_message_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = { + "\n\rmessage.proto\022\007message\"\033\n\004data\022\023\n\013send" + "er_name\030\001 \001(\tb\006proto3" }; static ::absl::once_flag descriptor_table_message_2eproto_once; -PROTOBUF_CONSTINIT const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { +const ::_pbi::DescriptorTable descriptor_table_message_2eproto = { false, false, - 63, + 61, descriptor_table_protodef_message_2eproto, "message.proto", &descriptor_table_message_2eproto_once, @@ -96,97 +87,98 @@ PROTOBUF_CONSTINIT const ::_pbi::DescriptorTable descriptor_table_message_2eprot schemas, file_default_instances, TableStruct_message_2eproto::offsets, + file_level_metadata_message_2eproto, file_level_enum_descriptors_message_2eproto, file_level_service_descriptors_message_2eproto, }; -namespace example { + +// This function exists to be marked as weak. +// It can significantly speed up compilation by breaking up LLVM's SCC +// in the .pb.cc translation units. Large translation units see a +// reduction of more than 35% of walltime for optimized builds. Without +// the weak attribute all the messages in the file, including all the +// vtables and everything they use become part of the same SCC through +// a cycle like: +// GetMetadata -> descriptor table -> default instances -> +// vtables -> GetMetadata +// By adding a weak function here we break the connection from the +// individual vtables back into the descriptor table. +PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_message_2eproto_getter() { + return &descriptor_table_message_2eproto; +} +// Force running AddDescriptors() at dynamic initialization time. +PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 +static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_message_2eproto(&descriptor_table_message_2eproto); +namespace message { // =================================================================== -class SimpleMessage::_Internal { +class data::_Internal { public: }; -SimpleMessage::SimpleMessage(::google::protobuf::Arena* arena) -#if defined(PROTOBUF_CUSTOM_VTABLE) - : ::google::protobuf::Message(arena, _class_data_.base()) { -#else // PROTOBUF_CUSTOM_VTABLE +data::data(::google::protobuf::Arena* arena) : ::google::protobuf::Message(arena) { -#endif // PROTOBUF_CUSTOM_VTABLE SharedCtor(arena); - // @@protoc_insertion_point(arena_constructor:example.SimpleMessage) + // @@protoc_insertion_point(arena_constructor:message.data) } -inline PROTOBUF_NDEBUG_INLINE SimpleMessage::Impl_::Impl_( +inline PROTOBUF_NDEBUG_INLINE data::Impl_::Impl_( ::google::protobuf::internal::InternalVisibility visibility, ::google::protobuf::Arena* arena, - const Impl_& from, const ::example::SimpleMessage& from_msg) - : text_(arena, from.text_), + const Impl_& from) + : sender_name_(arena, from.sender_name_), _cached_size_{0} {} -SimpleMessage::SimpleMessage( +data::data( ::google::protobuf::Arena* arena, - const SimpleMessage& from) -#if defined(PROTOBUF_CUSTOM_VTABLE) - : ::google::protobuf::Message(arena, _class_data_.base()) { -#else // PROTOBUF_CUSTOM_VTABLE + const data& from) : ::google::protobuf::Message(arena) { -#endif // PROTOBUF_CUSTOM_VTABLE - SimpleMessage* const _this = this; + data* const _this = this; (void)_this; _internal_metadata_.MergeFrom<::google::protobuf::UnknownFieldSet>( from._internal_metadata_); - new (&_impl_) Impl_(internal_visibility(), arena, from._impl_, from); + new (&_impl_) Impl_(internal_visibility(), arena, from._impl_); - // @@protoc_insertion_point(copy_constructor:example.SimpleMessage) + // @@protoc_insertion_point(copy_constructor:message.data) } -inline PROTOBUF_NDEBUG_INLINE SimpleMessage::Impl_::Impl_( +inline PROTOBUF_NDEBUG_INLINE data::Impl_::Impl_( ::google::protobuf::internal::InternalVisibility visibility, ::google::protobuf::Arena* arena) - : text_(arena), + : sender_name_(arena), _cached_size_{0} {} -inline void SimpleMessage::SharedCtor(::_pb::Arena* arena) { +inline void data::SharedCtor(::_pb::Arena* arena) { new (&_impl_) Impl_(internal_visibility(), arena); } -SimpleMessage::~SimpleMessage() { - // @@protoc_insertion_point(destructor:example.SimpleMessage) +data::~data() { + // @@protoc_insertion_point(destructor:message.data) _internal_metadata_.Delete<::google::protobuf::UnknownFieldSet>(); SharedDtor(); } -inline void SimpleMessage::SharedDtor() { +inline void data::SharedDtor() { ABSL_DCHECK(GetArena() == nullptr); - _impl_.text_.Destroy(); + _impl_.sender_name_.Destroy(); _impl_.~Impl_(); } -PROTOBUF_CONSTINIT -PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 -const ::google::protobuf::MessageLite::ClassDataFull - SimpleMessage::_class_data_ = { - ::google::protobuf::Message::ClassData{ - &_SimpleMessage_default_instance_._instance, - &_table_.header, - nullptr, // OnDemandRegisterArenaDtor - nullptr, // IsInitialized - &SimpleMessage::MergeImpl, -#if defined(PROTOBUF_CUSTOM_VTABLE) - ::google::protobuf::Message::GetDeleteImpl(), - ::google::protobuf::Message::GetNewImpl(), - ::google::protobuf::Message::GetClearImpl(), &SimpleMessage::ByteSizeLong, - &SimpleMessage::_InternalSerialize, -#endif // PROTOBUF_CUSTOM_VTABLE - PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_._cached_size_), - false, - }, - &SimpleMessage::kDescriptorMethods, - &descriptor_table_message_2eproto, - nullptr, // tracker -}; -const ::google::protobuf::MessageLite::ClassData* SimpleMessage::GetClassData() const { - ::google::protobuf::internal::PrefetchToLocalCache(&_class_data_); - ::google::protobuf::internal::PrefetchToLocalCache(_class_data_.tc_table); - return _class_data_.base(); +PROTOBUF_NOINLINE void data::Clear() { +// @@protoc_insertion_point(message_clear_start:message.data) + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ::uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + + _impl_.sender_name_.ClearToEmpty(); + _internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>(); +} + +const char* data::_InternalParse( + const char* ptr, ::_pbi::ParseContext* ctx) { + ptr = ::_pbi::TcParser::ParseLoop(this, ptr, ctx, &_table_.header); + return ptr; } + + PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 -const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { +const ::_pbi::TcParseTable<0, 1, 0, 32, 2> data::_table_ = { { 0, // no _has_bits_ 0, // no _extensions_ @@ -197,141 +189,122 @@ const ::_pbi::TcParseTable<0, 1, 0, 34, 2> SimpleMessage::_table_ = { 1, // num_field_entries 0, // num_aux_entries offsetof(decltype(_table_), field_names), // no aux_entries - _class_data_.base(), - nullptr, // post_loop_handler + &_data_default_instance_._instance, ::_pbi::TcParser::GenericFallback, // fallback - #ifdef PROTOBUF_PREFETCH_PARSE_TABLE - ::_pbi::TcParser::GetTable<::example::SimpleMessage>(), // to_prefetch - #endif // PROTOBUF_PREFETCH_PARSE_TABLE }, {{ - // string text = 1; + // string sender_name = 1; {::_pbi::TcParser::FastUS1, - {10, 63, 0, PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_.text_)}}, + {10, 63, 0, PROTOBUF_FIELD_OFFSET(data, _impl_.sender_name_)}}, }}, {{ 65535, 65535 }}, {{ - // string text = 1; - {PROTOBUF_FIELD_OFFSET(SimpleMessage, _impl_.text_), 0, 0, + // string sender_name = 1; + {PROTOBUF_FIELD_OFFSET(data, _impl_.sender_name_), 0, 0, (0 | ::_fl::kFcSingular | ::_fl::kUtf8String | ::_fl::kRepAString)}, }}, // no aux_entries {{ - "\25\4\0\0\0\0\0\0" - "example.SimpleMessage" - "text" + "\14\13\0\0\0\0\0\0" + "message.data" + "sender_name" }}, }; -PROTOBUF_NOINLINE void SimpleMessage::Clear() { -// @@protoc_insertion_point(message_clear_start:example.SimpleMessage) - ::google::protobuf::internal::TSanWrite(&_impl_); +::uint8_t* data::_InternalSerialize( + ::uint8_t* target, + ::google::protobuf::io::EpsCopyOutputStream* stream) const { + // @@protoc_insertion_point(serialize_to_array_start:message.data) + ::uint32_t cached_has_bits = 0; + (void)cached_has_bits; + + // string sender_name = 1; + if (!this->_internal_sender_name().empty()) { + const std::string& _s = this->_internal_sender_name(); + ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( + _s.data(), static_cast(_s.length()), ::google::protobuf::internal::WireFormatLite::SERIALIZE, "message.data.sender_name"); + target = stream->WriteStringMaybeAliased(1, _s, target); + } + + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = + ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( + _internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance), target, stream); + } + // @@protoc_insertion_point(serialize_to_array_end:message.data) + return target; +} + +::size_t data::ByteSizeLong() const { +// @@protoc_insertion_point(message_byte_size_start:message.data) + ::size_t total_size = 0; + ::uint32_t cached_has_bits = 0; // Prevent compiler warnings about cached_has_bits being unused (void) cached_has_bits; - _impl_.text_.ClearToEmpty(); - _internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>(); + // string sender_name = 1; + if (!this->_internal_sender_name().empty()) { + total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( + this->_internal_sender_name()); + } + + return MaybeComputeUnknownFieldsSize(total_size, &_impl_._cached_size_); +} + +const ::google::protobuf::Message::ClassData data::_class_data_ = { + data::MergeImpl, + nullptr, // OnDemandRegisterArenaDtor +}; +const ::google::protobuf::Message::ClassData* data::GetClassData() const { + return &_class_data_; } -#if defined(PROTOBUF_CUSTOM_VTABLE) - ::uint8_t* SimpleMessage::_InternalSerialize( - const MessageLite& base, ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream) { - const SimpleMessage& this_ = static_cast(base); -#else // PROTOBUF_CUSTOM_VTABLE - ::uint8_t* SimpleMessage::_InternalSerialize( - ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream) const { - const SimpleMessage& this_ = *this; -#endif // PROTOBUF_CUSTOM_VTABLE - // @@protoc_insertion_point(serialize_to_array_start:example.SimpleMessage) - ::uint32_t cached_has_bits = 0; - (void)cached_has_bits; - - // string text = 1; - if (!this_._internal_text().empty()) { - const std::string& _s = this_._internal_text(); - ::google::protobuf::internal::WireFormatLite::VerifyUtf8String( - _s.data(), static_cast(_s.length()), ::google::protobuf::internal::WireFormatLite::SERIALIZE, "example.SimpleMessage.text"); - target = stream->WriteStringMaybeAliased(1, _s, target); - } - - if (PROTOBUF_PREDICT_FALSE(this_._internal_metadata_.have_unknown_fields())) { - target = - ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray( - this_._internal_metadata_.unknown_fields<::google::protobuf::UnknownFieldSet>(::google::protobuf::UnknownFieldSet::default_instance), target, stream); - } - // @@protoc_insertion_point(serialize_to_array_end:example.SimpleMessage) - return target; - } - -#if defined(PROTOBUF_CUSTOM_VTABLE) - ::size_t SimpleMessage::ByteSizeLong(const MessageLite& base) { - const SimpleMessage& this_ = static_cast(base); -#else // PROTOBUF_CUSTOM_VTABLE - ::size_t SimpleMessage::ByteSizeLong() const { - const SimpleMessage& this_ = *this; -#endif // PROTOBUF_CUSTOM_VTABLE - // @@protoc_insertion_point(message_byte_size_start:example.SimpleMessage) - ::size_t total_size = 0; - - ::uint32_t cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void)cached_has_bits; - - { - // string text = 1; - if (!this_._internal_text().empty()) { - total_size += 1 + ::google::protobuf::internal::WireFormatLite::StringSize( - this_._internal_text()); - } - } - return this_.MaybeComputeUnknownFieldsSize(total_size, - &this_._impl_._cached_size_); - } - -void SimpleMessage::MergeImpl(::google::protobuf::MessageLite& to_msg, const ::google::protobuf::MessageLite& from_msg) { - auto* const _this = static_cast(&to_msg); - auto& from = static_cast(from_msg); - // @@protoc_insertion_point(class_specific_merge_from_start:example.SimpleMessage) +void data::MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg) { + auto* const _this = static_cast(&to_msg); + auto& from = static_cast(from_msg); + // @@protoc_insertion_point(class_specific_merge_from_start:message.data) ABSL_DCHECK_NE(&from, _this); ::uint32_t cached_has_bits = 0; (void) cached_has_bits; - if (!from._internal_text().empty()) { - _this->_internal_set_text(from._internal_text()); + if (!from._internal_sender_name().empty()) { + _this->_internal_set_sender_name(from._internal_sender_name()); } _this->_internal_metadata_.MergeFrom<::google::protobuf::UnknownFieldSet>(from._internal_metadata_); } -void SimpleMessage::CopyFrom(const SimpleMessage& from) { -// @@protoc_insertion_point(class_specific_copy_from_start:example.SimpleMessage) +void data::CopyFrom(const data& from) { +// @@protoc_insertion_point(class_specific_copy_from_start:message.data) if (&from == this) return; Clear(); MergeFrom(from); } +PROTOBUF_NOINLINE bool data::IsInitialized() const { + return true; +} -void SimpleMessage::InternalSwap(SimpleMessage* PROTOBUF_RESTRICT other) { +::_pbi::CachedSize* data::AccessCachedSize() const { + return &_impl_._cached_size_; +} +void data::InternalSwap(data* PROTOBUF_RESTRICT other) { using std::swap; auto* arena = GetArena(); ABSL_DCHECK_EQ(arena, other->GetArena()); _internal_metadata_.InternalSwap(&other->_internal_metadata_); - ::_pbi::ArenaStringPtr::InternalSwap(&_impl_.text_, &other->_impl_.text_, arena); + ::_pbi::ArenaStringPtr::InternalSwap(&_impl_.sender_name_, &other->_impl_.sender_name_, arena); } -::google::protobuf::Metadata SimpleMessage::GetMetadata() const { - return ::google::protobuf::Message::GetMetadataImpl(GetClassData()->full()); +::google::protobuf::Metadata data::GetMetadata() const { + return ::_pbi::AssignDescriptors( + &descriptor_table_message_2eproto_getter, &descriptor_table_message_2eproto_once, + file_level_metadata_message_2eproto[0]); } // @@protoc_insertion_point(namespace_scope) -} // namespace example +} // namespace message namespace google { namespace protobuf { } // namespace protobuf } // namespace google // @@protoc_insertion_point(global_scope) -PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 static ::std::false_type - _static_init2_ PROTOBUF_UNUSED = - (::_pbi::AddDescriptors(&descriptor_table_message_2eproto), - ::std::false_type{}); #include "google/protobuf/port_undef.inc" diff --git a/Jason/lab3/src/proto/message.pb.h b/Jason/lab3/src/proto/message.pb.h index 02ee48e..fa555cd 100644 --- a/Jason/lab3/src/proto/message.pb.h +++ b/Jason/lab3/src/proto/message.pb.h @@ -1,7 +1,6 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! -// NO CHECKED-IN PROTOBUF GENCODE // source: message.proto -// Protobuf C++ Version: 5.28.0 +// Protobuf C++ Version: 4.25.4 #ifndef GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh #define GOOGLE_PROTOBUF_INCLUDED_message_2eproto_2epb_2eh @@ -11,12 +10,19 @@ #include #include -#include "google/protobuf/runtime_version.h" -#if PROTOBUF_VERSION != 5028000 -#error "Protobuf C++ gencode is built with an incompatible version of" -#error "Protobuf C++ headers/runtime. See" -#error "https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp" -#endif +#include "google/protobuf/port_def.inc" +#if PROTOBUF_VERSION < 4025000 +#error "This file was generated by a newer version of protoc which is" +#error "incompatible with your Protocol Buffer headers. Please update" +#error "your headers." +#endif // PROTOBUF_VERSION + +#if 4025004 < PROTOBUF_MIN_PROTOC_VERSION +#error "This file was generated by an older version of protoc which is" +#error "incompatible with your Protocol Buffer headers. Please" +#error "regenerate this file with a newer version of protoc." +#endif // PROTOBUF_MIN_PROTOC_VERSION +#include "google/protobuf/port_undef.inc" #include "google/protobuf/io/coded_stream.h" #include "google/protobuf/arena.h" #include "google/protobuf/arenastring.h" @@ -49,45 +55,48 @@ struct TableStruct_message_2eproto { }; extern const ::google::protobuf::internal::DescriptorTable descriptor_table_message_2eproto; -namespace example { -class SimpleMessage; -struct SimpleMessageDefaultTypeInternal; -extern SimpleMessageDefaultTypeInternal _SimpleMessage_default_instance_; -} // namespace example +namespace message { +class data; +struct dataDefaultTypeInternal; +extern dataDefaultTypeInternal _data_default_instance_; +} // namespace message namespace google { namespace protobuf { } // namespace protobuf } // namespace google -namespace example { +namespace message { // =================================================================== // ------------------------------------------------------------------- -class SimpleMessage final : public ::google::protobuf::Message -/* @@protoc_insertion_point(class_definition:example.SimpleMessage) */ { +class data final : + public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:message.data) */ { public: - inline SimpleMessage() : SimpleMessage(nullptr) {} - ~SimpleMessage() PROTOBUF_FINAL; - template - explicit PROTOBUF_CONSTEXPR SimpleMessage( - ::google::protobuf::internal::ConstantInitialized); - - inline SimpleMessage(const SimpleMessage& from) : SimpleMessage(nullptr, from) {} - inline SimpleMessage(SimpleMessage&& from) noexcept - : SimpleMessage(nullptr, std::move(from)) {} - inline SimpleMessage& operator=(const SimpleMessage& from) { + inline data() : data(nullptr) {} + ~data() override; + template + explicit PROTOBUF_CONSTEXPR data(::google::protobuf::internal::ConstantInitialized); + + inline data(const data& from) + : data(nullptr, from) {} + data(data&& from) noexcept + : data() { + *this = ::std::move(from); + } + + inline data& operator=(const data& from) { CopyFrom(from); return *this; } - inline SimpleMessage& operator=(SimpleMessage&& from) noexcept { + inline data& operator=(data&& from) noexcept { if (this == &from) return *this; if (GetArena() == from.GetArena() -#ifdef PROTOBUF_FORCE_COPY_IN_MOVE + #ifdef PROTOBUF_FORCE_COPY_IN_MOVE && GetArena() != nullptr -#endif // !PROTOBUF_FORCE_COPY_IN_MOVE + #endif // !PROTOBUF_FORCE_COPY_IN_MOVE ) { InternalSwap(&from); } else { @@ -114,28 +123,33 @@ class SimpleMessage final : public ::google::protobuf::Message static const ::google::protobuf::Reflection* GetReflection() { return default_instance().GetMetadata().reflection; } - static const SimpleMessage& default_instance() { + static const data& default_instance() { return *internal_default_instance(); } - static inline const SimpleMessage* internal_default_instance() { - return reinterpret_cast( - &_SimpleMessage_default_instance_); + static inline const data* internal_default_instance() { + return reinterpret_cast( + &_data_default_instance_); + } + static constexpr int kIndexInFileMessages = + 0; + + friend void swap(data& a, data& b) { + a.Swap(&b); } - static constexpr int kIndexInFileMessages = 0; - friend void swap(SimpleMessage& a, SimpleMessage& b) { a.Swap(&b); } - inline void Swap(SimpleMessage* other) { + inline void Swap(data* other) { if (other == this) return; -#ifdef PROTOBUF_FORCE_COPY_IN_SWAP - if (GetArena() != nullptr && GetArena() == other->GetArena()) { -#else // PROTOBUF_FORCE_COPY_IN_SWAP + #ifdef PROTOBUF_FORCE_COPY_IN_SWAP + if (GetArena() != nullptr && + GetArena() == other->GetArena()) { + #else // PROTOBUF_FORCE_COPY_IN_SWAP if (GetArena() == other->GetArena()) { -#endif // !PROTOBUF_FORCE_COPY_IN_SWAP + #endif // !PROTOBUF_FORCE_COPY_IN_SWAP InternalSwap(other); } else { ::google::protobuf::internal::GenericSwap(this, other); } } - void UnsafeArenaSwap(SimpleMessage* other) { + void UnsafeArenaSwap(data* other) { if (other == this) return; ABSL_DCHECK(GetArena() == other->GetArena()); InternalSwap(other); @@ -143,100 +157,80 @@ class SimpleMessage final : public ::google::protobuf::Message // implements Message ---------------------------------------------- - SimpleMessage* New(::google::protobuf::Arena* arena = nullptr) const PROTOBUF_FINAL { - return ::google::protobuf::Message::DefaultConstruct(arena); + data* New(::google::protobuf::Arena* arena = nullptr) const final { + return CreateMaybeMessage(arena); } using ::google::protobuf::Message::CopyFrom; - void CopyFrom(const SimpleMessage& from); + void CopyFrom(const data& from); using ::google::protobuf::Message::MergeFrom; - void MergeFrom(const SimpleMessage& from) { SimpleMessage::MergeImpl(*this, from); } - - private: - static void MergeImpl( - ::google::protobuf::MessageLite& to_msg, - const ::google::protobuf::MessageLite& from_msg); - - public: - bool IsInitialized() const { - return true; + void MergeFrom( const data& from) { + data::MergeImpl(*this, from); } - ABSL_ATTRIBUTE_REINITIALIZES void Clear() PROTOBUF_FINAL; - #if defined(PROTOBUF_CUSTOM_VTABLE) private: - static ::size_t ByteSizeLong(const ::google::protobuf::MessageLite& msg); - static ::uint8_t* _InternalSerialize( - const MessageLite& msg, ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream); - + static void MergeImpl(::google::protobuf::Message& to_msg, const ::google::protobuf::Message& from_msg); public: - ::size_t ByteSizeLong() const { return ByteSizeLong(*this); } - ::uint8_t* _InternalSerialize( - ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream) const { - return _InternalSerialize(*this, target, stream); - } - #else // PROTOBUF_CUSTOM_VTABLE + PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final; + bool IsInitialized() const final; + ::size_t ByteSizeLong() const final; + const char* _InternalParse(const char* ptr, ::google::protobuf::internal::ParseContext* ctx) final; ::uint8_t* _InternalSerialize( - ::uint8_t* target, - ::google::protobuf::io::EpsCopyOutputStream* stream) const final; - #endif // PROTOBUF_CUSTOM_VTABLE + ::uint8_t* target, ::google::protobuf::io::EpsCopyOutputStream* stream) const final; int GetCachedSize() const { return _impl_._cached_size_.Get(); } private: + ::google::protobuf::internal::CachedSize* AccessCachedSize() const final; void SharedCtor(::google::protobuf::Arena* arena); void SharedDtor(); - void InternalSwap(SimpleMessage* other); - private: - friend class ::google::protobuf::internal::AnyMetadata; - static ::absl::string_view FullMessageName() { return "example.SimpleMessage"; } + void InternalSwap(data* other); - protected: - explicit SimpleMessage(::google::protobuf::Arena* arena); - SimpleMessage(::google::protobuf::Arena* arena, const SimpleMessage& from); - SimpleMessage(::google::protobuf::Arena* arena, SimpleMessage&& from) noexcept - : SimpleMessage(arena) { - *this = ::std::move(from); + private: + friend class ::google::protobuf::internal::AnyMetadata; + static ::absl::string_view FullMessageName() { + return "message.data"; } - const ::google::protobuf::Message::ClassData* GetClassData() const PROTOBUF_FINAL; - static const ::google::protobuf::Message::ClassDataFull _class_data_; + protected: + explicit data(::google::protobuf::Arena* arena); + data(::google::protobuf::Arena* arena, const data& from); + public: + + static const ClassData _class_data_; + const ::google::protobuf::Message::ClassData*GetClassData() const final; + + ::google::protobuf::Metadata GetMetadata() const final; - public: - ::google::protobuf::Metadata GetMetadata() const; // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- + enum : int { - kTextFieldNumber = 1, + kSenderNameFieldNumber = 1, }; - // string text = 1; - void clear_text() ; - const std::string& text() const; + // string sender_name = 1; + void clear_sender_name() ; + const std::string& sender_name() const; template - void set_text(Arg_&& arg, Args_... args); - std::string* mutable_text(); - PROTOBUF_NODISCARD std::string* release_text(); - void set_allocated_text(std::string* value); + void set_sender_name(Arg_&& arg, Args_... args); + std::string* mutable_sender_name(); + PROTOBUF_NODISCARD std::string* release_sender_name(); + void set_allocated_sender_name(std::string* value); private: - const std::string& _internal_text() const; - inline PROTOBUF_ALWAYS_INLINE void _internal_set_text( + const std::string& _internal_sender_name() const; + inline PROTOBUF_ALWAYS_INLINE void _internal_set_sender_name( const std::string& value); - std::string* _internal_mutable_text(); + std::string* _internal_mutable_sender_name(); public: - // @@protoc_insertion_point(class_scope:example.SimpleMessage) + // @@protoc_insertion_point(class_scope:message.data) private: class _Internal; + friend class ::google::protobuf::internal::TcParser; static const ::google::protobuf::internal::TcParseTable< 0, 1, 0, - 34, 2> + 32, 2> _table_; - - static constexpr const void* _raw_default_instance_ = - &_SimpleMessage_default_instance_; - friend class ::google::protobuf::MessageLite; friend class ::google::protobuf::Arena; template @@ -244,14 +238,14 @@ class SimpleMessage final : public ::google::protobuf::Message using InternalArenaConstructable_ = void; using DestructorSkippable_ = void; struct Impl_ { - inline explicit constexpr Impl_( - ::google::protobuf::internal::ConstantInitialized) noexcept; - inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, - ::google::protobuf::Arena* arena); - inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, - ::google::protobuf::Arena* arena, const Impl_& from, - const SimpleMessage& from_msg); - ::google::protobuf::internal::ArenaStringPtr text_; + + inline explicit constexpr Impl_( + ::google::protobuf::internal::ConstantInitialized) noexcept; + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena); + inline explicit Impl_(::google::protobuf::internal::InternalVisibility visibility, + ::google::protobuf::Arena* arena, const Impl_& from); + ::google::protobuf::internal::ArenaStringPtr sender_name_; mutable ::google::protobuf::internal::CachedSize _cached_size_; PROTOBUF_TSAN_DECLARE_MEMBER }; @@ -273,56 +267,59 @@ class SimpleMessage final : public ::google::protobuf::Message #endif // __GNUC__ // ------------------------------------------------------------------- -// SimpleMessage +// data -// string text = 1; -inline void SimpleMessage::clear_text() { - ::google::protobuf::internal::TSanWrite(&_impl_); - _impl_.text_.ClearToEmpty(); +// string sender_name = 1; +inline void data::clear_sender_name() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + _impl_.sender_name_.ClearToEmpty(); } -inline const std::string& SimpleMessage::text() const +inline const std::string& data::sender_name() const ABSL_ATTRIBUTE_LIFETIME_BOUND { - // @@protoc_insertion_point(field_get:example.SimpleMessage.text) - return _internal_text(); + // @@protoc_insertion_point(field_get:message.data.sender_name) + return _internal_sender_name(); } template -inline PROTOBUF_ALWAYS_INLINE void SimpleMessage::set_text(Arg_&& arg, +inline PROTOBUF_ALWAYS_INLINE void data::set_sender_name(Arg_&& arg, Args_... args) { - ::google::protobuf::internal::TSanWrite(&_impl_); - _impl_.text_.Set(static_cast(arg), args..., GetArena()); - // @@protoc_insertion_point(field_set:example.SimpleMessage.text) + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + _impl_.sender_name_.Set(static_cast(arg), args..., GetArena()); + // @@protoc_insertion_point(field_set:message.data.sender_name) } -inline std::string* SimpleMessage::mutable_text() ABSL_ATTRIBUTE_LIFETIME_BOUND { - std::string* _s = _internal_mutable_text(); - // @@protoc_insertion_point(field_mutable:example.SimpleMessage.text) +inline std::string* data::mutable_sender_name() ABSL_ATTRIBUTE_LIFETIME_BOUND { + std::string* _s = _internal_mutable_sender_name(); + // @@protoc_insertion_point(field_mutable:message.data.sender_name) return _s; } -inline const std::string& SimpleMessage::_internal_text() const { - ::google::protobuf::internal::TSanRead(&_impl_); - return _impl_.text_.Get(); +inline const std::string& data::_internal_sender_name() const { + PROTOBUF_TSAN_READ(&_impl_._tsan_detect_race); + return _impl_.sender_name_.Get(); } -inline void SimpleMessage::_internal_set_text(const std::string& value) { - ::google::protobuf::internal::TSanWrite(&_impl_); - _impl_.text_.Set(value, GetArena()); +inline void data::_internal_set_sender_name(const std::string& value) { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + _impl_.sender_name_.Set(value, GetArena()); } -inline std::string* SimpleMessage::_internal_mutable_text() { - ::google::protobuf::internal::TSanWrite(&_impl_); - return _impl_.text_.Mutable( GetArena()); +inline std::string* data::_internal_mutable_sender_name() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + ; + return _impl_.sender_name_.Mutable( GetArena()); } -inline std::string* SimpleMessage::release_text() { - ::google::protobuf::internal::TSanWrite(&_impl_); - // @@protoc_insertion_point(field_release:example.SimpleMessage.text) - return _impl_.text_.Release(); +inline std::string* data::release_sender_name() { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + // @@protoc_insertion_point(field_release:message.data.sender_name) + return _impl_.sender_name_.Release(); } -inline void SimpleMessage::set_allocated_text(std::string* value) { - ::google::protobuf::internal::TSanWrite(&_impl_); - _impl_.text_.SetAllocated(value, GetArena()); +inline void data::set_allocated_sender_name(std::string* value) { + PROTOBUF_TSAN_WRITE(&_impl_._tsan_detect_race); + _impl_.sender_name_.SetAllocated(value, GetArena()); #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING - if (_impl_.text_.IsDefault()) { - _impl_.text_.Set("", GetArena()); + if (_impl_.sender_name_.IsDefault()) { + _impl_.sender_name_.Set("", GetArena()); } #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING - // @@protoc_insertion_point(field_set_allocated:example.SimpleMessage.text) + // @@protoc_insertion_point(field_set_allocated:message.data.sender_name) } #ifdef __GNUC__ @@ -330,7 +327,7 @@ inline void SimpleMessage::set_allocated_text(std::string* value) { #endif // __GNUC__ // @@protoc_insertion_point(namespace_scope) -} // namespace example +} // namespace message // @@protoc_insertion_point(global_scope) diff --git a/Jason/lab3/src/proto/message.proto b/Jason/lab3/src/proto/message.proto index 3c7c817..cca539f 100644 --- a/Jason/lab3/src/proto/message.proto +++ b/Jason/lab3/src/proto/message.proto @@ -1,8 +1,8 @@ syntax = "proto3"; -package example; +package message; // Define a simple message with a single string field -message SimpleMessage { - string text = 1; +message data { + string sender_name = 1; } \ No newline at end of file diff --git a/Jason/lab3/src/server.cpp b/Jason/lab3/src/server.cpp index cc1ed2b..3d83fd2 100644 --- a/Jason/lab3/src/server.cpp +++ b/Jason/lab3/src/server.cpp @@ -1,29 +1,80 @@ -#include "UDPSocket.hpp" +#include "server.hpp" +#include #include -#include -#include "proto/message.pb.h" +#include // For memset -int main() { - UDPSocket udpSocket; - // Bind the socket to a local IP and port - if (!udpSocket.bindSocket("127.0.0.1", 8080)) { - std::cerr << "Failed to bind socket\n"; - return -1; +#include "src/proto/message.pb.h" + +/** + * @brief Class Server + * + * Set up socket + */ +Server::Server(const std::string& server_ip, uint16_t server_port) { + _sock = socket(AF_INET, SOCK_DGRAM, 0); + if (_sock < 0) { + std::cerr << "Failed to create socket" << std::endl; + exit(1); } - while (true) { - // Receive a message - std::string serializedMessage, senderIp; - int senderPort; - ssize_t bytesReceived = udpSocket.receiveFrom(serializedMessage, senderIp, senderPort); - - if (bytesReceived > 0) { - std::cout << "Received message from " << senderIp << ":" << senderPort << ": " << serializedMessage << "\n"; - } else { - std::cerr << "Error receiving message\n"; - } + // Setup server address + memset(&_server_addr, 0, sizeof(_server_addr)); + _server_addr.sin_family = AF_INET; + _server_addr.sin_port = htons(server_port); + inet_pton(AF_INET, server_ip.c_str(), &_server_addr.sin_addr); + + if (bind(_sock, (struct sockaddr*)&_server_addr, sizeof(_server_addr)) < 0) { + std::cerr << "Failed to bind socket" << std::endl; + exit(1); } +} - return 0; +Server::~Server() { + close(_sock); } + +/** + * @brief Class Server + * + * Sends response message to client + */ +bool Server::SendMessage(const message::data& message) { + + std::string serialized_message; + if (!message.SerializeToString(&serialized_message)) { + std::cerr << "Failed to serialize message" << std::endl; + return false; + } + + if (sendto(_sock, serialized_message.data(), serialized_message.size(), 0, + (struct sockaddr*)&_client_addr, sizeof(_client_addr)) < 0) { + std::cerr << "Failed to send message" << std:: endl; + } + return true; +} + +/** + * @brief Class Server + * + * Receives message from client + */ +bool Server::ReceiveMessage(message::data& message) { + char buffer[_BUFFER_SIZE]; + socklen_t _client_addr_len = sizeof(_client_addr); + + ssize_t received_bytes = recvfrom(_sock, buffer, _BUFFER_SIZE, 0, + (struct sockaddr*)&_client_addr, &_client_addr_len); + if (received_bytes < 0) { + std::cerr << "Failed to receive message" << std::endl; + return false; + } + + if (!message.ParseFromArray(buffer, received_bytes)) { + std::cerr << "Failed to parse message" << std::endl; + return false; + } + + std::cout << "Received message from " << message.sender_name() << std::endl; + return true; +} \ No newline at end of file From fb1ae0e2e5ece713765ef39796196ffff612d440 Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 22:09:59 -0400 Subject: [PATCH 5/8] it works! --- Jason/lab3/CMakeLists.txt | 15 ++++++++- Jason/lab3/app/client_driver.cpp | 39 +++++++++++++++--------- Jason/lab3/app/server_driver.cpp | 1 - Jason/lab3/include/client.hpp | 2 +- Jason/lab3/include/server.hpp | 2 +- Jason/lab3/{src => }/proto/message.pb.cc | 0 Jason/lab3/{src => }/proto/message.pb.h | 0 Jason/lab3/{src => }/proto/message.proto | 0 Jason/lab3/readme.txt | 4 +++ Jason/lab3/result | 2 +- Jason/lab3/src/client.cpp | 4 +-- Jason/lab3/src/server.cpp | 4 +-- 12 files changed, 50 insertions(+), 23 deletions(-) rename Jason/lab3/{src => }/proto/message.pb.cc (100%) rename Jason/lab3/{src => }/proto/message.pb.h (100%) rename Jason/lab3/{src => }/proto/message.proto (100%) create mode 100644 Jason/lab3/readme.txt diff --git a/Jason/lab3/CMakeLists.txt b/Jason/lab3/CMakeLists.txt index d60ee1f..2d43862 100644 --- a/Jason/lab3/CMakeLists.txt +++ b/Jason/lab3/CMakeLists.txt @@ -5,10 +5,23 @@ project(Lab3) find_package(Protobuf REQUIRED) # Define the libraries -add_library(msg STATIC src/proto/message.pb.cc) +add_library(msg STATIC proto/message.pb.cc) add_library(server STATIC src/server.cpp) add_library(client STATIC src/client.cpp) +target_include_directories(client PUBLIC + "$" + "$" +) +target_include_directories(server PUBLIC + "$" + "$" +) +target_include_directories(msg PUBLIC + "$" + "$" +) + #Link the necessary targets target_link_libraries(msg PUBLIC protobuf::libprotobuf) target_link_libraries(client PUBLIC msg) diff --git a/Jason/lab3/app/client_driver.cpp b/Jason/lab3/app/client_driver.cpp index 4753c99..d78b74b 100644 --- a/Jason/lab3/app/client_driver.cpp +++ b/Jason/lab3/app/client_driver.cpp @@ -1,5 +1,4 @@ #include "client.hpp" -#include "server.hpp" #include /** @@ -19,23 +18,35 @@ int main() { std::cout << "Press Enter to send a message: "; std::getline(std::cin, input); - if (input.empty()) { - // Send the message - if (client.SendMessage(message)) { - std::cout << "Message sent successfully" << std::endl; - } else { - std::cerr << "Failed to send message" << std::endl; + while (true) { + std::cout << "Press Enter to send a message (or type -1 to exit): "; + std::getline(std::cin, input); + + // If the user enters -1, break the loop and exit + if (input == "-1") { + std::cout << "Exiting program..." << std::endl; + break; } - // Receive and print the response! - message::data responseMessage; - if (client.ReceiveMessage(responseMessage)) { - ; + // If Enter is pressed (input is empty), send the message + if (input.empty()) { + // Send the message + if (client.SendMessage(message)) { + std::cout << "Message sent successfully" << std::endl; + } else { + std::cerr << "Failed to send message" << std::endl; + } + + // Receive and print the response + message::data responseMessage; + if (client.ReceiveMessage(responseMessage)) { + std::cout << "Received response from server" << std::endl; + } else { + std::cerr << "Failed to receive response" << std::endl; + } } else { - std::cerr << "Failed to receive response" << std::endl; + std::cout << "No message sent." << std::endl; } - } else { - std::cout << "No message sent." << std::endl; } return 0; diff --git a/Jason/lab3/app/server_driver.cpp b/Jason/lab3/app/server_driver.cpp index 04ce02d..62a42e0 100644 --- a/Jason/lab3/app/server_driver.cpp +++ b/Jason/lab3/app/server_driver.cpp @@ -1,4 +1,3 @@ -#include "client.hpp" // Update include path #include "server.hpp" #include diff --git a/Jason/lab3/include/client.hpp b/Jason/lab3/include/client.hpp index 25f237d..69190eb 100644 --- a/Jason/lab3/include/client.hpp +++ b/Jason/lab3/include/client.hpp @@ -6,7 +6,7 @@ #include // For socket functions #include // For close -#include "src/proto/message.pb.h" +#include "message.pb.h" class Client { public: diff --git a/Jason/lab3/include/server.hpp b/Jason/lab3/include/server.hpp index d531fc0..a00446f 100644 --- a/Jason/lab3/include/server.hpp +++ b/Jason/lab3/include/server.hpp @@ -6,7 +6,7 @@ #include // For socket functions #include // For close -#include "src/proto/message.pb.h" +#include "message.pb.h" class Server { public: diff --git a/Jason/lab3/src/proto/message.pb.cc b/Jason/lab3/proto/message.pb.cc similarity index 100% rename from Jason/lab3/src/proto/message.pb.cc rename to Jason/lab3/proto/message.pb.cc diff --git a/Jason/lab3/src/proto/message.pb.h b/Jason/lab3/proto/message.pb.h similarity index 100% rename from Jason/lab3/src/proto/message.pb.h rename to Jason/lab3/proto/message.pb.h diff --git a/Jason/lab3/src/proto/message.proto b/Jason/lab3/proto/message.proto similarity index 100% rename from Jason/lab3/src/proto/message.proto rename to Jason/lab3/proto/message.proto diff --git a/Jason/lab3/readme.txt b/Jason/lab3/readme.txt new file mode 100644 index 0000000..b65438a --- /dev/null +++ b/Jason/lab3/readme.txt @@ -0,0 +1,4 @@ +1. nix-shell -p protobuf +2. cd jason/lab3 +3. nix build +4. run client_exe and server_exe found in result/bin \ No newline at end of file diff --git a/Jason/lab3/result b/Jason/lab3/result index 8c6ee39..43c35cc 120000 --- a/Jason/lab3/result +++ b/Jason/lab3/result @@ -1 +1 @@ -/nix/store/aik936ldfgrx0g16g441wfqpv6bljydz-lab3-0.1.0 \ No newline at end of file +/nix/store/7bly8pkr548h7q211pjxbpmfy3fxcd7g-lab3-0.1.0 \ No newline at end of file diff --git a/Jason/lab3/src/client.cpp b/Jason/lab3/src/client.cpp index 28a8039..66171ee 100644 --- a/Jason/lab3/src/client.cpp +++ b/Jason/lab3/src/client.cpp @@ -2,7 +2,7 @@ #include #include // For memset -#include "proto/message.pb.h" +#include "message.pb.h" /** * @brief Class Client @@ -68,6 +68,6 @@ bool Client::ReceiveMessage(message::data& message) { return false; } - std::cout << "Received message from " << message.sender_name() << std::endl; + std::cout << "Received " << message.sender_name() << std::endl; return true; } \ No newline at end of file diff --git a/Jason/lab3/src/server.cpp b/Jason/lab3/src/server.cpp index 3d83fd2..940558d 100644 --- a/Jason/lab3/src/server.cpp +++ b/Jason/lab3/src/server.cpp @@ -4,7 +4,7 @@ #include // For memset -#include "src/proto/message.pb.h" +#include "message.pb.h" /** * @brief Class Server @@ -75,6 +75,6 @@ bool Server::ReceiveMessage(message::data& message) { return false; } - std::cout << "Received message from " << message.sender_name() << std::endl; + std::cout << "Received " << message.sender_name() << std::endl; return true; } \ No newline at end of file From e6c47f580e70cbb434742c50caa2a17e25a030dd Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Thu, 12 Sep 2024 22:17:32 -0400 Subject: [PATCH 6/8] added cross building? --- Jason/lab3/flake.nix | 26 ++++++++++++++++++-------- Jason/lab3/result | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Jason/lab3/flake.nix b/Jason/lab3/flake.nix index 42e18f2..7ad3064 100644 --- a/Jason/lab3/flake.nix +++ b/Jason/lab3/flake.nix @@ -9,15 +9,17 @@ self, nixpkgs, nixutils }: - let lab3_overlay = final: prev: { - lab3 = final.callPackage ./default.nix { }; - }; - - my_overlays = [ lab3_overlay ]; - pkgs = import nixpkgs { + let system = "aarch64-darwin"; - overlays = [ self.overlays.default ]; - }; + lab3_overlay = final: prev: { + lab3 = final.callPackage ./default.nix { }; + }; + + my_overlays = [ lab3_overlay ]; + pkgs = import nixpkgs { + system = "aarch64-darwin"; + overlays = [ self.overlays.default ]; + }; in { overlays.default = nixpkgs.lib.composeManyExtensions my_overlays; @@ -40,5 +42,13 @@ export PS1="$(echo -e '\uF121') {\[$(tput sgr0)\]\[\033[38;5;228m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]} (${name}) \\$ \[$(tput sgr0)\]" ''; }; + + legacyPackages.aarch64-darwin = + import nixpkgs { + inherit system; + overlays = [ + (final: _: { lab3 = final.callPackage ./default.nix { }; }) + ]; + }; }; } \ No newline at end of file diff --git a/Jason/lab3/result b/Jason/lab3/result index 43c35cc..944d699 120000 --- a/Jason/lab3/result +++ b/Jason/lab3/result @@ -1 +1 @@ -/nix/store/7bly8pkr548h7q211pjxbpmfy3fxcd7g-lab3-0.1.0 \ No newline at end of file +/nix/store/r1isr5h60av9ryq4gwdawcckc37ar0c8-lab3-0.1.0 \ No newline at end of file From 649faa4841b1a3963f9e8a5d156438418dde4d3e Mon Sep 17 00:00:00 2001 From: Jason Mo <67805049+BANANAPEEL202@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:39:54 -0700 Subject: [PATCH 7/8] Delete Jason/lab3/result --- Jason/lab3/result | 1 - 1 file changed, 1 deletion(-) delete mode 120000 Jason/lab3/result diff --git a/Jason/lab3/result b/Jason/lab3/result deleted file mode 120000 index 944d699..0000000 --- a/Jason/lab3/result +++ /dev/null @@ -1 +0,0 @@ -/nix/store/r1isr5h60av9ryq4gwdawcckc37ar0c8-lab3-0.1.0 \ No newline at end of file From c27317031f78641d8c0ab1c0c1a004f4a17d2a1d Mon Sep 17 00:00:00 2001 From: BANANAPEEL202 Date: Fri, 13 Sep 2024 19:41:58 -0400 Subject: [PATCH 8/8] fixed readme and ignored results symlink --- .gitignore | 1 + Jason/lab3/{readme.txt => readme.md} | 0 2 files changed, 1 insertion(+) rename Jason/lab3/{readme.txt => readme.md} (100%) diff --git a/.gitignore b/.gitignore index b89cd3e..ec4144d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ JasonWang/Lab2/Part1/build JasonWang/Lab2/Part1/result /.vscode +Jason/lab3/result BenHall/lab1/part1/build BenHall/lab1/part2/build diff --git a/Jason/lab3/readme.txt b/Jason/lab3/readme.md similarity index 100% rename from Jason/lab3/readme.txt rename to Jason/lab3/readme.md