1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#ifndef STREAM_SOCKET_H
17f857971dSopenharmony_ci#define STREAM_SOCKET_H
18f857971dSopenharmony_ci
19f857971dSopenharmony_ci#include <atomic>
20f857971dSopenharmony_ci#include <string>
21f857971dSopenharmony_ci#include <functional>
22f857971dSopenharmony_ci
23f857971dSopenharmony_ci#include <fcntl.h>
24f857971dSopenharmony_ci#include <sys/epoll.h>
25f857971dSopenharmony_ci#include <sys/socket.h>
26f857971dSopenharmony_ci#include <sys/types.h>
27f857971dSopenharmony_ci#include <sys/un.h>
28f857971dSopenharmony_ci#include <unistd.h>
29f857971dSopenharmony_ci
30f857971dSopenharmony_ci#include "nocopyable.h"
31f857971dSopenharmony_ci
32f857971dSopenharmony_ci#include "circle_stream_buffer.h"
33f857971dSopenharmony_ci#include "net_packet.h"
34f857971dSopenharmony_ci
35f857971dSopenharmony_cinamespace OHOS {
36f857971dSopenharmony_cinamespace Msdp {
37f857971dSopenharmony_cinamespace DeviceStatus {
38f857971dSopenharmony_ciclass StreamSocket {
39f857971dSopenharmony_cipublic:
40f857971dSopenharmony_ci    StreamSocket();
41f857971dSopenharmony_ci    DISALLOW_COPY_AND_MOVE(StreamSocket);
42f857971dSopenharmony_ci    virtual ~StreamSocket();
43f857971dSopenharmony_ci    using PacketCallBackFun = std::function<void(NetPacket&)>;
44f857971dSopenharmony_ci
45f857971dSopenharmony_ci    int32_t EpollCreate();
46f857971dSopenharmony_ci    int32_t EpollCtl(int32_t fd, int32_t op, struct epoll_event &event);
47f857971dSopenharmony_ci    int32_t EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events);
48f857971dSopenharmony_ci    void OnReadPackets(CircleStreamBuffer &buf, PacketCallBackFun callbackFun);
49f857971dSopenharmony_ci    void EpollClose();
50f857971dSopenharmony_ci    void Close();
51f857971dSopenharmony_ci
52f857971dSopenharmony_ci    int32_t GetFd() const
53f857971dSopenharmony_ci    {
54f857971dSopenharmony_ci        return fd_;
55f857971dSopenharmony_ci    }
56f857971dSopenharmony_ci
57f857971dSopenharmony_ciprotected:
58f857971dSopenharmony_ci    int32_t fd_ { -1 };
59f857971dSopenharmony_ci    int32_t epollFd_ { -1 };
60f857971dSopenharmony_ci};
61f857971dSopenharmony_ci} // namespace DeviceStatus
62f857971dSopenharmony_ci} // namespace Msdp
63f857971dSopenharmony_ci} // namespace OHOS
64f857971dSopenharmony_ci#endif // STREAM_SOCKET_H