Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 312371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:00:37+00:00 2026-05-12T08:00:37+00:00

I find myself constantly running into a situation where I have a set of

  • 0

I find myself constantly running into a situation where I have a set of messages that I need to send over a TCP/IP connection. I have never found a good solution for the design of the message class. I would like to have a message base class where all messages derive from it. Since each message will have different fields, this would allow me to access the fields through member variables or methods. Something like…

class message_base
{
  public:
   message_base();
   virtual ~message_base();

   unsigned int type;
};

class message_control : public message_base
{
  public:
   message_control();
   virtual ~message_control();

   unsigned int action;
};

This way I can create a message_control and access the action member for assigning to and reading from. I can also pass the messages around without writing too much code.

The problem arises when I need to send the messages. If I override the operator<< and operator>> then I can send the messages over one variable at a time. The problem with that solution is that with so many calls to send data, the context switches will slam the processor. Also, the streaming operator ends up the the socket class and not in the message class where I would prefer it lived.

socket& socket::operator<<(message_control& message)
{
   sock << type;
   sock << action;
}

If I pack the data in a buffer, I get away from C++ and more into the realm of C and find myself making generous use of pointers and the like. And, modifying the code is difficult and error prone. And, the streaming operator is still in the socket class and not the message class.

socket& socket::operator<<(message_control& message)
{
   byte* buffer = new byte[sizeof(message.type) + sizeof(message.action)];

   memcpy(buffer, message.type, sizeof(message.type));
   memcpy(buffer + sizeof(message.type), message.action, sizeof(message.action));

   sock.send(buffer);
}

My last attempt used an intermediate class to handle packing and unpacking the members in a buffer. The messages could implement operator<< and operator>> to the buffer class and then the buffer class is sent to the socket. This works but doesn’t feel right.

class socket
{
  public:
   socket();
   ~socket();

   socket& operator<<(buffer& buff);
};

class buffer
{
  public:
   buffer() {m_buffer = new byte[initial_size];}
   ~buffer() {delete [] m_buffer;}

   buffer& operator<<(unsigned int value);

  private:
   byte* m_buffer;
};

void message_control::serialize(buffer& buff)
{
   buff << type;
   buff << action;
}

I can’t help but feel there is an elegant solution to this problem. I can’t find any design patterns that match what I am trying to accomplish. Has anyone experienced this problem and come up with a going design that doesn’t make you feel like you would be better off with good old pointers and an array of bytes?

Update

I failed to mention in my original post that I am most often dealing with very well define wire protocols. That is why I typically need to roll my own solution and can’t use any of the wonderful toolkits available for messaging over a network connection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:00:37+00:00Added an answer on May 12, 2026 at 8:00 am

    “The problem with that solution is that with so many calls to send data, the context switches will slam the processor. Also, the streaming operator ends up the the socket class and not in the message class where I would prefer it lived.”

    The solution to the second problem is to define operator<< as a non-member function in the namespace which contains the message class, instead of as a member function of the socket class. ADL will find it.

    The solution to the first problem is to buffer the data within your process, and then flush at the end of each message. If Nagle buffering isn’t preventing context switches, then you might be able to achieve this by messing with the socket, I don’t know. What you certainly can do, though, is prepare each message before sending in a more C++-ish way. Replace:

    sock << type;
    sock << action;
    

    with:

    stringstream ss;
    ss << type;
    ss << action;
    sock << ss.str();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
I find myself running scripts and copy-pasting the output of these runs into emails
I commonly find myself extracting common behavior out of classes into helper/utility classes that
I find myself constantly pressing ctrl-z to undo the automatic formatting that happens in
I have Visual Studio 2008 and I find myself constantly adding the same 3
Sometimes I find myself in the situation where I want to execute several sequential
I find myself constantly learning new things in web development and there is always
I come from a c++ background and I find myself constantly doing this in
I find myself writing delegates occasionally for really simple functions (take no arguments and
I find myself needing to store public key certificates, and a single private key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.