2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FNetSock_SocketUtilityImpl.cpp
20 * @brief This is the implementation for the _SocketUtilityImpl class.
23 #include <arpa/inet.h>
24 #include <sys/select.h>
27 #include <FNetSockSocket.h>
28 #include <FNetSockSecureSocket.h>
29 #include <FNetSockSocketUtility.h>
30 #include <FBaseSysLog.h>
31 #include "FNetSock_SocketImpl.h"
32 #include "FNetSock_SecureSocketImpl.h"
33 #include "FNetSock_SocketUtilityImpl.h"
34 #include "FNetSock_SocketInternalHelp.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Net;
41 namespace Tizen { namespace Net { namespace Sockets
44 _SocketUtilityImpl::_SocketUtilityImpl(void)
45 : __isConstructed(false)
49 _SocketUtilityImpl::~_SocketUtilityImpl(void)
54 _SocketUtilityImpl::Construct(void)
56 SysAssertf(__isConstructed == false, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
58 __isConstructed = true;
64 _SocketUtilityImpl::Select(IList* pCheckRead, IList* pCheckWrite, IList* pCheckError, int microSeconds)
66 SysAssertf(__isConstructed == true, "Not yet constructed. Construct() should be called before use.");
68 SysTryReturnResult(NID_NET_SOCK, (pCheckRead != null) || (pCheckWrite != null) || (pCheckError != null), E_INVALID_ARG, "All three descriptor parameters were null.");
79 struct timeval timeout;
85 if (pCheckRead != null)
87 int count = pCheckRead->GetCount();
88 for (int i = 0; i < count; i++)
90 Socket* pSocket = dynamic_cast <Socket*>(pCheckRead->GetAt(i));
91 SecureSocket* pSecSocket = dynamic_cast <SecureSocket*>(pCheckRead->GetAt(i));
92 SysTryReturnResult(NID_NET_SOCK, (pSocket != null) || (pSecSocket != null), E_INVALID_ARG, "The given collection contains an element of incompatible class type.");
95 r = IListToSocketFdSet(*pCheckRead, &readFds, nFds);
96 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to pCheckRead IListToSocketFdSet.");
98 if (pCheckWrite != null)
100 int count = pCheckWrite->GetCount();
101 for (int i = 0; i < count; i++)
103 Socket* pSocket = dynamic_cast <Socket*>(pCheckWrite->GetAt(i));
104 SecureSocket* pSecSocket = dynamic_cast <SecureSocket*>(pCheckWrite->GetAt(i));
105 SysTryReturnResult(NID_NET_SOCK, (pSocket != null) || (pSecSocket != null), E_INVALID_ARG, "The given collection contains an element of incompatible class type.");
108 r = IListToSocketFdSet(*pCheckWrite, &writeFds, nFds);
109 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to pCheckWrite IListToSocketFdSet.");
111 if (pCheckError != null)
113 int count = pCheckError->GetCount();
114 for (int i = 0; i < count; i++)
116 Socket* pSocket = dynamic_cast <Socket*>(pCheckError->GetAt(i));
117 SecureSocket* pSecSocket = dynamic_cast <SecureSocket*>(pCheckError->GetAt(i));
118 SysTryReturnResult(NID_NET_SOCK, (pSocket != null) || (pSecSocket != null), E_INVALID_ARG, "The given collection contains an element of incompatible class type.");
121 r = IListToSocketFdSet(*pCheckError, &errorFds, nFds);
122 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to pCheckError IListToSocketFdSet.");
125 if (microSeconds < _MIN_SELECT_TIMEOUT) // If timeout value is lesser than MINIMUN value - set to minimum value by force
127 microSeconds = _MIN_SELECT_TIMEOUT;
130 timeout.tv_sec = microSeconds / 1000000;
131 timeout.tv_usec = microSeconds % 1000000;
133 ret = select(nFds + 1, &readFds, &writeFds, &errorFds, &timeout);
136 // Error code mapping
139 case EBADF: // An invalid file descriptor was given in one of the sets. (Perhaps a file descriptor that was already closed, or one on which an error has occurred.)
140 r = E_INVALID_SOCKET;
143 case EINTR: // A signal was caught.
147 case EINVAL: // nFds is negative or the value contained within timeout is invalid.
151 case ENOMEM: // Unable to allocate memory for internal tables.
159 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to SocketUtility Select().");
164 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to SocketUtility Select().");
167 if (pCheckRead != null)
169 SysLog(NID_NET_SOCK, "pCheckRead SocketFdSetToIList()");
170 r = SocketFdSetToIList(&readFds, *pCheckRead);
171 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to readFds SocketFdSetToIList.");
174 if (pCheckWrite != null)
176 SysLog(NID_NET_SOCK, "pCheckWrite SocketFdSetToIList()");
177 r = SocketFdSetToIList(&writeFds, *pCheckWrite);
178 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to writeFds SocketFdSetToIList.");
181 if (pCheckError != null)
183 SysLog(NID_NET_SOCK, "pCheckError SocketFdSetToIList()");
184 r = SocketFdSetToIList(&errorFds, *pCheckError);
185 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to errorFds SocketFdSetToIList.");
188 SysLog(NID_NET_SOCK, "SocketUtility Select() is succeed.");
194 _SocketUtilityImpl::HtoNS(unsigned short hostShort)
198 SysAssertf(__isConstructed != false, "Not Constructed yet or Invocation of the method is not right in this state.");
200 return (uint16_t) htons((uint16_t) hostShort);
204 _SocketUtilityImpl::HtoNL(unsigned long hostLong)
208 SysAssertf(__isConstructed != false, "Not Constructed yet or Invocation of the method is not right in this state.");
210 return (uint32_t) htonl((uint32_t) hostLong);
214 _SocketUtilityImpl::NtoHS(unsigned short netShort)
218 SysAssertf(__isConstructed != false, "Not Constructed yet or Invocation of the method is not right in this state.");
220 return (uint16_t) ntohs((uint16_t) netShort);
224 _SocketUtilityImpl::NtoHL(unsigned long netLong)
228 SysAssertf(__isConstructed != false, "Not Constructed yet or Invocation of the method is not right in this state.");
230 return (uint32_t) ntohl((uint32_t) netLong);
234 _SocketUtilityImpl::IListToSocketFdSet(const Tizen::Base::Collection::IList& iList, fd_set* pSet, int& nFds)
236 result r = E_SUCCESS;
241 Socket* pSocket = null;
242 _SocketImpl* pSocketImpl = null;
243 SecureSocket* pSecSocket = null;
244 _SecureSocketImpl* pSecSocketImpl = null;
248 int sockFd = INVALID_HANDLE;
252 count = iList.GetCount();
256 pList = iList.GetItemsN(index, count);
258 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "pList is a empty.");
261 for (int i = 0; i < count; i++)
263 pObj = pList->GetAt(i);
265 pSocket = dynamic_cast <Socket*>(pObj);
266 pSecSocket = dynamic_cast <SecureSocket*>(pObj);
267 SysTryReturnResult(NID_NET_SOCK, (pSocket != null) || (pSecSocket != null), E_SYSTEM, "The socket is null.");
271 pSocketImpl = _SocketImpl::GetInstance(*pSocket);
272 sockFd = pSocketImpl->GetSocketFd();
274 (void) FD_SET(sockFd, pSet);
277 if (pSecSocket != null)
279 pSecSocketImpl = _SecureSocketImpl::GetInstance(*pSecSocket);
280 sockFd = pSecSocketImpl->GetSecureSocketFd();
282 (void) FD_SET(sockFd, pSet);
296 _SocketUtilityImpl::SocketFdSetToIList(const fd_set* pSet, Tizen::Base::Collection::IList& iList)
298 result r = E_SUCCESS;
303 Socket* pSocket = null;
304 _SocketImpl* pSocketImpl = null;
305 SecureSocket* pSecSocket = null;
306 _SecureSocketImpl* pSecSocketImpl = null;
310 int sockFd = INVALID_HANDLE;
312 count = iList.GetCount();
316 pList = iList.GetItemsN(index, count);
318 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "pList is a empty.");
321 for (int i = 0; i < count; i++)
323 pObj = pList->GetAt(i);
325 pSocket = dynamic_cast <Socket*>(pObj);
326 pSecSocket = dynamic_cast <SecureSocket*>(pObj);
327 SysTryReturnResult(NID_NET_SOCK, (pSocket != null) || (pSecSocket != null), E_SYSTEM, "The socket is null.");
331 pSocketImpl = _SocketImpl::GetInstance(*pSocket);
332 sockFd = pSocketImpl->GetSocketFd();
333 if (!(FD_ISSET(sockFd, pSet)))
335 SysLog(NID_NET_SOCK, "Remove pObj in iList.");
336 r = iList.Remove(*pObj);
337 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "iList is a empty.");
341 if (pSecSocket != null)
343 pSecSocketImpl = _SecureSocketImpl::GetInstance(*pSecSocket);
344 sockFd = pSecSocketImpl->GetSecureSocketFd();
345 if (!(FD_ISSET(sockFd, pSet)))
347 SysLog(NID_NET_SOCK, "Remove pObj in iList.");
348 r = iList.Remove(*pObj);
349 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "iList is a empty.");
364 _SocketUtilityImpl::GetInstance(SocketUtility& socketUtility)
366 return socketUtility.__pSocketUtilityImpl;
369 const _SocketUtilityImpl*
370 _SocketUtilityImpl::GetInstance(const SocketUtility& socketUtility)
372 return socketUtility.__pSocketUtilityImpl;
375 } } } // Tizen::Net::Sockets