iam trying to build an multihreading webservice. Single threading is working, in my main function i use this:
int main(int argc, char **argv) {
CardSoapBindingService CardSrvc;
Config Conf ;
Conf.update();
int port = Conf.listener_port;
if (!port)
CardSrvc.serve();
else {
if (CardSrvc.run(port)) {
CardSrvc.soap_stream_fault(std::cerr);
exit(-1);
}
}
return 0;
}
But i want multithreading, so i looked in the documentation and found their example, which i tried instead my code. While compiling i get this errors:
main.cpp: In function int main(int, char**)':
main.cpp:56: error:soap_serve' undeclared (first use this function)
main.cpp:56: error: (Each undeclared identifier is reported only once for each
function it appears in.)
main.cpp: In function void* process_request(void*)':<br>
main.cpp:101: error:soap_serve' undeclared (first use this function)
make: *** [main.o] Fehler 1
How can i get this working?
Important:
This code requires gsoap version 2.8.5 as a minimum. It was initially built on Solaris 8 with gsoap version 2.8.3, porting the code to Ubuntu and running under valgrind showed that the 2.8.3 gsoap++ library was corrupting memory which lead to a SIGSEGV. It should be noted that as of 25/11/11 the version of gsoap that Ubuntu installs using apt-get is the broken 2.8.3. A manual download and build of the latest version of gsoap was required (make sure to install flex and bison before you configure the gsoap build!).
Using gsoap 2.8.5 the code below happily creates threads and serves SOAP messages to multiple clients, valgrind now reports 0 errors with the memory allocation.
Looking at your code the example you have working has been built with the -i (or -j) option to create C++ objects. The thread examples in the gsoap doumention are written in standard C; hence the reference to functions such as soap_serve() which you don't have.
Below is my quick re-write of the multithreaded example to use the C+ objects generated. It is based on the following definition file:
// Content of file "calc.h":
//gsoap ns service name: Calculator
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service location: http://www.cs.fsu.edu/~engelen/calc.cgi
//gsoap ns schema namespace: urn:calc
//gsoap ns service method-action: add ""
int ns__add(double a, double b, double &result);
int ns__sub(double a, double b, double &result);
int ns__mul(double a, double b, double &result);
int ns__div(double a, double b, double &result);
The main server code then looks like this:
#include "soapCalculatorService.h" // get server object
#include "Calculator.nsmap" // get namespace bindings
#include <pthread.h>
void *process_request(void *calc) ;
int main(int argc, char* argv[])
{
CalculatorService c;
int port = atoi(argv[1]) ;
printf("Starting to listen on port %d\n", port) ;
if (soap_valid_socket(c.bind(NULL, port, 100)))
{
CalculatorService *tc ;
pthread_t tid;
for (;;)
{
if (!soap_valid_socket(c.accept()))
return c.error;
tc = c.copy() ; // make a safe copy
if (tc == NULL)
break;
pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tc);
printf("Created a new thread %ld\n", tid) ;
}
}
else {
return c.error;
}
}
void *process_request(void *calc)
{
pthread_detach(pthread_self());
CalculatorService *c = static_cast<CalculatorService*>(calc) ;
c->serve() ;
c->destroy() ;
delete c ;
return NULL;
}
This is a very basic threading model but it shows how to use the C++ classes generated by gsoap to build a multithreaded server.
Related
Accessing IIS webservice using Gsoap.
I have similar error as in this link shows that the error was solved compiling with -lssl.
I did the same thing in build as
g++ -o client client.cpp stdsoap2.cpp soapC.cpp soapDataManagementSoapProxy.cpp -I /usr/local/ssl/include -L/home/xavier/GSOAP/lib -lgsoapssl++ -L/usr/local/ssl/lib -lssl
My GSOAP lib was build with OpenSSL.
But I still have error as
SOAP 1.2 fault SOAP-ENV:Sender[no subcode]
"OpenSSL not installed: recompile with -DWITH_OPENSSL"
Detail: [no detail]
My test code is as follow. What could be wrong?
#include "soapDataManagementSoapProxy.h"
#include "DataManagementSoap.nsmap"
const char server[] = "https://XXXXXXX.com/XXXmanagement.asmx";
int main(int argc, char **argv)
{
DataManagementSoapProxy webinf;
webinf.soap_endpoint = server;
_tempuri__ReadTestData* a;
_tempuri__ReadTestDataResponse res;
int ret = webinf.ReadTestData(a, res);
if (webinf.error){
webinf.soap_stream_fault(std::cerr);
}
else{
//printf("result = %g\n", result);
std::cout << "Success " << std::endl;
}
webinf.destroy(); /* clean up mem */
return 0;
}
Solution for this issue is
#include "calc.nsmap"
#include "soapcalcProxy.h" // generated with soapcpp2 -j calc.h
calcProxy calc("https-server-endpoint-URL");
double sum;
soap_ssl_init(); // init SSL (just need to do this once in an application)
// soap_ssl_no_init(); // or prevent init OpenSSL when already initialized elsewhere in an application
if (soap_ssl_client_context(calc.soap,
SOAP_SSL_DEFAULT,
NULL, // no keyfile
NULL, // no keyfile password
"cacerts.pem", // trusted certificates (or use self-signed cacert.pem)
NULL, // no capath to trusted certificates
NULL // no random data to seed randomness
))
{
calc.soap_stream_fault(std::cerr);
exit(EXIT_FAILURE);
}
if (calc.add(1.23, 4.56, sum) == SOAP_OK)
I would need some help with using ngspice as a library in a webassembly (wasm) project.
I installed emsdk and newest version of emcc (1.39.20) and downloaded the source of ngspice version 32.
To my greatest surprise, I was able to compile ngspice to wasm target by following this guide:
emconfigure ./configure --with-ngshared --disable-debug
emmake make
(I had to patch configure a little to pass the checks by adding .out.js a.out.wasm to this line:)
# The possible output files:
ac_files="a.out a.out.js a.out.wasm conftest.exe conftest a.exe a_out.exe b.out conftest.*"
This produced a libngspice.so.0.0.0 file that I tried to link to from C++ code. However that failed with duplicate symbol: main. So it seemed that libngspice.so.0.0.0 contained a main function, that shouldn't have been there if I understand the purpose of the --with-ngshared of the configure script correctly.
So I manually removed the main function from main.c of ngspice and recomplied using the above method. This time I could successfully complie my own project, linking to ngspice. However when I call ngSpice_Init, I recieve the following runtime errors:
stderr Note: can't find init file.
exception thrown: RuntimeError: unreachable executed,#http://localhost:8001/sim.js line 1802 > WebAssembly.instantiate:wasm-function[67]:0x24e9
#http://localhost:8001/sim.js line 1802 > WebAssembly.instantiate:wasm-function[88]:0x423b
...
Minimal reproducible steps:
compile ngspice as above
compile the code below using em++ -o sim.html sim.cpp lib/libngspice.so
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sharedspice.h"
using namespace std;
int recieve_char(char * str, int id, void* p){
printf("recieved %s\n", str);
}
int recieve_stat(char* status, int id, void* p){
printf("status: %s\n", status);
}
int ngexit(int status, bool unload, bool exit, int id, void* p){
printf("exit: %d\n", status);
}
int recieve_data(vecvaluesall* data, int numstructs, int id, void* p){
printf("data recieved: %f\n", data->vecsa[0]->creal);
}
int recieve_init_data(vecinfoall* data, int id, void* p){
printf("init data recieved from: %d\n", id);
}
int ngrunning(bool running, int id, void* p){
if(running){
printf("ng is running\n");
}else{
printf("ng is not running\n");
}
}
int main(){
ngSpice_Init(&recieve_char, &recieve_stat, &ngexit,
&recieve_data, &recieve_init_data, &ngrunning, (void*)NULL);
char** circarray = (char**)malloc(sizeof(char*) * 7);
circarray[0] = strdup("test array");
circarray[1] = strdup("V1 1 0 1");
circarray[2] = strdup("R1 1 2 1");
circarray[3] = strdup("C1 2 0 1 ic=0");
circarray[4] = strdup(".tran 10u 3 uic");
circarray[5] = strdup(".end");
circarray[6] = NULL;
ngSpice_Circ(circarray);
ngSpice_Command("run");
return 0;
}
So could someone please help me correctly compiling ngspice library to wasm target?
(Before someone asks, yes, I've seen this question, but it didn't help much)
I was able to compile the library and my example code after making several changes to the ngspice source. The patch and a guide on how to compile ngspice to wasm, can be found here.
(The issue leading to the error shown in my question was with the example code, not returning anything from functions that by signature should return int. This is not tolerated in wasm.)
I am currently trying to get a working tls websocket client running in C++ (which is a pain in the ass) and I have tried CPP Rest SDK as well as Websocket++. Both spit out a bunch of compile errors (see below). When I tried compiling it using Websocket++ without tls, it compiles so the error clearly is related to SSL.
I tried different OpenSSL versions (1.0.1, 1.0.2, 1.1.0), different C++ versions (11, 14 and even 17), and I just can't get it to compile.
I googled and none of the solutions worked. I am on Ubuntu 16 and the build command I am using looks like this:
g++ source/* -o test.out -Iinclude/ -std=c++14 -L/lib64 -lcurl -lboost_system -lssl -lcrypto -l pthread
Here are some of the errors:
/usr/include/boost/asio/ssl/detail/impl/openssl_init.ipp: In constructor ‘boost::asio::ssl::detail::openssl_init_base::do_init::do_init()’:
/usr/include/boost/asio/ssl/detail/impl/openssl_init.ipp:43:23: error: expected id-expression before ‘(’ token
mutexes_.resize(::CRYPTO_num_locks());
/usr/include/boost/asio/ssl/detail/impl/engine.ipp:221:9: error: ‘SSL_R_SHORT_READ’ was not declared in this scope
ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
And here is the basic source code:
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
void on_close(client* c, websocketpp::connection_hdl hdl) {
c->get_alog().write(websocketpp::log::alevel::app, "Connection Closed");
}
int main(int argc, char* argv[]) {
client c;
std::string uri = "wss://gateway.discord.gg/";
if (argc == 2) {
uri = argv[1];
}
try {
// set logging policy if needed
c.clear_access_channels(websocketpp::log::alevel::frame_header);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
//c.set_error_channels(websocketpp::log::elevel::none);
// Initialize ASIO
c.init_asio();
// Register our handlers
c.set_open_handler(bind(&on_open,&c,::_1));
c.set_fail_handler(bind(&on_fail,&c,::_1));
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
c.set_close_handler(bind(&on_close,&c,::_1));
// Create a connection to the given URI and queue it for connection once
// the event loop starts
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
c.connect(con);
// Start the ASIO io_service run loop
c.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
This was a long time ago but in case it helps, adding -lcrypto -lssl in the g++ cmd arguments solved the problem for me.
I want to get driver version of nVidia video card.
So I used WMI and get data from "DriverVersion" obejct of "Win32_VideoController" class.
But it was like "9.18.13.1106"(file version) and what I wanted is something like "311.06"(treiber version).
Where can I get that information?
If it is impossible on WMI, I want to know other way to get that.
Thanks.
You can do this using NVML from nVidia's Tesla Deployment Kit. You can retrieve the internal driver version (the one you're accustomed to seeing for an nVidia driver) with code like this:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <nvml.h>
#include <windows.h>
namespace {
typedef nvmlReturn_t (*init)();
typedef nvmlReturn_t (*shutdown)();
typedef nvmlReturn_t (*get_version)(char *, unsigned);
class NVML {
init nvmlInit;
shutdown nvmlShutdown;
get_version nvmlGetDriverVersion;
std::string find_dll() {
std::string loc(getenv("ProgramW6432"));
loc += "\\Nvidia Corporation\\nvsmi\\nvml.dll";
return loc;
}
public:
NVML() {
HMODULE lib = LoadLibrary(find_dll().c_str());
nvmlInit = (init)GetProcAddress(lib, "nvmlInit");
nvmlShutdown = (shutdown)GetProcAddress(lib, "nvmlShutdown");
nvmlGetDriverVersion = (get_version)GetProcAddress(lib, "nvmlSystemGetDriverVersion");
if (NVML_SUCCESS != nvmlInit())
throw(std::runtime_error("Unable to initialize NVML"));
}
std::string get_ver() {
char buffer[81];
nvmlGetDriverVersion(buffer, sizeof(buffer));
return std::string(buffer);
}
~NVML() {
if (NVML_SUCCESS != nvmlShutdown())
throw(std::runtime_error("Unable to shut down NVML"));
}
};
}
int main() {
std::cout << "nVidia Driver version: " << NVML().get_ver();
}
Note that if you're writing this purely for your own use on a machine where you're free to edit the PATH, you can simplify this quite a bit. Most of the code deals with the fact that this uses NVML.DLL, which is in a directory that's not normally on the path, so the code loads that dynamically, and uses GetProcAddress to find the functions in it that we need to use. In this case, we're only using three functions, so it's not all that difficult to deal with, but it still at drastically increases the length of the code.
If we could ignore all that nonsense, the real code would just come out to something on this general order:
nvmlInit();
nvmlSystemGetDriverVersion(result, sizeof(result));
std::cout << result;
nvmlShutdown();
Anyway, to build it, you'll need a command line something like:
cl -Ic:\tdk\nvml\include nv_driver_version.cpp
...assuming you've installed the Tesla Deployment Kit at c:\tdk.
In any case, yes, I've tested this to at least some degree. On my desktop it prints out:
nVidia Driver version: 314.22
...which matches what I have installed.
To get the Nvidia driver version through C++ on Win64:
Download NVAPI https://developer.nvidia.com/rtx/path-tracing/nvapi/get-started, a few MB
The main folder of the downloaded archive contains several header files, one of which is nvapi.h. Those headers are needed for compilation. The subfolder amd64 contains nvapi64.lib, which is needed for linking. The following code will now show the driver version:
#include <iostream>
extern "C" {
#include "nvapi.h"
}
int main() {
NvAPI_Status status = NVAPI_OK;
NvAPI_ShortString str;
status = NvAPI_Initialize();
if (status == NVAPI_LIBRARY_NOT_FOUND) {
//in this case NvAPI_GetErrorMessage() will only return an empty string
std::printf("error no nvidia driver found\n");
} else if (status != NVAPI_OK) {
NvAPI_GetErrorMessage(status, str);
std::printf("error initializing nvapi: %s\n", str);
}
NvU32 version = 0;
NvAPI_ShortString branch;
status = NvAPI_SYS_GetDriverAndBranchVersion(&version, branch);
if (status != NVAPI_OK) {
NvAPI_GetErrorMessage(status, str);
std::printf("error getting driver version: %s\n", str);
} else {
std::printf("driver version %d.%d", version / 100, version % 100);
}
}
I am trying write a "Hello World" example using C++Builder. This is my first project so I have probably made a simple mistake.
I want to create a console application that calls a calculator web service.
I open C++Builder 2007 and I create a Console Application. A cpp file called File1.cpp appears. Here it is the content:
//---------------------------------------------------------------------------
#include <iostream.h>
#include <vcl.h>
#pragma hdrstop
#include "calculator.h"
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
double a, b;
cout << "Enter the values to sum\n";
cout << "A: ";
cin >> a;
cout << "B: ";
cin >> b;
cout << "\nA+B:";
cout << GetCalculatorSoap()->Add(1,2);
cout << "\n\nPress any key to continue...";
getchar();
return 0;
}
//---------------------------------------------------------------------------
Additionally I added the soap proxy going into New->Other->WebService->WSDL Importer.
Using the WSDL http://www.dneonline.com/calculator.asmx?WSDL
This action added calculator.cpp:
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL : http://www.dneonline.com/calculator.asmx?WSDL
// >Import : http://www.dneonline.com/calculator.asmx?WSDL:0
// Encoding : utf-8
// Version : 1.0
// (21/02/2012 19:48:31 - - $Rev: 10138 $)
// ************************************************************************ //
#include <vcl.h>
#pragma hdrstop
#if !defined(calculatorH)
#include "calculator.h"
#endif
namespace NS_calculator {
_di_CalculatorSoap GetCalculatorSoap(bool useWSDL,
AnsiString addr, THTTPRIO* HTTPRIO)
{
static const char* defWSDL= "http://www.dneonline.com/calculator.asmx?WSDL";
static const char* defURL = "http://www.dneonline.com/calculator.asmx";
static const char* defSvc = "Calculator";
static const char* defPrt = "CalculatorSoap";
if (addr=="")
addr = useWSDL ? defWSDL : defURL;
THTTPRIO* rio = HTTPRIO ? HTTPRIO : new THTTPRIO(0);
if (useWSDL) {
rio->WSDLLocation = addr;
rio->Service = defSvc;
rio->Port = defPrt;
} else {
rio->URL = addr;
}
_di_CalculatorSoap service;
rio->QueryInterface(service);
if (!service && !HTTPRIO)
delete rio;
return service;
}
// ************************************************************************ //
// This routine registers the interfaces and types exposed by the WebService.
// ************************************************************************ //
static void RegTypes()
{
/* CalculatorSoap */
InvRegistry()->RegisterInterface(__interfaceTypeinfo(CalculatorSoap),
L"http://tempuri.org/", L"utf-8");
InvRegistry()->RegisterDefaultSOAPAction(__interfaceTypeinfo(CalculatorSoap),
L"http://tempuri.org/%operationName%");
InvRegistry()->RegisterInvokeOptions(__interfaceTypeinfo(CalculatorSoap),
ioDocument);
}
#pragma startup RegTypes 32
}; // NS_calculator
When I run the application it raises an exception when calling GetCalculatorSoap()->Add(1,2):
---------------------------
Debugger Exception Notification
---------------------------
Project Test.exe raised exception class EOleSysError
with message 'CoInitialize has not been called'.
---------------------------
Break Continue Help
---------------------------
Debugging it seems the GetCalculatorSoap() executes ok, but just before calling the Add method the exception is thrown...
Any ideas what is wrong? Thanks!
The error message tells you what the problem is - CoInitialize has not been called. (Actually, it's preferable to call CoInitializeEx instead, but either will work.)
Your SOAP code is using COM methods, and therefore COM has to be initialized first. This is done on a per-thread basis.
You can fix it by calling CoInitialize(NULL);' at the beginning of your main function. Don't forget to call CoUnitialize(); at the end of main as well.
In Delphi, CoInitialize/CoUninitialize are declared in the ActiveX unit. In C++Builder, it seems to be in OBJBASE.H (a quick search found it there, and that's also what's indicated in the MSDN documentation.
(If you're used to writing VCL form based apps, you won't have seen this before; the VCL initializes COM for you automatically. You're seeing it now because you're writing a console app.)