1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SOCKET_READER_NODE_H
18 #define SOCKET_READER_NODE_H
19 
20 #include <BaseNode.h>
21 #include <ISocket.h>
22 #include <mutex>
23 
24 class SocketReaderNode : public BaseNode, public ISocketListener
25 {
26 public:
27     SocketReaderNode(BaseSessionCallback* callback = nullptr);
28     virtual ~SocketReaderNode();
29     virtual kBaseNodeId GetNodeId();
30     virtual bool Prepare();
31     virtual ImsMediaResult Start();
32     virtual void Stop();
33     virtual bool IsRunTime();
34     virtual bool IsSourceNode();
35     void SetConfig(void* config);
36     virtual bool IsSameConfig(void* config);
37     virtual ImsMediaResult UpdateConfig(void* config);
38     virtual void OnReadDataFromSocket();
39 
40     /**
41      * @brief Set the local socket file descriptor
42      */
43     void SetLocalFd(int fd);
44 
45     /**
46      * @brief Set the local ip address and port number
47      */
48     void SetLocalAddress(const RtpAddress& address);
49 
50     /**
51      * @brief Set the peer ip address and port number
52      */
53     void SetPeerAddress(const RtpAddress& address);
54 
55     /**
56      * @brief Set the protocol type defined as kProtocolType
57      */
SetProtocolType(kProtocolType type)58     void SetProtocolType(kProtocolType type) { mProtocolType = type; }
59 
60     /* Get socket is opened */
IsSocketOpened()61     bool IsSocketOpened() { return mSocketOpened; }
62 
63 protected:
64     bool OpenSocket();
65     void CloseSocket();
66 
67     int mLocalFd;
68     kProtocolType mProtocolType;
69     ISocket* mSocket;
70     RtpAddress mLocalAddress;
71     RtpAddress mPeerAddress;
72     bool mSocketOpened;
73     std::mutex mMutex;
74     uint8_t mBuffer[DEFAULT_MTU];
75     bool mReceiveTtl;
76 };
77 
78 #endif