临时保存
This commit is contained in:
70
windows/src/common/services/ConstDefine.h
Normal file
70
windows/src/common/services/ConstDefine.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef CONST_DEFINE_H
|
||||
#define CONST_DEFINE_H
|
||||
|
||||
enum class ConstDefine {
|
||||
CONNECTING = 0,
|
||||
CONNECT_SUCCESS,
|
||||
CONNECT_FAILED,
|
||||
KICKED_OFFLINE,
|
||||
USER_TOKEN_EXPIRED,
|
||||
JOINED_GROUP_ADDED,
|
||||
JOINED_GROUP_DELETED,
|
||||
GROUP_MEMBER_ADDED,
|
||||
GROUP_MEMBER_DELETED,
|
||||
GROUP_APPLICATION_ADDED,
|
||||
GROUP_APPLICATION_DELETED,
|
||||
GROUP_INFO_CHANGED,
|
||||
GROUP_DISMISSED,
|
||||
GROUP_MEMBER_INFO_CHANGED,
|
||||
GROUP_APPLICATION_ACCEPTED,
|
||||
GROUP_APPLICATION_REJECTED,
|
||||
FRIEND_APPLICATION_ADDED,
|
||||
FRIEND_APPLICATION_DELETED,
|
||||
FRIEND_APPLICATION_ACCEPTED,
|
||||
FRIEND_APPLICATION_REJECTED,
|
||||
FRIEND_ADDED,
|
||||
FRIEND_DELETED,
|
||||
FRIEND_INFO_CHANGED,
|
||||
BLACK_ADDED,
|
||||
BLACK_DELETED,
|
||||
SYNC_SERVER_START,
|
||||
SYNC_SERVER_FINISH,
|
||||
SYNC_SERVER_PROGRESS,
|
||||
SYNC_SERVER_FAILED,
|
||||
NEW_CONVERSATION,
|
||||
CONVERSATION_CHANGED,
|
||||
TOTAL_UNREAD_MESSAGE_COUNT_CHANGED,
|
||||
RECV_NEW_MESSAGE,
|
||||
RECV_C2C_READ_RECEIPT,
|
||||
RECV_GROUP_READ_RECEIPT,
|
||||
NEW_RECV_MESSAGE_REVOKED,
|
||||
RECV_MESSAGE_EXTENSIONS_CHANGED,
|
||||
RECV_MESSAGE_EXTENSIONS_DELETED,
|
||||
RECV_MESSAGE_EXTENSIONS_ADDED,
|
||||
RECV_OFFLINE_NEW_MESSAGE,
|
||||
MSG_DELETED,
|
||||
RECV_NEW_MESSAGES,
|
||||
RECV_OFFLINE_NEW_MESSAGES,
|
||||
SELF_INFO_UPDATED,
|
||||
USER_STATUS_CHANGED,
|
||||
RECV_CUSTOM_BUSINESS_MESSAGE,
|
||||
MESSAGE_KV_INFO_CHANGED,
|
||||
OPEN,
|
||||
PART_SIZE,
|
||||
HASH_PART_PROGRESS,
|
||||
HASH_PART_COMPLETE,
|
||||
UPLOAD_ID,
|
||||
UPLOAD_PART_COMPLETE,
|
||||
UPLOAD_COMPLETE,
|
||||
COMPLETE,
|
||||
CONVERSATION_USER_INPUT_STATUS_CHANGED,
|
||||
RECV_ONLINE_ONLY_MESSAGE,
|
||||
RECV_MESSAGE_EDIT,
|
||||
USER_TOKEN_INVALID,
|
||||
ON_PROGRESS
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // EVENT_TYPES_H
|
||||
339
windows/src/common/services/IMManager.cpp
Normal file
339
windows/src/common/services/IMManager.cpp
Normal file
@@ -0,0 +1,339 @@
|
||||
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "IMManager.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "../NimResult.h"
|
||||
#include <libopenimsdk.h>
|
||||
#include "../ZegoDataUtils.h"
|
||||
#include "ConstDefine.h"
|
||||
|
||||
|
||||
IMManagerService::IMManagerService() {
|
||||
m_serviceName = "imManager";
|
||||
|
||||
_im_manager_listener.onInitSdk = [](int state, char *msg) {
|
||||
// friend added, handle friendInfo
|
||||
|
||||
flutter::EncodableMap arguments;
|
||||
switch (ConstDefine(state)) {
|
||||
case ConstDefine::CONNECTING:
|
||||
arguments.insert(std::make_pair("type", "onConnecting"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::CONNECT_SUCCESS:
|
||||
arguments.insert(std::make_pair("type", "onConnectSuccess"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::CONNECT_FAILED:
|
||||
arguments.insert(std::make_pair("type", "onConnectFailed"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::KICKED_OFFLINE:
|
||||
arguments.insert(std::make_pair("type", "onKickedOffline"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::USER_TOKEN_EXPIRED:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::USER_TOKEN_INVALID:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
}
|
||||
notifyEvent("connectListener", arguments);
|
||||
};
|
||||
|
||||
_im_manager_listener.baseCallBack = [](char *opid, int errCode, char *errMsg, char *msg) {
|
||||
CallBaseCallBack(msg, errCode, errMsg, msg);
|
||||
};
|
||||
|
||||
_im_manager_listener.uploadCallback = [](int state, char *msg) {
|
||||
// upload progress
|
||||
|
||||
flutter::EncodableMap arguments;
|
||||
switch (ConstDefine(state)) {
|
||||
case ConstDefine::OPEN:
|
||||
arguments.insert(std::make_pair("type", "onConnecting"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::PART_SIZE:
|
||||
arguments.insert(std::make_pair("type", "onConnectSuccess"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::HASH_PART_PROGRESS:
|
||||
arguments.insert(std::make_pair("type", "onConnectFailed"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::HASH_PART_COMPLETE:
|
||||
arguments.insert(std::make_pair("type", "onKickedOffline"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::UPLOAD_ID:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::UPLOAD_PART_COMPLETE:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::UPLOAD_COMPLETE:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
case ConstDefine::COMPLETE:
|
||||
arguments.insert(std::make_pair("type", "onUserTokenExpired"));
|
||||
arguments.insert(std::make_pair("data", *msg));
|
||||
break;
|
||||
}
|
||||
notifyEvent("uploadFileListener", arguments);
|
||||
};
|
||||
}
|
||||
|
||||
void IMManagerService::onMethodCalled(
|
||||
const std::string &method, const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (method == "initSDK") {
|
||||
initializeSDK(arguments, result);
|
||||
} else if (method == "unInitSDK") {
|
||||
unInitSDK(arguments, result);
|
||||
} else if (method == "login") {
|
||||
login(arguments, result);
|
||||
} else if (method == "logout") {
|
||||
logout(arguments, result);
|
||||
} else if (method == "getLoginStatus") {
|
||||
getLoginStatus(arguments, result);
|
||||
} else if (method == "uploadFile") {
|
||||
uploadFile(arguments, result);
|
||||
} else if (method == "uploadLogs") {
|
||||
uploadLogs(arguments, result);
|
||||
} else if (method == "logs") {
|
||||
logs(arguments, result);
|
||||
} else if (method == "updateFcmToken") {
|
||||
updateFcmToken(arguments, result);
|
||||
} else if (method == "setAppBackgroundStatus") {
|
||||
setAppBackgroundStatus(arguments, result);
|
||||
} else if (method == "networkStatusChanged") {
|
||||
networkStatusChanged(arguments, result);
|
||||
|
||||
} else {
|
||||
result->NotImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void IMManagerService::initializeSDK(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(arguments->at(ZFValue("operationID")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
auto config = map_2_json(*arguments);
|
||||
char *config_cs = const_cast<char *>(config.c_str());
|
||||
|
||||
|
||||
auto error = init_sdk(_im_manager_listener.onInitSdk,
|
||||
operationID_cs,
|
||||
config_cs);
|
||||
if (error == 0) {
|
||||
// handle error
|
||||
std::string msg = "IM init failed";
|
||||
result->Error("", "",
|
||||
NimResult::getErrorResult(error, ""));
|
||||
} else {
|
||||
|
||||
result->Success(EncodableValue(error == 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void IMManagerService::unInitSDK(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(
|
||||
arguments->at(flutter::EncodableValue("operationID")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
|
||||
un_init_sdk(operationID_cs);
|
||||
|
||||
} else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::login(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(
|
||||
arguments->at(flutter::EncodableValue("operationID")));
|
||||
auto userID = zego_value_get_string(arguments->at(flutter::EncodableValue("userID")));
|
||||
auto token = zego_value_get_string(arguments->at(flutter::EncodableValue("token")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
char *userID_cs = const_cast<char *>(userID.c_str());
|
||||
char *token_cs = const_cast<char *>(token.c_str());
|
||||
|
||||
NewBaseCallBack(operationID, result);
|
||||
|
||||
::login(_im_manager_listener.baseCallBack, operationID_cs, userID_cs, token_cs);
|
||||
|
||||
} else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::logout(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(
|
||||
arguments->at(flutter::EncodableValue("operationID")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
NewBaseCallBack(operationID, result);
|
||||
::logout(_im_manager_listener.baseCallBack, operationID_cs);
|
||||
|
||||
} else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::getLoginStatus(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(
|
||||
arguments->at(flutter::EncodableValue("operationID")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
|
||||
int status = get_login_status(operationID_cs);
|
||||
flutter::EncodableMap statusMap;
|
||||
|
||||
result->Success(flutter::EncodableValue(status));
|
||||
} else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::uploadFile(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
// if (arguments) {
|
||||
// auto operationID = zego_value_get_string(arguments->at(flutter::EncodableValue("operationID")));
|
||||
// auto id = zego_value_get_string(arguments->at(flutter::EncodableValue("id")));
|
||||
// auto fileInfo = map_2_json(*arguments);
|
||||
// char* operationID_cs = const_cast<char*>(operationID.c_str());
|
||||
// char* fileInfo_cs = const_cast<char*>(fileInfo.c_str());
|
||||
// uploadFileResult = result;
|
||||
// upload_file(_im_manager_listener.onUploadFile, operationID_cs, fileInfo_cs, _im_manager_listener.uploadCallback);
|
||||
// }
|
||||
// else {
|
||||
// result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
// }
|
||||
}
|
||||
|
||||
void IMManagerService::uploadLogs(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
// if (arguments) {
|
||||
// auto operationID = zego_value_get_string(
|
||||
// arguments->at(flutter::EncodableValue("operationID")));
|
||||
// auto line = zego_value_get_int(arguments->at(flutter::EncodableValue("line")));
|
||||
// auto ex = zego_value_get_string(arguments->at(flutter::EncodableValue("ex")));
|
||||
// char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
// char *ex_cs = const_cast<char *>(ex.c_str());
|
||||
//
|
||||
// upload_logs(_im_manager_listener.onUploadLogs, operationID_cs, line, ex_cs);
|
||||
//
|
||||
// } else {
|
||||
// result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
// }
|
||||
}
|
||||
|
||||
void IMManagerService::logs(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
// if (arguments) {
|
||||
// auto operationID = zego_value_get_string(arguments->at(flutter::EncodableValue("operationID")));
|
||||
// auto logLevel = zego_value_get_int(arguments->at(flutter::EncodableValue("logLevel")));
|
||||
// auto file = zego_value_get_string(arguments->at(flutter::EncodableValue("file")));
|
||||
// auto line = zego_value_get_int(arguments->at(flutter::EncodableValue("line")));
|
||||
// auto msgs = zego_value_get_string(arguments->at(flutter::EncodableValue("msgs")));
|
||||
// auto err = zego_value_get_string(arguments->at(flutter::EncodableValue("err")));
|
||||
// auto keyAndValue = zego_value_get_string(arguments->at(flutter::EncodableValue("keyAndValue")));
|
||||
// char* operationID_cs = const_cast<char*>(operationID.c_str());
|
||||
// char* file_cs = const_cast<char*>(file.c_str());
|
||||
// char* msgs_cs = const_cast<char*>(msgs.c_str());
|
||||
// char* err_cs = const_cast<char*>(err.c_str());
|
||||
// char* keyAndValue_cs = const_cast<char*>(keyAndValue.c_str());
|
||||
//
|
||||
// auto error = logs(_im_manager_listener.onLogs, operationID_cs, logLevel, file_cs, line, msgs_cs, err_cs, keyAndValue_cs);
|
||||
// if (error != 0) {
|
||||
// std::string msg = "Logging failed";
|
||||
// result->Error("", "", NimResult::getErrorResult(error, msg));
|
||||
// }
|
||||
// else {
|
||||
// result->Success(NimResult::getSuccessResult());
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
// }
|
||||
}
|
||||
|
||||
void IMManagerService::updateFcmToken(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(
|
||||
arguments->at(flutter::EncodableValue("operationID")));
|
||||
auto fcmToken = zego_value_get_string(arguments->at(flutter::EncodableValue("fcmToken")));
|
||||
auto expireTime = zego_value_get_int(arguments->at(flutter::EncodableValue("expireTime")));
|
||||
char *operationID_cs = const_cast<char *>(operationID.c_str());
|
||||
char *fcmToken_cs = const_cast<char *>(fcmToken.c_str());
|
||||
NewBaseCallBack(operationID, result);
|
||||
update_fcm_token(_im_manager_listener.baseCallBack, operationID_cs, fcmToken_cs,
|
||||
expireTime);
|
||||
} else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::setAppBackgroundStatus(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(arguments->at(flutter::EncodableValue("operationID")));
|
||||
auto isBackground = zego_value_get_bool(arguments->at(flutter::EncodableValue("isBackground")));
|
||||
char* operationID_cs = const_cast<char*>(operationID.c_str());
|
||||
NewBaseCallBack(operationID, result);
|
||||
set_app_background_status(_im_manager_listener.baseCallBack, operationID_cs, isBackground);
|
||||
|
||||
}
|
||||
else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
void IMManagerService::networkStatusChanged(
|
||||
const flutter::EncodableMap *arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
if (arguments) {
|
||||
auto operationID = zego_value_get_string(arguments->at(flutter::EncodableValue("operationID")));
|
||||
char* operationID_cs = const_cast<char*>(operationID.c_str());
|
||||
NewBaseCallBack(operationID, result);
|
||||
network_status_changed(_im_manager_listener.baseCallBack, operationID_cs);
|
||||
|
||||
}
|
||||
else {
|
||||
result->Error("INVALID_ARGUMENT", "Arguments cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
75
windows/src/common/services/IMManager.h
Normal file
75
windows/src/common/services/IMManager.h
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef IMMANAGER_H
|
||||
#define IMMANAGER_H
|
||||
|
||||
#include "../FLTService.h"
|
||||
#include "Listen.h"
|
||||
|
||||
struct IMManagerServiceListener {
|
||||
CB_I_S onInitSdk;
|
||||
CB_S_I_S_S baseCallBack;
|
||||
CB_I_S uploadCallback;
|
||||
};
|
||||
|
||||
class IMManagerService : public FLTService {
|
||||
public:
|
||||
IMManagerService();
|
||||
virtual void onMethodCalled(
|
||||
const std::string& method, const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
||||
override;
|
||||
|
||||
private:
|
||||
void initializeSDK(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void unInitSDK(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void login(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void logout(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void getLoginStatus(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void uploadFile(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void uploadLogs(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void logs(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void updateFcmToken(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void setAppBackgroundStatus(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
void networkStatusChanged(
|
||||
const flutter::EncodableMap* arguments,
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
|
||||
IMManagerServiceListener _im_manager_listener;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // IMMANAGER_H
|
||||
20
windows/src/common/services/Listen.cpp
Normal file
20
windows/src/common/services/Listen.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "Listen.h"
|
||||
#include <unordered_map>
|
||||
|
||||
std::unordered_map <std::string, std::unique_ptr<BaseCallBack>> g_baseCallBackMap;
|
||||
std::mutex g_baseCallBackMapMutex;
|
||||
|
||||
void NewBaseCallBack(std::string opid,
|
||||
std::shared_ptr <flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
std::lock_guard <std::mutex> lock(g_baseCallBackMapMutex);
|
||||
g_baseCallBackMap[opid] = std::make_unique<BaseCallBack>(result);
|
||||
}
|
||||
|
||||
void CallBaseCallBack(char *opid, int errCode, char *errMsg, char *msg) {
|
||||
std::lock_guard <std::mutex> lock(g_baseCallBackMapMutex);
|
||||
auto it = g_baseCallBackMap.find(std::string(opid));
|
||||
if (it != g_baseCallBackMap.end()) {
|
||||
auto ctx = std::move(it->second); //unique_ptr
|
||||
ctx->HandleCallback(opid, errCode, errMsg, msg);
|
||||
}
|
||||
}
|
||||
58
windows/src/common/services/Listen.h
Normal file
58
windows/src/common/services/Listen.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef LISTEN_H
|
||||
#define LISTEN_H
|
||||
|
||||
#include <libopenimsdk.h>
|
||||
#include "flutter/encodable_value.h"
|
||||
#include "flutter/method_result.h"
|
||||
|
||||
|
||||
|
||||
class BaseCallBack {
|
||||
public:
|
||||
BaseCallBack(std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
|
||||
this->result = result;
|
||||
}
|
||||
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result = nullptr;
|
||||
|
||||
void HandleCallback(char* opid, int errCode, char* errMsg, char* msg) {
|
||||
if (result == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (errCode == 0) {
|
||||
result->Success(flutter::EncodableValue(std::string(msg)));
|
||||
}
|
||||
else {
|
||||
result->Error(std::to_string(errCode), std::string(errMsg), flutter::EncodableValue(std::string(msg)));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 全局映射表
|
||||
extern std::unordered_map<std::string, std::unique_ptr<BaseCallBack>> g_baseCallBackMap;
|
||||
extern std::mutex g_baseCallBackMapMutex;
|
||||
|
||||
|
||||
void NewBaseCallBack(std::string opid, std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
void CallBaseCallBack(char* opid, int errCode, char* errMsg, char* msg);
|
||||
|
||||
|
||||
|
||||
//todo 没想好怎么实现
|
||||
class uploadFileCallBack {
|
||||
public:
|
||||
uploadFileCallBack(std::string id) {
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
std::string id = "";
|
||||
|
||||
void HandleCallback(char* opid, int errCode, char* errMsg, char* msg) {
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
0
windows/src/common/services/MessageManager.cpp
Normal file
0
windows/src/common/services/MessageManager.cpp
Normal file
0
windows/src/common/services/MessageManager.h
Normal file
0
windows/src/common/services/MessageManager.h
Normal file
311
windows/src/common/services/wrapp_cpp_function.inc
Normal file
311
windows/src/common/services/wrapp_cpp_function.inc
Normal file
@@ -0,0 +1,311 @@
|
||||
/*
|
||||
Using C++ template functions and recursion to initialize a function pointer pool,
|
||||
combined with lambda expressions, this implementation manages function pointers,
|
||||
achieving acquisition and release of pointers.All wrappers are used to convert
|
||||
and map C++ functions to C functions, and with the use of indexed locking,
|
||||
a thread-safe function pointer pool is implemented.For example, the wrapping
|
||||
functions essentially map C++ functions to C functions. When a C function callback is triggered,
|
||||
the function pointer pool, initialized through template recursion, locates the corresponding C++ function and invokes it.
|
||||
*/
|
||||
|
||||
#define MAX_NUM_OF_CB_S 10
|
||||
#define MAX_NUM_OF_CB_I_S 10
|
||||
#define MAX_NUM_OF_CB_S_I_S_S 10
|
||||
#define MAX_NUM_OF_CB_S_I_S_S_I 10
|
||||
#define SLEEP_TIME_FOR_GET_INDEX 100 //ms
|
||||
|
||||
// use recursive template to generate enough function pointer array
|
||||
// and define c type function interface
|
||||
// and define manager class to manage function pool
|
||||
namespace {
|
||||
CB_S* _fps_cb_s=new CB_S[MAX_NUM_OF_CB_S];
|
||||
CB_I_S* _fps_cb_i_s=new CB_I_S[MAX_NUM_OF_CB_I_S];
|
||||
CB_S_I_S_S* _fps_cb_s_i_s_s=new CB_S_I_S_S[MAX_NUM_OF_CB_S_I_S_S];
|
||||
CB_S_I_S_S_I* _fps_cb_s_i_s_s_i=new CB_S_I_S_S_I[MAX_NUM_OF_CB_S_I_S_S_I];
|
||||
// c type func interface call cpp function
|
||||
std::function<void(const std::string&)>* _cpp_function_cb_s=new std::function<void(const std::string&)>[MAX_NUM_OF_CB_S];
|
||||
std::function<void(int,const std::string&)>* _cpp_function_cb_i_s=new std::function<void(int,const std::string&)>[MAX_NUM_OF_CB_I_S];
|
||||
std::function<void(const std::string&,int,const std::string&,const std::string&)>* _cpp_function_cb_s_i_s_s=new std::function<void(const std::string&,int,const std::string&,const std::string&)>[MAX_NUM_OF_CB_S_I_S_S];
|
||||
std::function<void(const std::string&,int,const std::string&,const std::string&,int)>* _cpp_function_cb_s_i_s_s_i=new std::function<void(const std::string&,int,const std::string&,const std::string&,int)>[MAX_NUM_OF_CB_S_I_S_S_I];
|
||||
|
||||
template<int N>
|
||||
void _generate_cb_s(){
|
||||
_fps_cb_s[N]=[](char* c_str){
|
||||
_cpp_function_cb_s[N](std::string(c_str));
|
||||
};
|
||||
_generate_cb_s<N-1>();
|
||||
}
|
||||
template<>
|
||||
void _generate_cb_s<0>(){
|
||||
_fps_cb_s[0]=[](char* c_str){
|
||||
_cpp_function_cb_s[0](std::string(c_str));
|
||||
};
|
||||
}
|
||||
|
||||
template<int N>
|
||||
void _generate_cb_i_s(){
|
||||
_fps_cb_i_s[N]=[](int code,char* c_str){
|
||||
_cpp_function_cb_i_s[N](code,std::string(c_str));
|
||||
};
|
||||
_generate_cb_i_s<N-1>();
|
||||
}
|
||||
template<>
|
||||
void _generate_cb_i_s<0>(){
|
||||
_fps_cb_i_s[0]=[](int code,char* c_str){
|
||||
_cpp_function_cb_i_s[0](code,std::string(c_str));
|
||||
};
|
||||
}
|
||||
template<int N>
|
||||
void _generate_cb_s_i_s_s(){
|
||||
_fps_cb_s_i_s_s[N]=[](char* operationID,int code,char* c_str,char* c_str2){
|
||||
_cpp_function_cb_s_i_s_s[N](std::string(operationID),code,std::string(c_str),std::string(c_str2));
|
||||
};
|
||||
_generate_cb_s_i_s_s<N-1>();
|
||||
}
|
||||
template<>
|
||||
void _generate_cb_s_i_s_s<0>(){
|
||||
_fps_cb_s_i_s_s[0]=[](char* operationID,int code,char* c_str,char* c_str2){
|
||||
_cpp_function_cb_s_i_s_s[0](std::string(operationID),code,std::string(c_str),std::string(c_str2));
|
||||
};
|
||||
}
|
||||
template<int N>
|
||||
void _generate_cb_s_i_s_s_i(){
|
||||
_fps_cb_s_i_s_s_i[N]=[](char* operationID,int code,char* c_str,char* c_str2,int c_int){
|
||||
_cpp_function_cb_s_i_s_s_i[N](std::string(operationID),code,std::string(c_str),std::string(c_str2),c_int);
|
||||
};
|
||||
_generate_cb_s_i_s_s_i<N-1>();
|
||||
}
|
||||
template<>
|
||||
void _generate_cb_s_i_s_s_i<0>(){
|
||||
_fps_cb_s_i_s_s_i[0]=[](char* operationID,int code,char* c_str,char* c_str2,int c_int){
|
||||
_cpp_function_cb_s_i_s_s_i[0](std::string(operationID),code,std::string(c_str),std::string(c_str2),c_int);
|
||||
};
|
||||
}
|
||||
|
||||
// init global function pointer array
|
||||
void init(){
|
||||
_generate_cb_s<MAX_NUM_OF_CB_S-1>();
|
||||
_generate_cb_i_s<MAX_NUM_OF_CB_I_S-1>();
|
||||
_generate_cb_s_i_s_s<MAX_NUM_OF_CB_S_I_S_S-1>();
|
||||
_generate_cb_s_i_s_s_i<MAX_NUM_OF_CB_S_I_S_S_I-1>();
|
||||
}
|
||||
// define sigle instance class to manage function pool
|
||||
class FuncPoolManager{
|
||||
private:
|
||||
// define a global bitmap, and support atomic operation, to manage cb_s pool
|
||||
std::bitset<MAX_NUM_OF_CB_S> _cb_s_bitmap;
|
||||
std::bitset<MAX_NUM_OF_CB_I_S> _cb_i_s_bitmap;
|
||||
std::bitset<MAX_NUM_OF_CB_S_I_S_S> _cb_s_i_s_s_bitmap;
|
||||
std::bitset<MAX_NUM_OF_CB_S_I_S_S_I> _cb_s_i_s_s_i_bitmap;
|
||||
std::mutex _cb_s_mutex;
|
||||
std::mutex _cb_i_s_mutex;
|
||||
std::mutex _cb_s_i_s_s_mutex;
|
||||
std::mutex _cb_s_i_s_s_i_mutex;
|
||||
FuncPoolManager(){
|
||||
init();
|
||||
}
|
||||
FuncPoolManager(const FuncPoolManager&){}
|
||||
public:
|
||||
static FuncPoolManager& get_instance(){
|
||||
static FuncPoolManager instance;
|
||||
return instance;
|
||||
}
|
||||
// get a available cb_s function index
|
||||
int get_cb_s_index(){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_mutex);
|
||||
int index=-1;
|
||||
for(int i=0;i<_cb_s_bitmap.size();i++){
|
||||
if(_cb_s_bitmap[i]==0){
|
||||
_cb_s_bitmap[i]=1;
|
||||
index=i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
// get a available cb_i_s function index
|
||||
int get_cb_i_s_index(){
|
||||
std::lock_guard<std::mutex> lock(_cb_i_s_mutex);
|
||||
int index=-1;
|
||||
for(int i=0;i<_cb_i_s_bitmap.size();i++){
|
||||
if(_cb_i_s_bitmap[i]==0){
|
||||
_cb_i_s_bitmap[i]=1;
|
||||
index=i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
// get a available cb_s_i_s_s function index
|
||||
int get_cb_s_i_s_s_index(){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_i_s_s_mutex);
|
||||
int index=-1;
|
||||
for(int i=0;i<_cb_s_i_s_s_bitmap.size();i++){
|
||||
if(_cb_s_i_s_s_bitmap[i]==0){
|
||||
_cb_s_i_s_s_bitmap[i]=1;
|
||||
index=i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
// get a available cb_s_i_s_s_i function index
|
||||
int get_cb_s_i_s_s_i_index(){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_i_s_s_i_mutex);
|
||||
int index=-1;
|
||||
for(int i=0;i<_cb_s_i_s_s_i_bitmap.size();i++){
|
||||
if(_cb_s_i_s_s_i_bitmap[i]==0){
|
||||
_cb_s_i_s_s_i_bitmap[i]=1;
|
||||
index=i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
// release a available cb_s function index
|
||||
int release_cb_s_index(int index){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_mutex);
|
||||
if(index<0||index>=_cb_s_bitmap.size()){
|
||||
return -1;
|
||||
}
|
||||
_cpp_function_cb_s[index]=nullptr;
|
||||
_cb_s_bitmap[index]=0;
|
||||
return 0;
|
||||
}
|
||||
// release a available cb_i_s function index
|
||||
int release_cb_i_s_index(int index){
|
||||
std::lock_guard<std::mutex> lock(_cb_i_s_mutex);
|
||||
if(index<0||index>=_cb_i_s_bitmap.size()){
|
||||
return -1;
|
||||
}
|
||||
_cpp_function_cb_i_s[index]=nullptr;
|
||||
_cb_i_s_bitmap[index]=0;
|
||||
return 0;
|
||||
}
|
||||
// release a available cb_s_i_s_s function index
|
||||
int release_cb_s_i_s_s_index(int index){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_i_s_s_mutex);
|
||||
if(index<0||index>=_cb_s_i_s_s_bitmap.size()){
|
||||
return -1;
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s[index]=nullptr;
|
||||
_cb_s_i_s_s_bitmap[index]=0;
|
||||
return 0;
|
||||
}
|
||||
// release a available cb_s_i_s_s_i function index
|
||||
int release_cb_s_i_s_s_i_index(int index){
|
||||
std::lock_guard<std::mutex> lock(_cb_s_i_s_s_i_mutex);
|
||||
if(index<0||index>=_cb_s_i_s_s_i_bitmap.size()){
|
||||
return -1;
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s_i[index]=nullptr;
|
||||
_cb_s_i_s_s_i_bitmap[index]=0;
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
FuncPoolManager& instance=FuncPoolManager::get_instance();
|
||||
|
||||
// wrapper persistent function
|
||||
// wrapp CB_S,if function pool is full,return nullptr
|
||||
CB_S _wrapper_cpp_function(const std::function<void(const std::string&)>& cpp_function) {
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_index();
|
||||
}
|
||||
_cpp_function_cb_s[index]=cpp_function;
|
||||
return _fps_cb_s[index];
|
||||
}
|
||||
// wrapp CB_I_S
|
||||
CB_I_S _wrapper_cpp_function(const std::function<void(int,const std::string&)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_i_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_i_s_index();
|
||||
}
|
||||
_cpp_function_cb_i_s[index]=cpp_function;
|
||||
return _fps_cb_i_s[index];
|
||||
}
|
||||
// wrapp CB_S_I_S_S
|
||||
CB_S_I_S_S _wrapper_cpp_function(const std::function<void(const std::string&,int,const std::string&,const std::string&)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_i_s_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_i_s_s_index();
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s[index]=cpp_function;
|
||||
return _fps_cb_s_i_s_s[index];
|
||||
}
|
||||
// wrapp CB_S_I_S_S_I
|
||||
CB_S_I_S_S_I _wrapper_cpp_function(const std::function<void(const std::string&,int,const std::string&,const std::string&,int)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_i_s_s_i_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_i_s_s_i_index();
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s_i[index]=cpp_function;
|
||||
return _fps_cb_s_i_s_s_i[index];
|
||||
}
|
||||
|
||||
// wrapp function to onetime function
|
||||
CB_S _wrapper_callonce_cpp_function(const std::function<void(const std::string&)>& cpp_function) {
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_index();
|
||||
}
|
||||
_cpp_function_cb_s[index]=[cpp_function,index](const std::string& str)->void {
|
||||
cpp_function(str);
|
||||
FuncPoolManager::get_instance().release_cb_s_index(index);
|
||||
};
|
||||
return _fps_cb_s[index];
|
||||
}
|
||||
|
||||
CB_I_S _wrapper_callonce_cpp_function(const std::function<void(int,const std::string&)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_i_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_i_s_index();
|
||||
}
|
||||
_cpp_function_cb_i_s[index]=[cpp_function,index](int code,const std::string& str)->void {
|
||||
cpp_function(code,str);
|
||||
FuncPoolManager::get_instance().release_cb_i_s_index(index);
|
||||
};
|
||||
return _fps_cb_i_s[index];
|
||||
}
|
||||
|
||||
CB_S_I_S_S _wrapper_callonce_cpp_function(const std::function<void(const std::string&,int,const std::string&,const std::string&)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_i_s_s_index();
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_i_s_s_index();
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s[index]=[cpp_function,index](const std::string& operationID,int code,const std::string& str,const std::string& str2)->void {
|
||||
cpp_function(operationID,code,str,str2);
|
||||
FuncPoolManager::get_instance().release_cb_s_i_s_s_index(index);
|
||||
};
|
||||
return _fps_cb_s_i_s_s[index];
|
||||
}
|
||||
|
||||
CB_S_I_S_S_I _wrapper_callonce_cpp_function(const std::function<void(const std::string&,int,const std::string&,const std::string&,int)>& cpp_function)
|
||||
{
|
||||
int index=FuncPoolManager::get_instance().get_cb_s_i_s_s_i_index();
|
||||
// while loop util get a available index
|
||||
while(index<0){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_FOR_GET_INDEX));
|
||||
index=FuncPoolManager::get_instance().get_cb_s_i_s_s_i_index();
|
||||
}
|
||||
_cpp_function_cb_s_i_s_s_i[index]=[cpp_function,index](const std::string& operationID,int code,const std::string& str,const std::string& str2,int c_int)->void {
|
||||
cpp_function(operationID,code,str,str2,c_int);
|
||||
FuncPoolManager::get_instance().release_cb_s_i_s_s_i_index(index);
|
||||
};
|
||||
return _fps_cb_s_i_s_s_i[index];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user