1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "nstackx_dfile_send.h"
17 #include "nstackx_dfile_mp.h"
18 #include "nstackx_dfile_transfer.h"
19 #include "nstackx_file_manager.h"
20 #include "nstackx_dfile_log.h"
21 #include "nstackx_dfile_dfx.h"
22 #include "securec.h"
23 #define TAG "nStackXDfileMp"
24
DFileSocketRecvSP(DFileSession * session)25 int32_t DFileSocketRecvSP(DFileSession *session)
26 {
27 struct sockaddr_in peerAddr;
28 socklen_t addrLen = sizeof(struct sockaddr_in);
29 uint8_t frame[NSTACKX_MAX_FRAME_SIZE] = {0};
30 int32_t ret;
31
32 (void)memset_s(&peerAddr, addrLen, 0, addrLen);
33 if (CapsTcp(session)) {
34 if (session->sessionType == DFILE_SESSION_TYPE_SERVER) {
35 if (session->acceptFlag == 0) {
36 ret = DFileAcceptSocket(session);
37 return ret;
38 } else {
39 ret = SocketRecvForTcp(session, session->recvBuffer, &peerAddr, &addrLen);
40 }
41 } else {
42 ret = SocketRecvForTcp(session, session->recvBuffer, &peerAddr, &addrLen);
43 }
44 if (ret == NSTACKX_PEER_CLOSE) {
45 return ret;
46 }
47 } else {
48 ret = SocketRecv(session->socket[0], frame, sizeof(frame), &peerAddr, &addrLen);
49 }
50 if (ret <= 0) {
51 if (ret != NSTACKX_EAGAIN) {
52 DFILE_LOGE(TAG, "socket recv failed");
53 return NSTACKX_EFAILED;
54 }
55 return NSTACKX_EAGAIN;
56 }
57 NSTACKX_ATOM_FETCH_INC(&session->totalRecvBlocks);
58 if (CapsTcp(session)) {
59 ret = DFileSessionHandleReadBuffer(session, session->recvBuffer, (size_t)session->recvLen, &peerAddr, 0);
60 session->recvLen = 0;
61 } else {
62 ret = DFileSessionHandleReadBuffer(session, frame, (size_t)ret, &peerAddr, 0);
63 }
64 if (ret != NSTACKX_EOK) {
65 DFILE_LOGE(TAG, "handle read buffer failed");
66 }
67 return ret;
68 }
69
TransSelectPeerInfo(DFileSession * session)70 PeerInfo *TransSelectPeerInfo(DFileSession *session)
71 {
72 PeerInfo *peerInfo = (PeerInfo *)ListGetFront(&session->peerInfoChain);
73 return peerInfo;
74 }
75
76 /* only for client */
ClientGetPeerInfoByTransId(DFileSession * session)77 PeerInfo *ClientGetPeerInfoByTransId(DFileSession *session)
78 {
79 PeerInfo *peerInfo = (PeerInfo *)ListGetFront(&session->peerInfoChain);
80 return peerInfo;
81 }
82
83 /* only for client */
ClientGetPeerInfoBySocketIndex(uint8_t socketIndex,const DFileSession * session)84 PeerInfo *ClientGetPeerInfoBySocketIndex(uint8_t socketIndex, const DFileSession *session)
85 {
86 PeerInfo *peerInfo = NULL;
87 List *pos = NULL;
88
89 LIST_FOR_EACH(pos, &session->peerInfoChain) {
90 peerInfo = (PeerInfo *)pos;
91 if (peerInfo->socketIndex == socketIndex) {
92 return peerInfo;
93 }
94 }
95 return NULL;
96 }
97
CreateSenderThread(DFileSession * session)98 int32_t CreateSenderThread(DFileSession *session)
99 {
100 SenderThreadPara *para = NULL;
101
102 para = malloc(sizeof(SenderThreadPara));
103 if (para == NULL) {
104 return NSTACKX_ENOMEM;
105 }
106 para->session = session;
107 para->socketIndex = 0;
108 if (PthreadCreate(&(session->senderTid[0]), NULL, DFileSenderHandle, para)) {
109 DFILE_LOGE(TAG, "Create sender thread 0 failed");
110 free(para);
111 return NSTACKX_EFAILED;
112 }
113
114 return NSTACKX_EOK;
115 }
116
RebuildFilelist(const char * files[],const char * remotePath[],uint32_t fileNum,DFileSession * session,DFileRebuildFileList * rebuildList)117 int32_t RebuildFilelist(const char *files[], const char *remotePath[], uint32_t fileNum,
118 DFileSession *session, DFileRebuildFileList *rebuildList)
119 {
120 if (session->allTaskCount >= NSTACKX_MAX_FILE_LIST_NUM) {
121 DFILE_LOGI(TAG, "more than %d send task", NSTACKX_MAX_FILE_LIST_NUM);
122 return NSTACKX_EFAILED;
123 }
124
125 (void)memset_s(rebuildList, sizeof(DFileRebuildFileList), 0, sizeof(DFileRebuildFileList));
126
127 rebuildList->transNum = 1;
128 for (uint32_t i = 0; i < fileNum; i++) {
129 rebuildList->files[i] = files[i];
130 if (remotePath) {
131 rebuildList->remotePath[i] = remotePath[i];
132 }
133 }
134 return NSTACKX_EOK;
135 }
136
InitOutboundQueueWait(DFileSession * session)137 int32_t InitOutboundQueueWait(DFileSession *session)
138 {
139 if (SemInit(&session->outboundQueueWait[0], 0, 0) != 0) {
140 return NSTACKX_EFAILED;
141 }
142
143 return NSTACKX_EOK;
144 }
145
DestroyOutboundQueueWait(DFileSession * session)146 void DestroyOutboundQueueWait(DFileSession *session)
147 {
148 SemDestroy(&session->outboundQueueWait[0]);
149 }
150
PostOutboundQueueWait(DFileSession * session)151 void PostOutboundQueueWait(DFileSession *session)
152 {
153 SemPost(&session->outboundQueueWait[0]);
154 }
155