• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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  * Description: tcp connection.
15  * Author: renshuang
16  * Create: 2023-09-25
17  */
18 
19 #ifndef TCP_CONNECTION_H
20 #define TCP_CONNECTION_H
21 
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <thread>
26 #include <unordered_map>
27 #include <vector>
28 
29 #include "connection.h"
30 #include "channel.h"
31 #include "tcp_socket.h"
32 
33 namespace OHOS {
34 namespace CastEngine {
35 namespace CastEngineService {
36 class TcpConnection : public Connection, public Channel, public std::enable_shared_from_this<TcpConnection> {
37 public:
38     using Connection::channelRequest_;
39 
TcpConnection()40     TcpConnection() {};
41     ~TcpConnection() override;
42 
43     int StartConnection(const ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener) override;
44     int StartListen(const ChannelRequest &request, std::shared_ptr<IChannelListener> channelListener) override;
45     void CloseConnection() override;
46     bool Send(const uint8_t *buf, int bufLen) override;
47 
48 private:
49     void ConfigSocket();
50     void Connect();
51     void Receive(int socket);
52     void ReadLooper(int socket);
53     void AcceptVideoAndAudio();
54     void Accept();
55     void SetAudioConnection(int socket);
56     void HandleReceivedData(int socket);
57     uint32_t GetReceivedDataLength(uint8_t *header);
58     void HandleRemoteControlReceivedData(uint32_t dataLength, uint8_t *header, uint8_t *buf);
59 
60     static constexpr int RET_ERR = -1;
61     static constexpr int RET_OK = 0;
62     static constexpr int INVALID_SOCKET = -1;
63     static constexpr int STOP_RECEIVE = -2;
64     /*
65      * 数据包头长度固定为4
66      */
67     static constexpr unsigned int PACKET_HEADER_LEN = 4;
68     /*
69      * TCP每次收数据包的最大长度,超过则认为非法
70      */
71     static constexpr unsigned int ILLEGAL_LENGTH = 10 * 1024 * 1024;
72     /*
73      * 设置发送缓冲区大小为512K
74      */
75     static constexpr unsigned int SOCKET_SEND_BUFFER_SIZE = 512 * 1024;
76     /*
77      * 设置接收缓冲区大小为 10M
78      */
79     static constexpr unsigned int SOCKET_RECV_BUFFER_SIZE = 10 * 1024 * 1024;
80     static constexpr int CONTROL_LENGTH_MASK = 0xFFFF;
81 
82     std::atomic<bool> isReceiving_{ false };
83     TcpSocket socket_;
84     // 连接的客户端套接字
85     int remoteSocket_{ INVALID_SOCKET };
86     // 音频通道
87     std::shared_ptr<TcpConnection> tcpAudioConn_{ nullptr };
88     std::mutex connectionMtx_;
89 };
90 } // namespace CastEngineService
91 } // namespace CastEngine
92 } // namespace OHOS
93 
94 #endif