Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried using function capnp::writePackedMessageToFd(fd, builder) to serialize C++ message and send it as packet to the file descriptor. Using the above function in my code gave me runtime Exception.

Code:
C++
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include "MapSchema.capnp.h"       // Include the generated schema code
#include <capnp/serialize-packed.h>
#include <iostream>
#include <winsock2.h>        // Include Winsock for UDP sockets
#include <kj/exception.h>

#pragma comment(lib, "ws2_32.lib") // Link against Winsock library

int main() {
   WSADATA wsaData;
   unsigned char* buff;
   if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
       std::cerr << "Failed to initialize Winsock." << std::endl;
       return 1;
   }

   // Create a UDP socket
   SOCKET udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
   if (udpSocket == INVALID_SOCKET) {
       std::cerr << "Failed to create socket." << std::endl;
       WSACleanup();
       return 1;
   }
   std::cout << "WSA Success" << std::endl;

   // Define the destination UDP address
   sockaddr_in destAddr;
   destAddr.sin_family = AF_INET;
   destAddr.sin_port = htons(12345);         // Destination port
   destAddr.sin_addr.s_addr = 
                     inet_addr("127.0.0.1"); // Destination IP address

   // Create a Cap'n Proto message
   ::capnp::MallocMessageBuilder message;
   KeyValueMap::Builder mapBuilder = message.initRoot<keyvaluemap>();

   std::cout << "ip Success" << std::endl;
   // Add entries to the map
   auto entries = mapBuilder.initEntries(2);
   entries[0].setKey("key1");
   entries[0].setValue("value1");
   entries[1].setKey("key2");
   entries[1].setValue("value2");

   kj::HandleOutputStream out((HANDLE)udpSocket);
    std::cout << "soc: " << &out;
    capnp::writePackedMessage(out, message);

   // Clean up Winsock
   closesocket(udpSocket);
   WSACleanup();

   return 0;
}

Exception:
Unhandled exception at 0x76D5D9502 in file.exception Microsoft C++ exception: kj::ExceptionImp at memory location 0x0102D4D8


What I have tried:

Replacing the code snippet:
C++
kj::HandleOutputStream out((HANDLE)udpSocket);
    std::cout << "soc: " << &out;
    capnp::writePackedMessage(out, message);


with this:
C++
auto serializedMessage = capnp::messageToFlatArray(message);
const char* buffer = serializedMessage.asChars().begin();
int bufferLength = serializedMessage.asChars().size();

buff = const_cast<unsigned char*="">(reinterpret_cast<const unsigned="" char*="">(buffer));
int result = sendto(udpSocket, buffer, bufferLength, 0,
    (sockaddr*)&destAddr, sizeof(destAddr));
if (result == SOCKET_ERROR) {
    std::cerr << "Failed to send data: " << WSAGetLastError() << std::endl;
}

Solved the exception, so does that mean capnp::writePackedMessageToFd(fd, builder) cannot be used in Windows to send the serialized data directly to file descriptor?
Posted
Updated 29-Aug-23 0:36am
v3
Comments
Richard MacCutchan 25-Aug-23 5:02am    
You first need to find out what caused the exception.
raddevus 25-Aug-23 14:01pm    
I found this, which could be similar and may just provide a bit more related info: https://stackoverflow.com/questions/75006774/capn-proto-unaligned-data-error-while-trying-to-sendreceive-serialized-object

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900