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 IMS_MEDIA_SOCKET_H 18 #define IMS_MEDIA_SOCKET_H 19 20 #include <ImsMediaDefine.h> 21 #include <ImsMediaCondition.h> 22 #include <ISocket.h> 23 #include <stdint.h> 24 #include <list> 25 #include <mutex> 26 27 class ImsMediaSocket : public ISocket 28 { 29 public: 30 static ImsMediaSocket* GetInstance( 31 uint32_t localPort, const char* peerIpAddress, uint32_t peerPort); 32 static void ReleaseInstance(ImsMediaSocket* node); 33 34 private: 35 ImsMediaSocket(); 36 virtual ~ImsMediaSocket(); 37 static void StartSocketMonitor(); 38 static void StopSocketMonitor(); 39 static void SocketMonitorThread(); 40 static uint32_t SetSocketFD(void* pReadFds, void* pWriteFds, void* pExceptFds); 41 static void ReadDataFromSocket(void* pReadfds); 42 bool isValidDscp(int32_t dscp); 43 int32_t convertDscpToTos(int32_t dscp); 44 45 public: 46 /** 47 * @brief Set the local ip address and port number 48 */ 49 virtual void SetLocalEndpoint(const char* ipAddress, const uint32_t port); 50 51 /** 52 * @brief Set the peer ip address and port number 53 */ 54 virtual void SetPeerEndpoint(const char* ipAddress, const uint32_t port); 55 virtual int GetLocalPort(); 56 virtual int GetPeerPort(); 57 virtual char* GetLocalIPAddress(); 58 virtual char* GetPeerIPAddress(); 59 60 /** 61 * @brief Add socket file descriptor to the list, stack into list is done only once when the 62 * socket reference counter is zero 63 * 64 * @param socketFd The unique socket file descriptor 65 * @return true Returns when the give argument is valid 66 * @return false Returns when the give argument is invalid 67 */ 68 virtual bool Open(int socketFd = -1); 69 70 /** 71 * @brief Add socket listener to the rx socket list for callback when the socket listener is not 72 * null, if the listener is null, remove the socket instance from the rx socket list 73 * 74 * @param listener The listener to decide add or remove from the rx socket list. 75 */ 76 virtual void Listen(ISocketListener* listener); 77 78 /** 79 * @brief Send data to registered socket 80 * 81 * @param pData The data array 82 * @param nDataSize The length of data 83 * @return int32_t The length of data which is sent successfully, return -1 when it is failed 84 * to send 85 */ 86 virtual int32_t SendTo(uint8_t* pData, uint32_t nDataSize); 87 88 /** 89 * @brief Receive data to the give buffer 90 * 91 * @param pData The data buffer to copy 92 * @param nBufferSize The size of buffer 93 * @return int32_t The length of data which is received successfully, return -1 when it is 94 * failed to received or has invalid arguments 95 */ 96 virtual int32_t ReceiveFrom(uint8_t* pData, uint32_t nBufferSize); 97 98 /** 99 * @brief Retrieve optional data from the socket 100 * 101 * @param type The type of the socket option to receive 102 * @param value The value to received, if method returns true, the value is valid 103 */ 104 virtual bool RetrieveOptionMsg(uint32_t type, int32_t& value); 105 106 /** 107 * @brief Remove the socket from the socket list 108 */ 109 virtual void Close(); 110 111 /** 112 * @brief Set the socket option, calls setsockopt 113 * 114 * @param nOption The option type defined as kSocketOption 115 * @param nOptionValue The value to set 116 * @return true Returns when the setsockopt returns valid status 117 * @return false Returns when the setsockopt returns -1 118 */ 119 virtual bool SetSocketOpt(kSocketOption nOption, int32_t nOptionValue); 120 int32_t GetSocketFd(); 121 ISocketListener* GetListener(); 122 123 private: 124 static std::list<ImsMediaSocket*> slistSocket; 125 static std::list<ImsMediaSocket*> slistRxSocket; 126 static int32_t sRxSocketCount; 127 static bool mSocketListUpdated; 128 static bool mTerminateMonitor; 129 static std::mutex sMutexRxSocket; 130 static std::mutex sMutexSocketList; 131 static ImsMediaCondition mConditionExit; 132 int32_t mSocketFd; 133 int32_t mRefCount; 134 ISocketListener* mListener; 135 kIpVersion mLocalIPVersion; 136 kIpVersion mPeerIPVersion; 137 char mLocalIP[MAX_IP_LEN]{}; 138 char mPeerIP[MAX_IP_LEN]{}; 139 uint32_t mLocalPort; 140 uint32_t mPeerPort; 141 bool mRemoteIpFiltering; 142 }; 143 144 #endif