本人在这块卡了比较久,现记录能运行的代码。
这块主要是由于UA_String为结构体变量,char[]转换成UA_String时,还会用到char[]的空间长度,如果长度不对,会导致和UaExpert连接不成功,表现为:
Error'BadUserAccessDenied'
由此段代码可以得到
以下为我使用的方法:
widget.h
extern int sizename;
extern char* username3;
extern char* password3;//定义三个外部变量
widget.cpp
void servertest2::on_openserverbtn_clicked()
{
QString username = ui->Username->text();
QSring password = ui->Password->text();
QByteArray ba = username.toLatin1(); // must
char* username2= ba.data();
sizename = strlen(username2);
username3 = (char*) malloc(sizeof(char(sizename)));//分配指定数量的地址空间
strcpy(username3, username2);
QByteArray ca = password.toLatin1(); // must
char* password2 = ca.data();
sizename = strlen(password2);
password3 = (char*)malloc(sizeof(char(sizename)));
strcpy(password3, password2);
if (ui->AnonymousLogin->isChecked())
anonymouslogin();//匿名登录
if (ui->PasswordLogin->isChecked())
passwordlogin();//账号密码登录
}
ui界面:很多还没完善,暂时没有搞证书和安全策略
线程.cpp,具体可参考https://wanghao1314.blog.csdn.net/article/details/104713548
#include"widget.h"//要把包含外部变量的头文件包含进来
//下面是其他博主的代码,具体可看
static UA_Boolean
allowAddNode(UA_Server* server, UA_AccessControl* ac,
const UA_NodeId* sessionId, void* sessionContext,
const UA_AddNodesItem* item) {
printf("Called allowAddNode\n");
return UA_TRUE;
}
static UA_Boolean
allowAddReference(UA_Server* server, UA_AccessControl* ac,
const UA_NodeId* sessionId, void* sessionContext,
const UA_AddReferencesItem* item) {
printf("Called allowAddReference\n");
return UA_TRUE;
}
static UA_Boolean
allowDeleteNode(UA_Server* server, UA_AccessControl* ac,
const UA_NodeId* sessionId, void* sessionContext,
const UA_DeleteNodesItem* item) {
printf("Called allowDeleteNode\n");
return UA_FALSE; // Do not allow deletion from client
}
static UA_Boolean
allowDeleteReference(UA_Server* server, UA_AccessControl* ac,
const UA_NodeId* sessionId, void* sessionContext,
const UA_DeleteReferencesItem* item) {
printf("Called allowDeleteReference\n");
return UA_TRUE;
}
static UA_String usernamestr;
static UA_String passwordstr;
static UA_UsernamePasswordLogin logins;//不能在这里直接赋值,不然logins会是空的,不知道为什么
int server3(void)//基本服务器,可自行添加功能信息模型等
{
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server* server = UA_Server_new();
UA_ServerConfig* config = UA_Server_getConfig(server);
UA_ServerConfig_setDefault(config);
config->accessControl.clear(&config->accessControl);
UA_StatusCode retval = UA_AccessControl_default(config, false, NULL,
&config->securityPolicies[config->securityPoliciesSize - 1].policyUri, 1, &logins);
//if (retval != UA_STATUSCODE_GOOD)
// goto cleanup;
/* Set accessControl functions for nodeManagement */
config->accessControl.allowAddNode = allowAddNode;
config->accessControl.allowAddReference = allowAddReference;
config->accessControl.allowDeleteNode = allowDeleteNode;
config->accessControl.allowDeleteReference = allowDeleteReference;
retval = UA_Server_run(server, &running);
//cleanup:
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
void servert2::working2()//线程工作函数,且刚刚定义的ua_string和login必须在此处初始化。
{
usernamestr= UA_STRING(username3);
passwordstr= UA_STRING(password3);
//可以通过以下两个代码查看自己的UA_String变量的值
printf("size_t:%d\n", (UA_Int32)usernamestr.length);
printf("NodeID: %.*s\n", (UA_Int32)usernamestr.length, usernamestr.data);
logins = { usernamestr,passwordstr };
server3();
}