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 <stdio.h>
17 #include <unistd.h>
18 
19 #include "client_trans_udp_stream_interface.h"
20 #include "securec.h"
21 #include "session.h"
22 #include "softbus_error_code.h"
23 
24 #define CHANNELID 1
25 #define CHANNELID2 2
26 #define PKGNAME   "test"
27 #define TWO_CLIENT_ARGC   3
28 #define FIRST_ARGV   1
29 #define SECOND_ARGV   2
30 #define SHORT_SLEEP   3
31 #define LONG_SLEEP    30
32 #define LOOP_ROUND    10
33 #define SESSION_KEY_LENGTH   32
34 #define STREAM_DATA_LENGTH   10
35 
SetStatus(int channelId,int status)36 void SetStatus(int channelId, int status)
37 {
38     printf("[client]:channelID:%d, status:%d\n", channelId, status);
39 }
40 
OnStreamReceived(int channelId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)41 void OnStreamReceived(int channelId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
42 {
43     printf("[client]:OnStreamReceived, len:%d, extLen:%d", data->bufLen, ext->bufLen);
44     printf("[client]:channelID:%d, streamBuf:%.*s\n", channelId, data->bufLen, data->buf);
45 }
46 
47 static IStreamListener g_callback = {
48     .OnStatusChange = SetStatus,
49     .OnStreamReceived = OnStreamReceived,
50 };
51 
ConstructVtpStreamOpenParam(VtpStreamOpenParam * p1,VtpStreamOpenParam * p2,char * argv[])52 int ConstructVtpStreamOpenParam(VtpStreamOpenParam *p1, VtpStreamOpenParam *p2, char *argv[])
53 {
54     int port = 0;
55     if (sscanf_s(argv[FIRST_ARGV], "%d", &port) <= 0) {
56         return SOFTBUS_INVALID_PARAM;
57     }
58 
59     int port2 = 0;
60     if (sscanf_s(argv[SECOND_ARGV], "%d", &port2) <= 0) {
61         return SOFTBUS_INVALID_PARAM;
62     }
63 
64     p1->pkgName = PKGNAME;
65     p1->myIp = "127.0.0.1";
66     p1->peerIp = "127.0.0.1";
67     p1->peerPort = port;
68     p1->type = RAW_STREAM;
69     p1->sessionKey = (uint8_t*)"abcdef@ghabcdefghabcdefghfgdabc";
70     p1->keyLen = SESSION_KEY_LENGTH;
71 
72     p2->pkgName = PKGNAME;
73     p2->myIp = "127.0.0.1";
74     p2->peerIp = "127.0.0.1";
75     p2->peerPort = port2;
76     p2->type = RAW_STREAM;
77     p2->sessionKey = (uint8_t*)"abcdef\0ghabcdefghabcdefghfgdabc";
78     p2->keyLen = SESSION_KEY_LENGTH;
79 
80     return SOFTBUS_OK;
81 }
82 
SendVtpStreamTest(VtpStreamOpenParam * p1,VtpStreamOpenParam * p2,const IStreamListener * callback)83 int SendVtpStreamTest(VtpStreamOpenParam *p1, VtpStreamOpenParam *p2, const IStreamListener *callback)
84 {
85     if (p1 == NULL || p2 == NULL) {
86         return SOFTBUS_INVALID_PARAM;
87     }
88 
89     int ret = StartVtpStreamChannelClient(CHANNELID, p1, callback);
90     if (ret != SOFTBUS_OK) {
91         return ret;
92     }
93     printf("[client]:StartChannelClient ret:%d\n", ret);
94 
95     ret = StartVtpStreamChannelClient(CHANNELID2, p2, callback);
96     if (ret != SOFTBUS_OK) {
97         return ret;
98     }
99     printf("[client]:StartChannelClient ret:%d\n", ret);
100 
101     sleep(SHORT_SLEEP);
102 
103     StreamData tmpData = {
104         "diudiudiu\0",
105         STREAM_DATA_LENGTH,
106     };
107 
108     StreamData tmpData2 = {
109         "oohoohooh\0",
110         STREAM_DATA_LENGTH,
111     };
112     StreamFrameInfo tmpf = {};
113 
114     for (int i  = 0; i < LOOP_ROUND; i++) {
115         ret = SendVtpStream(CHANNELID, &tmpData, NULL, &tmpf);
116         printf("[client]:DstreamSendStream1 ret:%d\n", ret);
117         ret = SendVtpStream(CHANNELID2, &tmpData2, NULL, &tmpf);
118         printf("[client]:DstreamSendStream2 ret:%d\n", ret);
119         sleep(LONG_SLEEP);
120     }
121 
122     CloseVtpStreamChannel(CHANNELID, PKGNAME);
123     CloseVtpStreamChannel(CHANNELID2, PKGNAME);
124     sleep(LONG_SLEEP);
125 
126     return SOFTBUS_OK;
127 }
128 
main(int argc,char * argv[])129 int main(int argc, char *argv[])
130 {
131     if (argc != TWO_CLIENT_ARGC) {
132         printf("[client]:Please input server sorcket to connect\n");
133         return SOFTBUS_INVALID_PARAM;
134     }
135     VtpStreamOpenParam p1;
136     VtpStreamOpenParam p2;
137     int ret = ConstructVtpStreamOpenParam(&p1, &p2, argv);
138     if (ret != SOFTBUS_OK) {
139         return ret;
140     }
141 
142     ret = SendVtpStreamTest(&p1, &p2, &g_callback);
143     if (ret != SOFTBUS_OK) {
144         return ret;
145     }
146 
147     return SOFTBUS_OK;
148 }
149