fix:modify socket name and listener

Signed-off-by: yueyan <yueyan8@huawei.com>
This commit is contained in:
yueyan 2024-02-21 09:53:32 +08:00 committed by yue
parent d9b2b289b5
commit 5f1b892bf2
11 changed files with 188 additions and 110 deletions

View File

@ -74,8 +74,11 @@ typedef struct {
* @brief Called when a socket is bind.
*
* This callback is invoked to verify the socket or initialize resources related to the socket.
* When the connection is successful, this callback be called on the server side.
* The server side refers to the side that called {@Listen} function.
*
* @param socket Indicates the unique socket fd; socket fd = <b>0</b> if the bind is failed.
* @param socket Indicates the unique socket fd.
* @param info Indicates the information of peer socket.
* @since 2.0
* @version 2.0
*/
@ -153,7 +156,8 @@ typedef struct {
*
* @param socket Indicates the unique socket fd.
* @param event Indicates the type of QoS state change.
* @param qos[] Indicates the QoS status that we can provide.
* @param qos Indicates the QoS status that we can provide.
* @param qosCount Indicates the number of the third parameter <b>qos</b>.
* @since 2.0
* @version 2.0
*/
@ -163,12 +167,13 @@ typedef struct {
/**
* @brief Creates a socket.
*
* A maximum of 10 socket can be created.
* A maximum of 15 socket can be created.
*
* @param info Indicates the description of the socket structure.
* It is the unique identifier of the upper-layer service. The value cannot be empty or exceed 64 characters.
*
* @return Returns <b>socket fd</b> if the socket creation is successful; returns <b>-1</b> otherwise.
* @return Returns <b>socket fd</b> if the socket creation is successful;
* returns an error code less than zero otherwise.
* @since 2.0
* @version 2.0
*/
@ -179,9 +184,11 @@ int32_t Socket(SocketInfo info);
*
* @param socket Indicates the the unique socket fd.
* @param qos Indicates the QoS requirements for socket. The value cannot be empty.
* @param qosCount Indicates the number of the second parameter <b>qos</b>.
* @param listener Indicates the pointer to the socket callback.
*
* @return Returns <b>0</b> if the listen creation is successful; returns <b>-1</b> otherwise.
* @return Returns <b>SOFTBUS_OK</b> if the listen creation is successful;
* returns an error code less than zero otherwise.
* @since 2.0
* @version 2.0
*/
@ -190,16 +197,17 @@ int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISock
/**
* @brief Binds a socket, which is called by client.
*
* {@link OnBind} is invoked to return whether the socket is successfully bind.
* Data can be transmitted only after the socket is successfully bind.
* When the connection is successful, this function return <b>SOFTBUS_OK</b> and
* {@link OnBind} be called on the server side.
*
* @param socket Indicates the the unique socket fd.
* @param qos Indicates the QoS requirements for socket. The value cannot be empty.
* @param qosCount Indicates the number of the second parameter <b>qos</b>.
* @param listener Indicates the pointer to the socket callback.
*
* @return Returns <b>SOFTBUS_TRANS_INVALID_PARAM</b> if invalid parameters are detected.
* @return Returns <b>INVALID_SOCKET</b> if the operation fails.
* @return Returns the socket fd (an integer greater than <b>0</b>) if the socket is bind;
* @return Returns <b>SOFTBUS_OK</b> if the socket is bind;
* returns an error code otherwise.
* @since 2.0
* @version 2.0
@ -303,7 +311,7 @@ void Shutdown(int32_t socket);
* @param peerNetworkId Indicates the pointer to the remote device ID.
* @param dataType Indicates the type of data.
* @param qos Indicates the expected quality of service.
* @param qosLen Indicates the number of qos
* @param qosCount Indicates the number of the fourth parameter <b>qos</b>.
*
* @return Returns no value.
* @since 2.0

View File

@ -70,7 +70,8 @@ typedef struct {
typedef struct {
bool isSocketListener;
ISessionListener session;
ISocketListener socket;
ISocketListener socketClient;
ISocketListener socketServer;
} SessionListenerAdapter;
typedef struct {
@ -162,7 +163,7 @@ int32_t ClientDeleteSocketSession(int32_t sessionId);
int32_t ClientAddSocketSession(const SessionParam *param, int32_t *sessionId, bool *isEnabled);
int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener);
int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer);
int32_t ClientIpcOpenSession(int32_t sessionId, const QosTV *qos, uint32_t qosCount, TransInfo *transInfo);
@ -170,7 +171,7 @@ int32_t ClientSetSocketState(int32_t socket, uint32_t maxIdleTimeout, SessionRol
int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionListenerAdapter *callbackAdapter);
int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter);
int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer);
int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo);

View File

@ -91,7 +91,7 @@ static int32_t GetSessionCallbackByChannelId(int32_t channelId, int32_t channelT
}
static int32_t GetSocketCallbackAdapterByChannelId(int32_t channelId, int32_t channelType, int32_t *sessionId,
SessionListenerAdapter *sessionCallback)
SessionListenerAdapter *sessionCallback, bool *isServer)
{
if ((channelId < 0) || (sessionId == NULL) || (sessionCallback == NULL)) {
TRANS_LOGE(TRANS_SDK, "Invalid param");
@ -103,7 +103,7 @@ static int32_t GetSocketCallbackAdapterByChannelId(int32_t channelId, int32_t ch
TRANS_LOGE(TRANS_SDK, "get sessionId failed, channelId=%{public}d", channelId);
return SOFTBUS_ERR;
}
ret = ClientGetSessionCallbackAdapterById(*sessionId, sessionCallback);
ret = ClientGetSessionCallbackAdapterById(*sessionId, sessionCallback, isServer);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "get socket callback failed");
return SOFTBUS_ERR;
@ -111,12 +111,8 @@ static int32_t GetSocketCallbackAdapterByChannelId(int32_t channelId, int32_t ch
return SOFTBUS_OK;
}
static int32_t TransOnBindSuccess(int32_t sessionId, const ISocketListener *socketCallback, bool isServer)
NO_SANITIZE("cfi") static int32_t TransOnServerBindSuccess(int32_t sessionId, const ISocketListener *socketCallback)
{
if (!isServer) {
return SOFTBUS_OK;
}
PeerSocketInfo info;
int32_t ret = ClientGetPeerSocketInfoById(sessionId, &info);
if (ret != SOFTBUS_OK) {
@ -130,7 +126,7 @@ static int32_t TransOnBindSuccess(int32_t sessionId, const ISocketListener *sock
}
(void)socketCallback->OnBind(sessionId, info);
TRANS_LOGI(TRANS_SDK, "OnBind success, client socket=%{public}d", sessionId);
TRANS_LOGI(TRANS_SDK, "OnBind success, server socket=%{public}d", sessionId);
return SOFTBUS_OK;
}
@ -169,7 +165,11 @@ NO_SANITIZE("cfi") int32_t TransOnSessionOpened(const char *sessionName, const C
}
if (sessionCallback.isSocketListener) {
ret = TransOnBindSuccess(sessionId, &sessionCallback.socket, channel->isServer);
if (!channel->isServer) {
TRANS_LOGI(TRANS_SDK, "OnBind success, client socket=%{public}d", sessionId);
return SOFTBUS_OK;
}
ret = TransOnServerBindSuccess(sessionId, &sessionCallback.socketServer);
if (ret != SOFTBUS_OK) {
(void)ClientDeleteSession(sessionId);
}
@ -191,8 +191,9 @@ NO_SANITIZE("cfi") int32_t TransOnSessionOpenFailed(int32_t channelId, int32_t c
TRANS_LOGI(TRANS_SDK, "channelId=%{public}d, channelType=%{public}d", channelId, channelType);
int32_t sessionId = INVALID_SESSION_ID;
SessionListenerAdapter sessionCallback;
bool isServer = false;
(void)memset_s(&sessionCallback, sizeof(SessionListenerAdapter), 0, sizeof(SessionListenerAdapter));
(void)GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback);
(void)GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback, &isServer);
TRANS_LOGI(TRANS_SDK, "trigger session open failed callback");
if (sessionCallback.session.OnSessionOpened != NULL) {
(void)sessionCallback.session.OnSessionOpened(sessionId, errCode);
@ -208,12 +209,16 @@ NO_SANITIZE("cfi") int32_t TransOnSessionClosed(int32_t channelId, int32_t chann
int32_t sessionId = INVALID_SESSION_ID;
int32_t ret;
SessionListenerAdapter sessionCallback;
bool isServer = false;
(void)memset_s(&sessionCallback, sizeof(SessionListenerAdapter), 0, sizeof(SessionListenerAdapter));
(void)GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback);
(void)GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback, &isServer);
TRANS_LOGI(TRANS_SDK, "trigger session close callback");
if (sessionCallback.socket.OnShutdown != NULL) {
sessionCallback.socket.OnShutdown(sessionId, reason);
if (sessionCallback.isSocketListener) {
ISocketListener *listener = isServer ? &sessionCallback.socketServer : &sessionCallback.socketClient;
if (listener->OnShutdown != NULL) {
listener->OnShutdown(sessionId, reason);
}
} else if (sessionCallback.session.OnSessionClosed != NULL) {
sessionCallback.session.OnSessionClosed(sessionId);
}
@ -249,25 +254,30 @@ NO_SANITIZE("cfi") int32_t TransOnDataReceived(int32_t channelId, int32_t channe
{
int32_t sessionId;
SessionListenerAdapter sessionCallback;
bool isServer = false;
(void)memset_s(&sessionCallback, sizeof(SessionListenerAdapter), 0, sizeof(SessionListenerAdapter));
int32_t ret = GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback);
int32_t ret = GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback, &isServer);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "get session callback failed");
return ret;
}
(void)ClientResetIdleTimeoutById(sessionId);
ISocketListener *listener = isServer ? &sessionCallback.socketServer : &sessionCallback.socketClient;
switch (type) {
case TRANS_SESSION_BYTES:
if (sessionCallback.socket.OnBytes != NULL) {
sessionCallback.socket.OnBytes(sessionId, data, len);
if (sessionCallback.isSocketListener) {
if (listener->OnBytes != NULL) {
listener->OnBytes(sessionId, data, len);
}
} else if (sessionCallback.session.OnBytesReceived != NULL) {
sessionCallback.session.OnBytesReceived(sessionId, data, len);
}
break;
case TRANS_SESSION_MESSAGE:
if (sessionCallback.socket.OnMessage != NULL) {
sessionCallback.socket.OnMessage(sessionId, data, len);
if (sessionCallback.isSocketListener) {
if (listener->OnMessage != NULL) {
listener->OnMessage(sessionId, data, len);
}
} else if (sessionCallback.session.OnMessageReceived != NULL) {
sessionCallback.session.OnMessageReceived(sessionId, data, len);
}
@ -298,16 +308,20 @@ NO_SANITIZE("cfi") int32_t TransOnOnStreamRecevied(int32_t channelId, int32_t ch
{
int32_t sessionId;
SessionListenerAdapter sessionCallback;
bool isServer = false;
(void)memset_s(&sessionCallback, sizeof(SessionListenerAdapter), 0, sizeof(SessionListenerAdapter));
int32_t ret = GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback);
int32_t ret = GetSocketCallbackAdapterByChannelId(channelId, channelType, &sessionId, &sessionCallback, &isServer);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_STREAM, "get session callback failed");
return ret;
}
(void)ClientResetIdleTimeoutById(sessionId);
if (sessionCallback.socket.OnStream != NULL) {
sessionCallback.socket.OnStream(sessionId, data, ext, param);
if (sessionCallback.isSocketListener) {
ISocketListener *listener = isServer ? &sessionCallback.socketServer : &sessionCallback.socketClient;
if (listener->OnStream != NULL) {
listener->OnStream(sessionId, data, ext, param);
}
return SOFTBUS_OK;
}

View File

@ -195,7 +195,8 @@ NO_SANITIZE("cfi") static DestroySessionInfo *CreateDestroySessionNode(SessionIn
destroyNode->channelId = sessionNode->channelId;
destroyNode->channelType = sessionNode->channelType;
destroyNode->OnSessionClosed = server->listener.session.OnSessionClosed;
destroyNode->OnShutdown = server->listener.socket.OnShutdown;
destroyNode->OnShutdown = sessionNode->isServer ? server->listener.socketServer.OnShutdown :
server->listener.socketClient.OnShutdown;
return destroyNode;
}
@ -1714,12 +1715,18 @@ static SessionInfo *GetSocketExistSession(const SessionParam *param)
ClientSessionServer *serverNode = NULL;
SessionInfo *sessionInfo = NULL;
LIST_FOR_EACH_ENTRY(serverNode, &(g_clientSessionServerList->list), ClientSessionServer, node) {
// distributeddata module can create different socket by same name
// distributeddata module can create different socket of whether the SocketInfo is same or not
if ((strcmp(serverNode->sessionName, param->sessionName) != 0) || IsListEmpty(&serverNode->sessionList) ||
IsDistributedDataSession(param->sessionName)) {
continue;
}
LIST_FOR_EACH_ENTRY(sessionInfo, &(serverNode->sessionList), SessionInfo, node) {
if ((strcmp(sessionInfo->info.peerSessionName, param->peerSessionName) != 0) ||
(strcmp(sessionInfo->info.peerDeviceId, param->peerDeviceId) != 0) ||
(strcmp(sessionInfo->info.groupId, param->groupId) != 0) ||
(sessionInfo->info.flag != param->attr->dataType)) {
continue;
}
return sessionInfo;
}
}
@ -1822,18 +1829,16 @@ int32_t ClientAddSocketSession(const SessionParam *param, int32_t *sessionId, bo
return SOFTBUS_OK;
}
int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener)
int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *listener, bool isServer)
{
if ((sessionId < 0) || listener == NULL) {
TRANS_LOGE(TRANS_SDK, "Invalid param");
return SOFTBUS_INVALID_PARAM;
}
if (g_clientSessionServerList == NULL) {
TRANS_LOGE(TRANS_SDK, "not init");
return SOFTBUS_TRANS_SESSION_SERVER_NOINIT;
}
if (SoftBusMutexLock(&(g_clientSessionServerList->lock)) != 0) {
TRANS_LOGE(TRANS_SDK, "lock failed");
return SOFTBUS_LOCK_ERR;
@ -1841,7 +1846,6 @@ int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *l
ClientSessionServer *serverNode = NULL;
SessionInfo *sessionNode = NULL;
int32_t ret = GetSessionById(sessionId, &serverNode, &sessionNode);
if (ret != SOFTBUS_OK) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
@ -1854,28 +1858,25 @@ int32_t ClientSetListenerBySessionId(int32_t sessionId, const ISocketListener *l
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
return SOFTBUS_TRANS_SOCKET_IN_USE;
}
ret = memcpy_s(&(serverNode->listener.socket), sizeof(ISocketListener), listener,
sizeof(ISocketListener));
serverNode->listener.isSocketListener = true;
ISocketListener *socketListener = isServer ? &(serverNode->listener.socketServer) :
&(serverNode->listener.socketClient);
ret = memcpy_s(socketListener, sizeof(ISocketListener), listener, sizeof(ISocketListener));
if (ret != EOK) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
TRANS_LOGE(TRANS_SDK, "memcpy failed");
return SOFTBUS_MEM_ERR;
}
// register file listener
if (serverNode->listener.socket.OnFile == NULL) {
serverNode->listener.isSocketListener = true;
if (socketListener->OnFile == NULL) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
return SOFTBUS_OK;
}
ret = TransSetSocketFileListener(serverNode->sessionName, serverNode->listener.socket.OnFile);
ret = TransSetSocketFileListener(serverNode->sessionName, socketListener->OnFile, isServer);
if (ret != SOFTBUS_OK) {
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
TRANS_LOGE(TRANS_SDK, "register socket file listener failed");
return ret;
}
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
return SOFTBUS_OK;
}
@ -2040,7 +2041,7 @@ int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionLi
return SOFTBUS_ERR;
}
int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter)
int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter, bool *isServer)
{
if (sessionId < 0 || callbackAdapter == NULL) {
TRANS_LOGE(TRANS_SDK, "Invalid param");
@ -2068,6 +2069,7 @@ int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAd
ret = memcpy_s(callbackAdapter, sizeof(SessionListenerAdapter), &serverNode->listener,
sizeof(SessionListenerAdapter));
*isServer = sessionNode->isServer;
(void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock));
if (ret != EOK) {
TRANS_LOGE(TRANS_SDK, "memcpy SessionListenerAdapter failed, socket=%{public}d", sessionId);
@ -2125,7 +2127,7 @@ bool IsSessionExceedLimit()
return false;
}
static void ClientCleanUpTimeoutSession(const ListNode *destroyList)
NO_SANITIZE("cfi") static void ClientCleanUpTimeoutSession(const ListNode *destroyList)
{
if (IsListEmpty(destroyList)) {
TRANS_LOGD(TRANS_SDK, "destroyList is empty.");

View File

@ -929,9 +929,9 @@ int32_t ClientAddSocket(const SocketInfo *info, int32_t *sessionId)
}
SessionParam param = {
.sessionName = info->name,
.peerSessionName = info->peerName,
.peerDeviceId = info->peerNetworkId,
.sessionName = info->name != NULL ? info->name : "",
.peerSessionName = info->peerName != NULL ? info->peerName : "",
.peerDeviceId = info->peerNetworkId != NULL ? info->peerNetworkId : "",
.groupId = "reserved",
.attr = tmpAttr,
};
@ -1006,7 +1006,7 @@ int32_t ClientBind(int32_t socket, const QosTV qos[], uint32_t qosCount, const I
return SOFTBUS_INVALID_PARAM;
}
int32_t ret = ClientSetListenerBySessionId(socket, listener);
int32_t ret = ClientSetListenerBySessionId(socket, listener, false);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "ClientBind set listener failed, ret=%{public}d", ret);
return ret;
@ -1056,7 +1056,7 @@ int32_t ClientListen(int32_t socket, const QosTV qos[], uint32_t qosCount, const
return SOFTBUS_INVALID_PARAM;
}
int32_t ret = ClientSetListenerBySessionId(socket, listener);
int32_t ret = ClientSetListenerBySessionId(socket, listener, true);
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_SDK, "set listener failed. ret=%{public}d", ret);
return ret;

View File

@ -117,9 +117,9 @@ static void ClearRecipientResources(FileRecipientInfo *info)
(void)SendFileTransResult(info->channelId, info->recvFileInfo.seq, SOFTBUS_ERR, IS_RECV_RESULT);
}
if (info->fileListener.fileCallback != NULL) {
if (info->fileListener.socketRecvCallback != NULL) {
FileEvent event = {.type = FILE_EVENT_RECV_ERROR};
info->fileListener.fileCallback(info->sessionId, &event);
info->fileListener.socketRecvCallback(info->sessionId, &event);
} else if (info->fileListener.recvListener.OnFileTransError != NULL) {
info->fileListener.recvListener.OnFileTransError(info->sessionId);
}
@ -1023,7 +1023,7 @@ static int32_t FileToFrame(SendListenerInfo *sendInfo, uint64_t frameNum,
TRANS_LOGE(TRANS_FILE, "send one frame failed");
goto EXIT_ERR;
}
if (sendInfo->fileListener.fileCallback != NULL) {
if (sendInfo->fileListener.socketSendCallback != NULL) {
FileEvent event = {
.type = FILE_EVENT_SEND_PROCESS,
.files = sendInfo->totalInfo.files,
@ -1032,7 +1032,7 @@ static int32_t FileToFrame(SendListenerInfo *sendInfo, uint64_t frameNum,
.bytesTotal = sendInfo->totalInfo.bytesTotal,
.UpdateRecvPath = NULL,
};
sendInfo->fileListener.fileCallback(sendInfo->sessionId, &event);
sendInfo->fileListener.socketSendCallback(sendInfo->sessionId, &event);
} else if (sendInfo->fileListener.sendListener.OnSendFileProcess != NULL) {
sendInfo->fileListener.sendListener.OnSendFileProcess(sendInfo->channelId, fileOffset, fileSize);
}
@ -1240,7 +1240,7 @@ static int32_t ProxyStartSendFile(const SendListenerInfo *sendInfo, const char *
sendInfo->channelId, sendInfo->result);
return SOFTBUS_ERR;
}
if (sendInfo->fileListener.fileCallback != NULL) {
if (sendInfo->fileListener.socketSendCallback != NULL) {
FileEvent event = {
.type = FILE_EVENT_SEND_FINISH,
.files = sendInfo->totalInfo.files,
@ -1249,7 +1249,7 @@ static int32_t ProxyStartSendFile(const SendListenerInfo *sendInfo, const char *
.bytesTotal = sendInfo->totalInfo.bytesTotal,
.UpdateRecvPath = NULL,
};
sendInfo->fileListener.fileCallback(sendInfo->sessionId, &event);
sendInfo->fileListener.socketSendCallback(sendInfo->sessionId, &event);
} else if (sendInfo->fileListener.sendListener.OnSendFileFinished != NULL) {
sendInfo->fileListener.sendListener.OnSendFileFinished(sendInfo->sessionId, dFileList[0]);
}
@ -1372,7 +1372,7 @@ int32_t ProxyChannelSendFile(int32_t channelId, const char *sFileList[],
if (ret != SOFTBUS_OK) {
TRANS_LOGE(TRANS_FILE, "proxy send file trans error");
if (sendInfo != NULL && sendInfo->fileListener.fileCallback != NULL) {
if (sendInfo != NULL && sendInfo->fileListener.socketSendCallback != NULL) {
FileEvent event = {
.type = FILE_EVENT_SEND_ERROR,
.files = sendInfo->totalInfo.files,
@ -1380,7 +1380,7 @@ int32_t ProxyChannelSendFile(int32_t channelId, const char *sFileList[],
.bytesProcessed = 0,
.bytesTotal = 0,
};
sendInfo->fileListener.fileCallback(sendInfo->sessionId, &event);
sendInfo->fileListener.socketSendCallback(sendInfo->sessionId, &event);
} else if (sendInfo != NULL && sendInfo->fileListener.sendListener.OnFileTransError != NULL) {
sendInfo->fileListener.sendListener.OnFileTransError(sendInfo->sessionId);
}
@ -1514,7 +1514,7 @@ void ClientDeleteRecvFileList(int32_t sessionId)
static int32_t UpdateFileReceivePath(int32_t sessionId, FileListener *fileListener)
{
if (fileListener->fileCallback == NULL) {
if (fileListener->socketRecvCallback == NULL) {
return SOFTBUS_OK;
}
FileEvent event = {
@ -1525,7 +1525,7 @@ static int32_t UpdateFileReceivePath(int32_t sessionId, FileListener *fileListen
.bytesTotal = 0,
.UpdateRecvPath = NULL,
};
fileListener->fileCallback(sessionId, &event);
fileListener->socketRecvCallback(sessionId, &event);
if (event.UpdateRecvPath == NULL) {
TRANS_LOGE(TRANS_FILE, "failed to obtain the file receive path");
return SOFTBUS_ERR;
@ -1719,7 +1719,7 @@ static int32_t CreateFileFromFrame(int32_t sessionId, int32_t channelId, const F
TRANS_LOGE(TRANS_FILE, "put to recv files failed. sessionId=%{public}u", recipient->sessionId);
goto EXIT_ERR;
}
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
const char *fileList[] = { file->filePath };
FileEvent event = {
.type = FILE_EVENT_RECV_START,
@ -1729,7 +1729,7 @@ static int32_t CreateFileFromFrame(int32_t sessionId, int32_t channelId, const F
.bytesTotal = file->fileSize,
.UpdateRecvPath = NULL,
};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnReceiveFileStarted != NULL) {
recipient->fileListener.recvListener.OnReceiveFileStarted(sessionId, file->filePath, 1);
}
@ -1744,9 +1744,9 @@ EXIT_ERR:
if (recipient->crc == APP_INFO_FILE_FEATURES_SUPPORT) {
(void)SendFileTransResult(channelId, 0, result, IS_RECV_RESULT);
}
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
FileEvent event = {.type = FILE_EVENT_RECV_ERROR};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnFileTransError != NULL) {
recipient->fileListener.recvListener.OnFileTransError(sessionId);
}
@ -1919,7 +1919,7 @@ static int32_t WriteFrameToFile(int32_t sessionId, const FileFrame *fileFrame)
goto EXIT_ERR;
}
fileInfo->timeOut = 0;
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
const char *fileList[] = { fileInfo->filePath };
FileEvent event = {
.type = FILE_EVENT_RECV_PROCESS,
@ -1929,7 +1929,7 @@ static int32_t WriteFrameToFile(int32_t sessionId, const FileFrame *fileFrame)
.bytesTotal = fileInfo->fileSize,
.UpdateRecvPath = NULL,
};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnReceiveFileProcess != NULL) {
recipient->fileListener.recvListener.OnReceiveFileProcess(sessionId, fileInfo->filePath,
fileInfo->fileOffset, fileInfo->fileSize);
@ -1956,9 +1956,9 @@ EXIT_ERR:
(void)SendFileTransResult(recipient->channelId, 0, result, IS_RECV_RESULT);
}
SetRecipientRecvState(recipient, TRANS_FILE_RECV_ERR_STATE);
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
FileEvent event = {.type = FILE_EVENT_RECV_ERROR};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnFileTransError != NULL) {
recipient->fileListener.recvListener.OnFileTransError(sessionId);
}
@ -2003,7 +2003,7 @@ static int32_t ProcessFileListData(int32_t sessionId, const FileFrame *frame)
goto EXIT_ERR;
}
SetRecipientRecvState(recipient, TRANS_FILE_RECV_IDLE_STATE);
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
const char *fileList[] = { absRecvPath };
FileEvent event = {
.type = FILE_EVENT_RECV_FINISH,
@ -2012,7 +2012,7 @@ static int32_t ProcessFileListData(int32_t sessionId, const FileFrame *frame)
.bytesProcessed = 0,
.bytesTotal = 0,
};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnReceiveFileFinished != NULL) {
recipient->fileListener.recvListener.OnReceiveFileFinished(sessionId, absRecvPath, fileCount);
}
@ -2021,9 +2021,9 @@ static int32_t ProcessFileListData(int32_t sessionId, const FileFrame *frame)
ret = SOFTBUS_OK;
EXIT_ERR:
if (ret != SOFTBUS_OK) {
if (recipient->fileListener.fileCallback != NULL) {
if (recipient->fileListener.socketRecvCallback != NULL) {
FileEvent event = {.type = FILE_EVENT_RECV_ERROR};
recipient->fileListener.fileCallback(sessionId, &event);
recipient->fileListener.socketRecvCallback(sessionId, &event);
} else if (recipient->fileListener.recvListener.OnFileTransError != NULL) {
recipient->fileListener.recvListener.OnFileTransError(sessionId);
}

View File

@ -31,7 +31,8 @@ typedef struct {
IFileSendListener sendListener;
IFileReceiveListener recvListener;
char rootDir[FILE_RECV_ROOT_DIR_SIZE_MAX];
SocketFileCallbackFunc fileCallback;
SocketFileCallbackFunc socketSendCallback;
SocketFileCallbackFunc socketRecvCallback;
} FileListener;
int TransFileInit(void);
@ -47,7 +48,7 @@ int32_t TransGetFileListener(const char *sessionName, FileListener *fileListener
void TransDeleteFileListener(const char *sessionName);
int32_t TransSetSocketFileListener(const char *sessionName, SocketFileCallbackFunc fileCallback);
int32_t TransSetSocketFileListener(const char *sessionName, SocketFileCallbackFunc fileCallback, bool isReceiver);
#ifdef __cplusplus
}
#endif

View File

@ -82,7 +82,7 @@ static void NotifySocketSendResult(int32_t socket, DFileMsgType msgType, const D
event.bytesProcessed = msgData->transferUpdate.bytesTransferred;
event.bytesTotal = msgData->transferUpdate.totalBytes;
event.UpdateRecvPath = NULL;
listener->fileCallback(socket, &event);
listener->socketSendCallback(socket, &event);
}
static void FileSendListener(int32_t dfileId, DFileMsgType msgType, const DFileMsg *msgData)
@ -118,11 +118,11 @@ static void FileSendListener(int32_t dfileId, DFileMsgType msgType, const DFileM
}
if (msgType == DFILE_ON_CONNECT_FAIL || msgType == DFILE_ON_FATAL_ERROR) {
if (fileListener.fileCallback != NULL) {
if (fileListener.socketSendCallback != NULL) {
FileEvent event;
(void)memset_s(&event, sizeof(FileEvent), 0, sizeof(FileEvent));
event.type = FILE_EVENT_SEND_ERROR;
fileListener.fileCallback(sessionId, &event);
fileListener.socketSendCallback(sessionId, &event);
} else if (fileListener.sendListener.OnFileTransError != NULL) {
fileListener.sendListener.OnFileTransError(sessionId);
}
@ -131,7 +131,7 @@ static void FileSendListener(int32_t dfileId, DFileMsgType msgType, const DFileM
return;
}
(void)g_udpChannelMgrCb->OnIdleTimeoutReset(sessionId);
if (fileListener.fileCallback != NULL) {
if (fileListener.socketSendCallback != NULL) {
NotifySocketSendResult(sessionId, msgType, msgData, &fileListener);
} else {
NotifySendResult(sessionId, msgType, msgData, &fileListener);
@ -202,7 +202,7 @@ static void NotifySocketRecvResult(int32_t socket, DFileMsgType msgType, const D
event.bytesProcessed = msgData->transferUpdate.bytesTransferred;
event.bytesTotal = msgData->transferUpdate.totalBytes;
event.UpdateRecvPath = NULL;
listener->fileCallback(socket, &event);
listener->socketRecvCallback(socket, &event);
}
static void FileReceiveListener(int32_t dfileId, DFileMsgType msgType, const DFileMsg *msgData)
@ -232,11 +232,11 @@ static void FileReceiveListener(int32_t dfileId, DFileMsgType msgType, const DFi
return;
}
if (msgType == DFILE_ON_CONNECT_FAIL || msgType == DFILE_ON_FATAL_ERROR) {
if (fileListener.fileCallback != NULL) {
if (fileListener.socketRecvCallback != NULL) {
FileEvent event;
(void)memset_s(&event, sizeof(FileEvent), 0, sizeof(FileEvent));
event.type = FILE_EVENT_RECV_ERROR;
fileListener.fileCallback(sessionId, &event);
fileListener.socketRecvCallback(sessionId, &event);
} else if (fileListener.recvListener.OnFileTransError != NULL) {
fileListener.recvListener.OnFileTransError(sessionId);
}
@ -244,7 +244,7 @@ static void FileReceiveListener(int32_t dfileId, DFileMsgType msgType, const DFi
return;
}
(void)g_udpChannelMgrCb->OnIdleTimeoutReset(sessionId);
if (fileListener.fileCallback != NULL) {
if (fileListener.socketRecvCallback != NULL) {
NotifySocketRecvResult(sessionId, msgType, msgData, &fileListener);
} else {
NotifyRecvResult(sessionId, msgType, msgData, &fileListener);
@ -259,11 +259,11 @@ static int32_t UpdateFileRecvPath(int32_t channelId, FileListener *fileListener,
return SOFTBUS_ERR;
}
if (fileListener->fileCallback != NULL) {
if (fileListener->socketRecvCallback != NULL) {
FileEvent event;
(void)memset_s(&event, sizeof(FileEvent), 0, sizeof(FileEvent));
event.type = FILE_EVENT_RECV_UPDATE_PATH;
fileListener->fileCallback(sessionId, &event);
fileListener->socketRecvCallback(sessionId, &event);
if (event.UpdateRecvPath == NULL) {
TRANS_LOGE(TRANS_SDK, "UpdateRecvPath is null");
return SOFTBUS_ERR;

View File

@ -17,6 +17,7 @@
#include <securec.h>
#include "anonymizer.h"
#include "softbus_adapter_mem.h"
#include "softbus_adapter_thread.h"
#include "softbus_def.h"
@ -161,7 +162,33 @@ int32_t TransSetFileSendListener(const char *sessionName, const IFileSendListene
return SOFTBUS_OK;
}
int32_t TransSetSocketFileListener(const char *sessionName, SocketFileCallbackFunc fileCallback)
static int32_t TransAddNewSocketFileListener(const char *sessionName, SocketFileCallbackFunc fileCallback,
bool isReceiver)
{
if (sessionName == NULL || fileCallback == NULL) {
TRANS_LOGE(TRANS_SDK, "invalid param.");
return SOFTBUS_INVALID_PARAM;
}
FileListener *listener = (FileListener *)SoftBusCalloc(sizeof(FileListener));
if (listener == NULL) {
TRANS_LOGE(TRANS_SDK, "file send listener calloc failed");
return SOFTBUS_MALLOC_ERR;
}
if (strcpy_s(listener->mySessionName, SESSION_NAME_SIZE_MAX, sessionName) != EOK) {
TRANS_LOGE(TRANS_SDK, "file node copy failed.");
SoftBusFree(listener);
return SOFTBUS_STRCPY_ERR;
}
if (isReceiver) {
listener->socketRecvCallback = fileCallback;
} else {
listener->socketSendCallback = fileCallback;
}
ListAdd(&(g_fileListener->list), &(listener->node));
return SOFTBUS_OK;
}
int32_t TransSetSocketFileListener(const char *sessionName, SocketFileCallbackFunc fileCallback, bool isReceiver)
{
if (sessionName == NULL || fileCallback == NULL) {
TRANS_LOGE(TRANS_SDK, "[client] invalid param.");
@ -185,27 +212,28 @@ int32_t TransSetSocketFileListener(const char *sessionName, SocketFileCallbackFu
}
}
if (exist) {
fileNode->fileCallback = fileCallback;
if (isReceiver) {
fileNode->socketRecvCallback = fileCallback;
} else {
fileNode->socketSendCallback = fileCallback;
}
(void)SoftBusMutexUnlock(&(g_fileListener->lock));
TRANS_LOGI(TRANS_SDK, "update file callback of socket");
return SOFTBUS_OK;
}
fileNode = (FileListener *)SoftBusCalloc(sizeof(FileListener));
if (fileNode == NULL) {
int32_t ret = TransAddNewSocketFileListener(sessionName, fileCallback, isReceiver);
if (ret != SOFTBUS_OK) {
(void)SoftBusMutexUnlock(&(g_fileListener->lock));
TRANS_LOGE(TRANS_SDK, "file send listener calloc failed");
return SOFTBUS_MALLOC_ERR;
TRANS_LOGE(TRANS_SDK, "failed to add new socket file listener");
return ret;
}
if (strcpy_s(fileNode->mySessionName, SESSION_NAME_SIZE_MAX, sessionName) != EOK) {
TRANS_LOGE(TRANS_SDK, "file node copy failed.");
SoftBusFree(fileNode);
(void)SoftBusMutexUnlock(&(g_fileListener->lock));
return SOFTBUS_ERR;
}
fileNode->fileCallback = fileCallback;
ListAdd(&(g_fileListener->list), &(fileNode->node));
(void)SoftBusMutexUnlock(&(g_fileListener->lock));
TRANS_LOGI(TRANS_SDK, "set socket file listener ok, sessionName=%{public}s", sessionName);
char *tmpName = NULL;
Anonymize(sessionName, &tmpName);
TRANS_LOGI(TRANS_SDK, "set socket file listener ok, sessionName=%{public}s, isReceiver=%{public}d",
tmpName, isReceiver);
AnonymizeFree(tmpName);
return SOFTBUS_OK;
}

View File

@ -1776,7 +1776,7 @@ HWTEST_F(ClientTransProxyFileManagerTest, ClinetTransProxyUpdateFileReceivePathT
int32_t sessionId = TEST_SESSION_ID;
FileListener *fileListener = (FileListener *)SoftBusCalloc(sizeof(FileListener));
fileListener->fileCallback = NULL;
fileListener->socketRecvCallback = NULL;
int ret = UpdateFileReceivePath(sessionId, fileListener);
EXPECT_EQ(SOFTBUS_OK, ret);

View File

@ -742,18 +742,42 @@ HWTEST_F(TransSdkFileTest, TransFileTest013, TestSize.Level0)
*/
HWTEST_F(TransSdkFileTest, TransFileTest014, TestSize.Level0)
{
int32_t ret = TransSetSocketFileListener(nullptr, nullptr);
int32_t ret = TransSetSocketFileListener(nullptr, nullptr, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransSetSocketFileListener(g_mySessionName, nullptr);
ret = TransSetSocketFileListener(g_mySessionName, nullptr, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransSetSocketFileListener(nullptr, SocketFileCallbackFuncTest);
ret = TransSetSocketFileListener(nullptr, SocketFileCallbackFuncTest, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransFileInit();
EXPECT_EQ(ret, SOFTBUS_OK);
ret = TransSetSocketFileListener(g_mySessionName, SocketFileCallbackFuncTest);
ret = TransSetSocketFileListener(g_mySessionName, SocketFileCallbackFuncTest, false);
ASSERT_EQ(ret, SOFTBUS_OK);
TransFileDeinit();
}
/**
* @tc.name: TransFileTest015
* @tc.desc: trans add new file callback of socket
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TransSdkFileTest, TransFileTest015, TestSize.Level0)
{
int32_t ret = TransFileInit();
EXPECT_EQ(ret, SOFTBUS_OK);
ret = TransAddNewSocketFileListener(nullptr, nullptr, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransAddNewSocketFileListener(g_mySessionName, nullptr, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransAddNewSocketFileListener(nullptr, SocketFileCallbackFuncTest, false);
ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
ret = TransAddNewSocketFileListener(g_mySessionName, SocketFileCallbackFuncTest, false);
ASSERT_EQ(ret, SOFTBUS_OK);
TransFileDeinit();
}