99 const std::string& aClientName )
103 m_lastError = wxS(
"API client is not connected" );
107 kiapi::common::ApiRequest request;
108 request.mutable_header()->set_client_name( aClientName );
110 if( !request.mutable_message()->PackFrom( aRequest ) )
112 m_lastError = wxS(
"Failed to pack command into ApiRequest" );
116 std::string requestStr = request.SerializeAsString();
117 void* sendBuf = nng_alloc( requestStr.size() );
125 std::memcpy( sendBuf, requestStr.data(), requestStr.size() );
127 int ret = nng_send(
m_socket, sendBuf, requestStr.size(), NNG_FLAG_ALLOC );
131 nng_free( sendBuf, requestStr.size() );
132 m_lastError = wxString::Format( wxS(
"nng_send failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
136 char* reply =
nullptr;
137 size_t replySize = 0;
138 int flags =
m_block ? 0 : NNG_FLAG_NONBLOCK;
140 ret = nng_recv(
m_socket, &reply, &replySize, NNG_FLAG_ALLOC | flags );
144 m_lastError = wxString::Format( wxS(
"nng_recv failed: %s" ), wxString::FromUTF8( nng_strerror( ret ) ) );
148 std::string responseStr( reply, replySize );
149 nng_free( reply, replySize );
151 if( !aResponse.ParseFromString( responseStr ) )
153 m_lastError = wxS(
"Failed to parse reply from KiCad" );
bool Send(const google::protobuf::Message &aRequest, kiapi::common::ApiResponse &aResponse, const std::string &aClientName="kicad")
Send a protobuf request to the KiCad API server and wait for a response.