WeChatFerry/SDK/rpc_client.cpp

264 lines
6.5 KiB
C++
Raw Normal View History

2022-08-07 20:50:49 +08:00
#include "rpc_client.h"
#include "sdk.h"
2022-08-07 16:21:28 +08:00
#include "util.h"
static RPC_WSTR pszStringBinding = NULL;
extern std::function<int(WxMessage_t)> g_cbReceiveTextMsg;
RPC_STATUS RpcConnectServer()
{
RPC_STATUS status = 0;
// Creates a string binding handle.
status = RpcStringBindingCompose(NULL, // UUID to bind to
reinterpret_cast<RPC_WSTR>((RPC_WSTR)L"ncalrpc"), // Use TCP/IP protocol
NULL, // TCP/IP network address to use
2022-08-07 21:17:04 +08:00
reinterpret_cast<RPC_WSTR>((RPC_WSTR)L"wcferry"), // TCP/IP port to use
2022-08-07 16:21:28 +08:00
NULL, // Protocol dependent network options to use
&pszStringBinding); // String binding output
if (status)
return status;
/* Validates the format of the string binding handle and converts it to a binding handle.
pszStringBinding: The string binding to validate
hSpyBinding: Put the result in the implicit binding(defined in the IDL file)
*/
status = RpcBindingFromStringBinding(pszStringBinding, &hSpyBinding);
return status;
}
RPC_STATUS RpcDisconnectServer()
{
RPC_STATUS status;
// Free the memory allocated by a string
status = RpcStringFree(&pszStringBinding);
// Releases binding handle resources and disconnects from the server
status = RpcBindingFree(&hSpyBinding);
return status;
}
2022-08-20 15:15:04 +08:00
int RpcEnableReceiveMsg()
2022-08-07 16:21:28 +08:00
{
unsigned long ulCode = 0;
RpcTryExcept
{
// 建立RPC通道让服务端能够调用客户端的回调函数。该接口会被服务端阻塞直到异常退出
client_EnableReceiveMsg();
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
2022-08-20 15:15:04 +08:00
printf("RpcEnableReceiveMsg exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
return 0;
}
int RpcDisableReceiveMsg()
{
unsigned long ulCode = 0;
RpcTryExcept
{
// UnHook Message receiving
client_DisableReceiveMsg();
}
2022-08-20 17:39:21 +08:00
RpcExcept(1)
2022-08-20 15:15:04 +08:00
{
ulCode = RpcExceptionCode();
printf("RpcDisableReceiveMsg exception 0x%lx = %ld\n", ulCode, ulCode);
2022-08-07 16:21:28 +08:00
}
RpcEndExcept;
return 0;
}
int RpcIsLogin()
{
int loginFlag = 0;
unsigned long ulCode = 0;
RpcTryExcept
{
// 查询登录状态
loginFlag = client_IsLogin();
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
2022-08-20 15:15:04 +08:00
printf("RpcIsLogin exception 0x%lx = %ld\n", ulCode, ulCode);
return -1;
2022-08-07 16:21:28 +08:00
}
RpcEndExcept;
return loginFlag;
}
2022-08-20 17:39:21 +08:00
int RpcSendTextMsg(const wchar_t *wxid, const wchar_t *msg, const wchar_t *atWxids)
2022-08-07 16:21:28 +08:00
{
int ret = 0;
unsigned long ulCode = 0;
2022-08-20 17:39:21 +08:00
RpcTryExcept { ret = client_SendTextMsg(wxid, msg, atWxids); }
2022-08-07 16:21:28 +08:00
RpcExcept(1)
{
ulCode = RpcExceptionCode();
2022-08-20 15:15:04 +08:00
printf("RpcSendTextMsg exception 0x%lx = %ld\n", ulCode, ulCode);
2022-08-07 16:21:28 +08:00
}
RpcEndExcept;
return ret;
}
int RpcSendImageMsg(const wchar_t *wxid, const wchar_t *path)
{
int ret = 0;
unsigned long ulCode = 0;
RpcTryExcept { ret = client_SendImageMsg(wxid, path); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
2022-08-20 15:15:04 +08:00
printf("RpcSendImageMsg exception 0x%lx = %ld\n", ulCode, ulCode);
2022-08-07 16:21:28 +08:00
}
RpcEndExcept;
return ret;
}
2022-08-07 20:50:49 +08:00
PPRpcIntBstrPair RpcGetMsgTypes(int *pNum)
2022-08-07 20:08:54 +08:00
{
2022-08-07 20:50:49 +08:00
int ret = 0;
unsigned long ulCode = 0;
PPRpcIntBstrPair ppRpcMsgTypes = NULL;
2022-08-07 20:08:54 +08:00
2022-08-07 20:50:49 +08:00
RpcTryExcept { ret = client_GetMsgTypes(pNum, &ppRpcMsgTypes); }
2022-08-07 20:08:54 +08:00
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("RpcGetMsgTypes exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
if (ret != 0) {
printf("GetMsgTypes Failed: %d\n", ret);
return NULL;
}
return ppRpcMsgTypes;
}
2022-08-07 20:50:49 +08:00
PPRpcContact RpcGetContacts(int *pNum)
{
int ret = 0;
unsigned long ulCode = 0;
PPRpcContact ppRpcContacts = NULL;
RpcTryExcept { ret = client_GetContacts(pNum, &ppRpcContacts); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("RpcGetContacts exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
if (ret != 0) {
printf("GetContacts Failed: %d\n", ret);
return NULL;
}
return ppRpcContacts;
}
2022-08-07 23:32:25 +08:00
BSTR *RpcGetDbNames(int *pNum)
{
int ret = 0;
unsigned long ulCode = 0;
BSTR *pBstr = NULL;
RpcTryExcept { ret = client_GetDbNames(pNum, &pBstr); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("RpcGetDbNames exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
if (ret != 0) {
printf("RpcGetDbNames Failed: %d\n", ret);
return NULL;
}
return pBstr;
}
2022-08-07 23:49:37 +08:00
PPRpcTables RpcGetDbTables(const wchar_t *db, int *pNum)
{
int ret = 0;
unsigned long ulCode = 0;
PPRpcTables ppRpcTables = NULL;
RpcTryExcept { ret = client_GetDbTables(db, pNum, &ppRpcTables); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("RpcGetDbTables exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
if (ret != 0) {
printf("RpcGetDbTables Failed: %d\n", ret);
return NULL;
}
return ppRpcTables;
}
2022-08-20 22:10:11 +08:00
PPPRpcSqlResult RpcExecDbQuery(const wchar_t *db, const wchar_t *sql, int *pRow, int *pCol)
{
int ret = 0;
unsigned long ulCode = 0;
PPPRpcSqlResult pppRpcSqlResult = NULL;
RpcTryExcept { ret = client_ExecDbQuery(db, sql, pRow, pCol, &pppRpcSqlResult); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("RpcExecDbQuery exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
if (ret != 0) {
printf("RpcExecDbQuery Failed: %d\n", ret);
return NULL;
}
return pppRpcSqlResult;
}
2022-08-21 08:45:16 +08:00
BOOL AcceptNewFriend(const wchar_t *v3, const wchar_t *v4)
{
BOOL ret = 0;
unsigned long ulCode = 0;
RpcTryExcept { ret = client_AcceptNewFriend(v3, v4); }
RpcExcept(1)
{
ulCode = RpcExceptionCode();
printf("AcceptNewFriend exception 0x%lx = %ld\n", ulCode, ulCode);
}
RpcEndExcept;
return ret;
}
2022-08-07 16:21:28 +08:00
int server_ReceiveMsg(RpcMessage_t rpcMsg)
{
WxMessage_t msg;
GetRpcMessage(&msg, rpcMsg);
try {
g_cbReceiveTextMsg(msg); // 调用接收消息回调
} catch (...) {
printf("callback error...\n");
}
return 0;
}