Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ queue doesn’t like less than 8 char*s
    primarykey
    data
    text
    <p>A program written in C++ reads from a network socket and then writes to local socket. It uses a separate thread for the “read”.</p> <p>When a message is read, it is put into a char * queue (using mutex from the boost libraries to make it thread-safe).</p> <p>Meanwhile the queue is checked to see if it is empty, if it isn’t, then the first message is popped from the queue (again using mutex) and written to the local socket as a char *.</p> <p>My issue is: when a message of 4 bytes is pushed onto the queue, the queue saves it without issue, however when writing the message back out again, it has increased the message to eight bytes! The “new” four bytes are zero.</p> <hr> <p>Example A</p> <pre><code>Message in: {4,0,0,0} Saved to queue as; &lt;4&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt; Read from queue as: &lt;4&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt; </code></pre> <hr> <p>Example B</p> <pre><code>Message in: {4,0,0,0,8,0,0,0} Saved to queue as; &lt;4&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;8&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt; Read from queue as: &lt;4&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt;, &lt;8&gt;, &lt;0&gt;, &lt;0&gt;, &lt;0&gt; </code></pre> <hr> <p>Any ideas as to the reason for this? Can the queue class only cope with a minimum number of chars? (Wouldn’t have thought so as there’s the “empty” method). (This isn’t a major issue, as I never talk in less than eight bytes; I just want to know in case it comes up and attacks me later on in life.)</p> <p>I did a bit of digging around online and through the documentation and found the odd referece to a buffer, but that seems to be more to do with using the queue as a buffer, rather than it having one...</p> <p>Other information;</p> <p>OS: RedHat</p> <p>IDE: Eclipse</p> <hr> <p>Code: Queue</p> <pre><code>//Thread-safe call to save the message to the queue void MessageQueue::SaveToQueue(char* Data) { // Lock the mutex to prevent any other threads accessing this member (released when method exits). boost::mutex::scoped_lock l(m_Msg); //int i = 0; //while (i &lt; sizeof(Data))//iLength) //{ // unsigned int ByteVaule = Data[i];//pBuffer[i];//ByteValue = int(pBuffer[i]);//unsigned int(pBuffer[i]); // cout &lt;&lt; "Buffer in Queue" &lt;&lt; i &lt;&lt; ": " &lt;&lt; ByteVaule &lt;&lt; endl; // i++; //} MsgQ.push(Data); } //Thread-safe call to get the message from the queue char* MessageQueue::GetFromQueue() { // Lock the mutex to prevent any other threads accessing this member (released when method exits). boost::mutex::scoped_lock l(m_Msg); char* message = MsgQ.front(); MsgQ.pop(); return message; } //Thread-safe call to check if the queue is empty bool MessageQueue::IsEmpty() { // Lock the mutex to prevent any other threads accessing this member (released when method exits). boost::mutex::scoped_lock l(m_Msg); return MsgQ.empty(); } </code></pre> <hr> <p>Code: Manager int iStatus = 0;</p> <pre><code>//Start class to store message queue MessageQueue* pQueue = new MessageQueue(); // Current hard coded value for the write scoket location // TODO: change this to reading from enviroment variable string WritePath = "DefaultSocket"; ReadSocket* pRead = new ReadSocket(); WriteSocket* pWrite = new WriteSocket(); cout &lt;&lt; "Creating read socket" &lt;&lt; endl; iStatus = pRead-&gt;CreateSocket(pQueue); cout &lt;&lt; "Creating write socket." &lt;&lt; endl; iStatus = pWrite-&gt;CreateSocket(WritePath); //while is running, check the message container and process it as needed while (pRead-&gt;IsRunning()) { while (!(pQueue-&gt;IsEmpty())) { char* Msg = pQueue-&gt;GetFromQueue(); iStatus = pWrite-&gt;WriteToSocket(Msg); // TODO: catch on failure } //sleep(1); } //Destroy sockets as program is closing pWrite-&gt;~WriteSocket(); pRead-&gt;~ReadSocket(); // TODO: add exception? //Token return return iStatus; </code></pre> <hr> <p>To save making this too long and complex the read and write sockets are the same as in</p> <pre><code>http://www.linuxhowtos.org/C_C++/socket.htm </code></pre> <p>The read char* is saved to the queue using the </p> <pre><code>SaveToQueue() </code></pre> <p>method and taken from the queue using the</p> <pre><code>GetFromQueue </code></pre> <p>method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload