2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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_SocketImpl.cpp
20 * @brief This is the implementation for the _SocketImpl class.
23 #include <unique_ptr.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
29 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <sys/select.h>
37 #include <FNetNetTypes.h>
38 #include <FNetSockSocket.h>
39 #include <FNetNetEndPoint.h>
40 #include <FNetIpAddress.h>
41 #include <FNetIp4Address.h>
42 #include <FNetNetConnection.h>
43 #include <FNetNetConnectionManager.h>
44 #include <FBaseSysLog.h>
45 #include "FBase_StringConverter.h"
46 #include "FBaseRt_EventDispatcher.h"
47 #include "FApp_AppInfo.h"
48 #include "FNet_NetEndPointImpl.h"
49 #include "FNet_Ip4AddressImpl.h"
50 #include "FNet_NetConnectionManagerImpl.h"
51 #include "FNet_ManagedNetConnectionImpl.h"
52 #include "FNet_NetConnectionInfoImpl.h"
53 #include "FNet_NetConnectionImpl.h"
54 #include "FNetSock_SocketInternalHelp.h"
55 #include "FNetSock_SocketEventArg.h"
56 #include "FNetSock_SocketInternalHelp.h"
57 #include "FNetSock_LingerOptionImpl.h"
58 #include "FNetSock_IpMulticastRequestOptionImpl.h"
59 #include "FNetSock_SocketManagedNetConnectionEventListenerImpl.h"
60 #include "FNetSock_SocketCustomNetConnectionEventListenerImpl.h"
61 #include "FNetSock_SocketImpl.h"
64 using namespace Tizen::Base;
65 using namespace Tizen::Base::Runtime;
66 using namespace Tizen::Base::Collection;
67 using namespace Tizen::Base::Utility;
68 using namespace Tizen::Net;
69 using namespace Tizen::App;
71 namespace Tizen { namespace Net { namespace Sockets
74 static const long _THREAD_SLEEP_TIMER = 2000; // 2000 milliseconds = 2 seconds (sleep time)
75 static const long _MAX_COUNT_THREAD_SLEEP_TIMER = 15; // 2000 * 15 = 30000 milliseconds = 30 seconds (max sleep time)
77 _SocketImpl::_SocketImpl(Socket* pSocket)
78 : __socketFd(INVALID_HANDLE)
79 , __isConstructed(false)
86 , __isAcceptFired(false)
87 , __isReadFired(false)
88 , __isErrorReadFired(false)
89 , __isConnectFired(false)
90 , __isWriteFired(false)
91 , __isCloseFired(false)
92 , __socketEventType(NET_SOCKET_EVENT_NONE)
94 , __protocolFamily(NET_SOCKET_AF_NONE)
95 , __socketType(NET_SOCKET_TYPE_NONE)
96 , __protocol(NET_SOCKET_PROTOCOL_NONE)
97 , __pNetConnectionManager(null)
98 , __pManagedNetConnection(null)
99 , __pManagedNetConnectionEventListener(null)
100 , __pCustomNetConnection(null)
101 , __pCustomNetConnectionEventListener(null)
103 , __pSocketEvent(null)
106 , __pLingerOption(null)
107 , __pMulticastOption(null)
108 , __pGMainContext(null)
109 , __pGlibSocketInfo(null)
113 _SocketImpl::~_SocketImpl(void)
117 delete __pGlibSocketInfo;
118 __pGlibSocketInfo = null;
120 __pGMainContext = null;
122 delete __pMulticastOption;
123 __pMulticastOption = null;
125 delete __pLingerOption;
126 __pLingerOption = null;
134 __socketEventListenerList.RemoveAll();
136 delete __pSocketEvent;
137 __pSocketEvent = null;
139 delete __pCustomNetConnection;
140 __pCustomNetConnection = null;
142 delete __pCustomNetConnectionEventListener;
143 __pCustomNetConnectionEventListener = null;
145 delete __pManagedNetConnection;
146 __pManagedNetConnection = null;
148 delete __pManagedNetConnectionEventListener;
149 __pManagedNetConnectionEventListener = null;
151 delete __pNetConnectionManager;
152 __pNetConnectionManager = null;
154 __protocol = NET_SOCKET_PROTOCOL_NONE;
155 __socketType = NET_SOCKET_TYPE_NONE;
156 __protocolFamily = NET_SOCKET_AF_NONE;
157 __socketEventType = NET_SOCKET_EVENT_NONE;
159 __isCloseFired = false;
160 __isWriteFired = false;
161 __isConnectFired = false;
162 __isErrorReadFired = false;
163 __isReadFired = false;
164 __isAcceptFired = false;
166 __isNonblock = false;
169 __isLoopback = false;
171 __isConstructed = false;
172 __socketFd = INVALID_HANDLE;
176 _SocketImpl::Construct(const NetConnection& netConnection, NetSocketAddressFamily addressFamily, NetSocketType socketType,
177 NetSocketProtocol protocol)
179 SysTryReturnResult(NID_NET_SOCK, addressFamily == NET_SOCKET_AF_IPV4, E_UNSUPPORTED_FAMILY, "The AddressFamily is unsupported.");
180 SysTryReturnResult(NID_NET_SOCK, socketType == NET_SOCKET_TYPE_STREAM || socketType == NET_SOCKET_TYPE_DATAGRAM,
181 E_UNSUPPORTED_TYPE, "The SocketType is unsupported.");
182 SysTryReturnResult(NID_NET_SOCK, protocol == NET_SOCKET_PROTOCOL_TCP || protocol == NET_SOCKET_PROTOCOL_UDP ||
183 protocol == NET_SOCKET_PROTOCOL_SSL, E_UNSUPPORTED_PROTOCOL, "The Protocol is unsupported.");
185 SysLog(NID_NET_SOCK, "Socket uses CustomNetConnection.");
187 // Add netConection null check validation
188 NetConnection* pNetConnection = const_cast <NetConnection*>(&netConnection);
189 SysTryReturnResult(NID_NET_SOCK, pNetConnection != null, E_INVALID_ARG, "NetConnection is invalid.");
191 result r = E_SUCCESS;
194 int sockFd = INVALID_HANDLE;
197 _NetConnectionImpl* pCustomNCImpl = null;
198 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
201 char* pDeviceName = null;
202 const NetConnectionInfo* pNetConnectionInfo = null;
203 const _NetConnectionInfoImpl* pNCInfoImpl = null;
205 unique_ptr<_SocketEvent> pSocketEvent;
206 unique_ptr<_SocketCustomNetConnectionEventListener> pCustomNetConnectionEventListener;
207 unique_ptr<NetConnection> pCustomNetConnection;
209 // Set proper inparam
210 if (addressFamily == NET_SOCKET_AF_IPV4)
213 __protocolFamily = addressFamily;
216 if ((socketType == NET_SOCKET_TYPE_STREAM && protocol != NET_SOCKET_PROTOCOL_TCP) ||
217 (socketType == NET_SOCKET_TYPE_DATAGRAM && protocol != NET_SOCKET_PROTOCOL_UDP))
220 SysLogException(NID_NET_SOCK, r, "[%s] Type mismatch between SocketType [%d] and protocol [%d].", GetErrorMessage(r), socketType, protocol);
223 else if (socketType == NET_SOCKET_TYPE_STREAM)
226 __socketType = socketType;
227 __protocol = NET_SOCKET_PROTOCOL_TCP;
229 else if (socketType == NET_SOCKET_TYPE_DATAGRAM)
232 __socketType = socketType;
233 __protocol = NET_SOCKET_PROTOCOL_UDP;
237 // Custom Network Check
238 connState = pNetConnection->GetConnectionState();
240 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
242 SysLog(NID_NET_SOCK, "Custom Network is available.");
246 r = E_INVALID_CONNECTION;
247 SysLogException(NID_NET_SOCK, r, "[%s] Custom Network is not available.", GetErrorMessage(r));
251 __socketFd = socket(af, type, 0);
255 r = ConvertErrorToResult(errno);
256 SysLogException(NID_NET_SOCK, r, "[%s] Failed to construct the socket.", GetErrorMessage(r));
260 if (fcntl(__socketFd, F_SETFL, O_NONBLOCK) < 0)
262 __isNonblock = false;
264 SysLogException(NID_NET_SOCK, r, "[%s] Failed to construct(fcntl) the socket.", GetErrorMessage(r));
271 __socketEventListenerList.Construct();
273 pSocketEvent.reset(new (std::nothrow) _SocketEvent());
274 SysTryCatch(NID_NET_SOCK, pSocketEvent != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
276 r = pSocketEvent->Construct(this);
277 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to construct the SocketEvent.", GetErrorMessage(r));
280 // Set the device name (setsockopt)
281 pNetConnectionInfo = pNetConnection->GetNetConnectionInfo();
282 SysTryCatch(NID_NET_SOCK, pNetConnectionInfo != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get the ConnectionInfo Object.");
284 pNCInfoImpl = _NetConnectionInfoImpl::GetInstance(*pNetConnectionInfo);
285 SysTryCatch(NID_NET_SOCK, pNCInfoImpl != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get the ConnectionInfoImpl Object.");
287 deviceName = pNCInfoImpl->GetDeviceName();
289 if (deviceName.IsEmpty())
291 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
295 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
297 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
298 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
300 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
301 delete[] pDeviceName;
304 r = ConvertErrorToResult(errno);
305 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_CONNECTION, E_INVALID_CONNECTION,
306 "[E_INVALID_CONNECTION] Failed to bind(setsockopt) Device Name.");
310 // Copy New CustomNetConnection
311 pCustomNCImpl = _NetConnectionImpl::GetInstance(*pNetConnection);
313 pCustomNetConnection.reset(pCustomNCImpl->CopyInstanceN());
314 SysTryCatch(NID_NET_SOCK, pCustomNetConnection != null, r = E_INVALID_CONNECTION, E_INVALID_CONNECTION,
315 "[E_INVALID_CONNECTION] CustomNetConnection is null. Failed to copy the InstanceN.");
317 // New CustomNetConnectionEventListener
318 pCustomNetConnectionEventListener.reset(new (std::nothrow) _SocketCustomNetConnectionEventListener());
319 SysTryCatch(NID_NET_SOCK, pCustomNetConnectionEventListener != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
320 "[E_OUT_OF_MEMORY] Memory allocation failed.");
322 // AddNetConnectionListener
323 r = pCustomNetConnection->AddNetConnectionListener(*pCustomNetConnectionEventListener);
324 r = TransExceptionsExclusive(r, E_INVALID_CONNECTION, E_OUT_OF_MEMORY);
325 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Failed to add the NetConnectionListener.", GetErrorMessage(r));
327 // Set the __socketFd
328 pCustomNetConnectionEventListener->SetConstructParams(pSocketEvent.get(), __socketFd, this);
330 __pCustomNetConnection = pCustomNetConnection.release();
331 __pCustomNetConnectionEventListener = pCustomNetConnectionEventListener.release();
332 __pSocketEvent = pSocketEvent.release();
334 __isConstructed = true;
336 SysLog(NID_NET_SOCK, "Socket is constructed by non-blocking mode.(Default) [Fd : %d]", __socketFd);
341 if (__socketFd > INVALID_HANDLE)
345 SysLog(NID_NET_SOCK, "### SocketFd is closed. [Fd : %d]", sockFd);
351 _SocketImpl::Construct(NetSocketAddressFamily addressFamily, NetSocketType socketType, NetSocketProtocol protocol)
353 SysTryReturnResult(NID_NET_SOCK, addressFamily == NET_SOCKET_AF_IPV4, E_UNSUPPORTED_FAMILY, "The AddressFamily is unsupported.");
354 SysTryReturnResult(NID_NET_SOCK, socketType == NET_SOCKET_TYPE_STREAM || socketType == NET_SOCKET_TYPE_DATAGRAM,
355 E_UNSUPPORTED_TYPE, "The SocketType is unsupported.");
356 SysTryReturnResult(NID_NET_SOCK, protocol == NET_SOCKET_PROTOCOL_TCP || protocol == NET_SOCKET_PROTOCOL_UDP || protocol ==
357 NET_SOCKET_PROTOCOL_SSL, E_UNSUPPORTED_PROTOCOL, "The Protocol is unsupported.");
359 SysLog(NID_NET_SOCK, "Socket uses DefaultNetConnection.");
361 result r = E_SUCCESS;
364 int sockFd = INVALID_HANDLE;
366 _ManagedNetConnectionImpl* pManagedNCImpl = null;
368 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
369 _SocketMethodFlag flag = FLAG_NONE;
371 unique_ptr<_SocketEvent> pSocketEvent;
372 unique_ptr<_SocketManagedNetConnectionEventListener> pManagedNetConnectionEventListener;
373 unique_ptr<ManagedNetConnection> pManagedNetConnection;
374 unique_ptr<NetConnectionManager> pNetConnectionManager;
376 // Set proper inparam
377 if (addressFamily == NET_SOCKET_AF_IPV4)
380 __protocolFamily = addressFamily;
383 if ((socketType == NET_SOCKET_TYPE_STREAM && protocol != NET_SOCKET_PROTOCOL_TCP) ||
384 (socketType == NET_SOCKET_TYPE_DATAGRAM && protocol != NET_SOCKET_PROTOCOL_UDP))
387 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Invalid SocketType - Mismatch.");
390 else if (socketType == NET_SOCKET_TYPE_STREAM)
393 __socketType = socketType;
394 __protocol = NET_SOCKET_PROTOCOL_TCP;
396 else if (socketType == NET_SOCKET_TYPE_DATAGRAM)
399 __socketType = socketType;
400 __protocol = NET_SOCKET_PROTOCOL_UDP;
404 __socketFd = socket(af, type, 0);
408 r = ConvertErrorToResult(errno);
409 SysLogException(NID_NET_SOCK, r, "[%s] Failed to construct the socket.", GetErrorMessage(r));
413 if (fcntl(__socketFd, F_SETFL, O_NONBLOCK) < 0)
415 __isNonblock = false;
417 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to construct(fcntl) the socket.");
424 __socketEventListenerList.Construct();
427 pSocketEvent.reset(new (std::nothrow) _SocketEvent());
428 SysTryCatch(NID_NET_SOCK, pSocketEvent != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
430 r = pSocketEvent->Construct(this);
431 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[%s] Failed to construct the Socket Event.", GetErrorMessage(r));
434 if (_NetConnectionManagerImpl::IsDefaultMode() != true)
436 SysLog(NID_NET_SOCK, "Socket uses ManagedNetConnection.");
438 pNetConnectionManager.reset(new (std::nothrow) NetConnectionManager());
439 SysTryCatch(NID_NET_SOCK, pNetConnectionManager != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
441 r = pNetConnectionManager->Construct();
442 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_CONNECTION, r, "[%s] Failed to construct the ManagedNetConnection.", GetErrorMessage(r));
444 pManagedNetConnection.reset(pNetConnectionManager->GetManagedNetConnectionN());
445 SysTryCatch(NID_NET_SOCK, pManagedNetConnection != null, r = E_INVALID_CONNECTION, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] ManagedNetConnection is null. Failed to the GetManagedNetConnectionN.");
447 pManagedNCImpl = _ManagedNetConnectionImpl::GetInstance(*pManagedNetConnection);
449 // New ManagedNetConnectionEventListener
450 pManagedNetConnectionEventListener.reset(new (std::nothrow) _SocketManagedNetConnectionEventListener());
451 SysTryCatch(NID_NET_SOCK, pManagedNetConnectionEventListener != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
453 // SetManagedNetConnectionEventListener
454 r = pManagedNetConnection->SetManagedNetConnectionEventListener(pManagedNetConnectionEventListener.get());
455 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_CONNECTION, r, "[%s] Failed to the SetManagedNetConnectionEventListener.", GetErrorMessage(r));
457 // Set the __socketFd
458 flag = FLAG_CONSTRUCT;
459 pManagedNetConnectionEventListener->SetConstructParams(pSocketEvent.get(), __socketFd, flag, this);
461 // Start ManagedNetConnectionImpl
462 r = pManagedNCImpl->Start();
463 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_CONNECTION, r, "[%s] Failed to start the ManagedNetConnection.", GetErrorMessage(r));
465 connState = pManagedNCImpl->GetConnectionState();
466 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
468 pManagedNetConnectionEventListener->__isStarted = true;
472 __pNetConnectionManager = pNetConnectionManager.release();
473 __pManagedNetConnection = pManagedNetConnection.release();
474 __pManagedNetConnectionEventListener = pManagedNetConnectionEventListener.release();
475 __pSocketEvent = pSocketEvent.release();
477 __isConstructed = true;
479 SysLog(NID_NET_SOCK, "Socket is constructed by Non-blocking mode.(Default) [Fd : %d]", __socketFd);
484 if (__socketFd > INVALID_HANDLE)
488 SysLog(NID_NET_SOCK, "### SocketFd is closed. [Fd : %d]", sockFd);
494 _SocketImpl::Close(void)
496 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
498 result r = E_SUCCESS;
500 int sockFd = __socketFd;
501 IEnumeratorT<ISocketEventListener*>* pEnum = null;
502 ISocketEventListener* pSocketEventListener = null;
504 pEnum = __socketEventListenerList.GetEnumeratorN();
508 while (pEnum->MoveNext() == E_SUCCESS)
510 r = pEnum->GetCurrent(pSocketEventListener);
512 if (pSocketEventListener != null)
514 r = __pSocketEvent->RemoveListener(*pSocketEventListener);
515 r = __socketEventListenerList.Remove(const_cast<ISocketEventListener*>(pSocketEventListener));
521 if (__pGlibSocketInfo != null)
523 delete __pGlibSocketInfo;
524 __pGlibSocketInfo = null;
527 if (__socketFd > INVALID_HANDLE)
529 if (close(__socketFd) < 0)
531 r = ConvertErrorToResult(errno);
532 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to close the socket.");
535 SysLog(NID_NET_SOCK, "Socket is closed. [Fd : %d]", sockFd);
538 __socketFd = INVALID_HANDLE;
545 _SocketImpl::Bind(const NetEndPoint& localEP)
547 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
549 result r = E_SUCCESS;
552 unsigned long ipAddr = 0;
553 unsigned short port = 0;
555 Ip4Address* pIp4Addr = null;
556 const _NetEndPointImpl* pLocalEPImpl = null;
557 struct sockaddr_in localAddr;
559 pLocalEPImpl = _NetEndPointImpl::GetInstance(localEP);
561 af = pLocalEPImpl->GetNetAddressFamily();
562 SysTryReturnResult(NID_NET_SOCK, af == NET_AF_IPV4, E_INVALID_ARG, "Address family is invalid.");
564 pIp4Addr = static_cast <Ip4Address*>(pLocalEPImpl->GetAddress());
565 r = pIp4Addr->GetAddress(ipAddr);
566 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "Address is invalid.");
568 port = pLocalEPImpl->GetPort();
570 memset(&localAddr, 0, sizeof(localAddr));
571 localAddr.sin_family = AF_INET;
572 localAddr.sin_addr.s_addr = htonl(ipAddr);
573 localAddr.sin_port = htons(port);
575 if (bind(__socketFd, (struct sockaddr*) &localAddr, sizeof(localAddr)) < 0)
577 r = ConvertErrorToResult(errno);
578 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to bind the socket.");
581 SysLog(NID_NET_SOCK, "Socket is binded. [Fd : %d]", __socketFd);
583 if (__isAsync == true && __isUdp == true)
585 __isConnectFired = true;
586 __isWriteFired = true;
587 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
588 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
589 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the SocketEvent to attach all condition except G_IO_OUT.", __socketFd);
596 _SocketImpl::Connect(const NetEndPoint& remoteEP)
598 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
600 result r = E_SUCCESS;
604 unsigned long ipAddr = 0;
605 unsigned short port = 0;
607 Ip4Address* pIp4Addr = null;
608 const _NetEndPointImpl* pRemoteEPImpl = null;
609 struct sockaddr_in remoteAddr;
610 String remoteAddrString;
612 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
613 _SocketMethodFlag flag = FLAG_NONE;
616 char* pDeviceName = null;
617 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
618 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
619 // long timeout = OSP_DEFAULT_TIMEOUT; // 30 seconds (blocking time)
620 // guint timerId = -1;
623 pRemoteEPImpl = _NetEndPointImpl::GetInstance(remoteEP);
625 if (pRemoteEPImpl->IsValid() == true)
627 pIp4Addr = static_cast <Ip4Address*>(pRemoteEPImpl->GetAddress());
628 remoteAddrString = pIp4Addr->ToString();
633 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] NetEndPoint is invalid.");
637 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
639 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
641 if (__isLoopback == false)
643 if (remoteAddrString == LOOPBACK_ADDRESS)
646 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
652 if (remoteAddrString != LOOPBACK_ADDRESS)
655 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
661 af = pRemoteEPImpl->GetNetAddressFamily();
662 SysTryReturnResult(NID_NET_SOCK, af == NET_AF_IPV4, E_INVALID_ARG, "Address family is invalid.");
664 r = pIp4Addr->GetAddress(ipAddr);
665 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "Address is invalid.");
667 port = pRemoteEPImpl->GetPort();
669 memset(&remoteAddr, 0, sizeof(remoteAddr));
670 remoteAddr.sin_family = AF_INET;
671 remoteAddr.sin_addr.s_addr = htonl(ipAddr);
672 remoteAddr.sin_port = htons(port);
674 if (__pManagedNetConnection != null)
676 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
678 if (__isNonblock == false)
680 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
682 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
684 // timerId = g_timeout_add_seconds(timeout, (GSourceFunc)_SocketImpl::OnTimerCallback, this);
689 connState = pMNCConnImpl->QueryConnectionState(deviceName);
693 case NET_CONNECTION_STATE_STARTED:
695 case NET_CONNECTION_STATE_RESUMED:
697 case NET_CONNECTION_STATE_SUSPENDED:
698 SysLog(NID_NET_SOCK, "Network is available.");
700 if (deviceName.IsEmpty())
702 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
706 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
708 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
709 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
711 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
712 delete[] pDeviceName;
715 ConvertErrorToResult(errno);
717 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind the device name.");
721 err = connect(__socketFd, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
724 case NET_CONNECTION_STATE_STARTING:
726 case NET_CONNECTION_STATE_NONE:
728 case NET_CONNECTION_STATE_STOPPING:
730 case NET_CONNECTION_STATE_STOPPED:
731 SysLog(NID_NET_SOCK, "Network is not available.");
735 SysLog(NID_NET_SOCK, "Should not come here.");
739 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
741 // g_source_remove(timerId);
745 if (connState == NET_CONNECTION_STATE_STOPPED)
747 r = E_INVALID_CONNECTION;
748 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
749 // g_source_remove(timerId);
756 // SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network timeout happened. Try Again");
757 // g_source_remove(timerId);
758 // __isTimeout = false;
764 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
767 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout.");
772 Thread::Sleep(milliseconds);
777 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
779 if (__pManagedNetConnectionEventListener->__isStarted)
781 err = connect(__socketFd, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
787 __pManagedNetConnectionEventListener->SetConnectParams(__socketFd, remoteAddr, flag);
793 err = connect(__socketFd, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
798 r = ConvertErrorToResult(errno);
800 if (r != E_WOULD_BLOCK)
802 SysLogException(NID_NET_SOCK, r, "[%s] Failed to connect the socket.", GetErrorMessage(r));
803 if (__isAsync == true)
805 __isConnectFired = true;
811 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to connect the socket.", GetErrorMessage(r));
812 if (__isAsync == true)
814 __isConnectFired = false;
815 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
816 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
817 SysTryReturnResult(NID_NET_SOCK, r != E_SUCCESS, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach all condition.", __socketFd);
823 SysLog(NID_NET_SOCK, "Connect the socket. [Fd : %d]", __socketFd);
832 _SocketImpl::Send(ByteBuffer& buffer)
834 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
836 result r = E_SUCCESS;
837 result res = E_SUCCESS;
840 unsigned int offset = buffer.GetPosition();
841 unsigned int length = buffer.GetRemaining();
843 SysTryReturnResult(NID_NET_SOCK, length > 0, E_INVALID_ARG, "Address is invalid.");
845 sentLength = send(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0);
849 r = ConvertErrorToResult(errno);
851 if (r != E_WOULD_BLOCK)
853 SysLogException(NID_NET_SOCK, r, "[%s] Failed to send the socket.", GetErrorMessage(r));
854 if (__isAsync == true)
856 __isWriteFired = true;
862 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to send the socket.", GetErrorMessage(r));
863 if (__isAsync == true)
865 __isWriteFired = false;
866 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
867 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
868 SysTryCatch(NID_NET_SOCK, r != E_SUCCESS, r = E_WOULD_BLOCK, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
874 SysLog(NID_NET_SOCK, "Actually send byte : %d byte.", sentLength);
876 if (__isAsync == true)
878 __isWriteFired = false;
879 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
880 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
881 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
884 r = buffer.SetPosition(offset + sentLength);
885 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM , E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
890 res = buffer.SetPosition(offset);
891 SysTryReturnResult(NID_NET_SOCK, res == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
897 _SocketImpl::Send(void* pBuffer, int length, int& sentLength)
899 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
901 result r = E_SUCCESS;
903 if (!pBuffer || length <= 0)
906 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] The buffer is null or the length is invalid.");
910 sentLength = send(__socketFd, static_cast <char*>(pBuffer), length, 0);
914 r = ConvertErrorToResult(errno);
916 if (r != E_WOULD_BLOCK)
918 SysLogException(NID_NET_SOCK, r, "[%s] Failed to send the socket.", GetErrorMessage(r));
919 if (__isAsync == true)
921 __isWriteFired = true;
927 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to send the socket.", GetErrorMessage(r));
928 if (__isAsync == true)
930 __isWriteFired = false;
931 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
932 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
933 SysTryReturnResult(NID_NET_SOCK, r != E_SUCCESS, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
939 SysLog(NID_NET_SOCK, "Actually send byte : %d byte.", sentLength);
941 if (__isAsync == true)
943 __isWriteFired = false;
944 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
945 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
946 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
956 _SocketImpl::SendTo(ByteBuffer& buffer, const NetEndPoint& remoteEP)
958 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
960 result r = E_SUCCESS;
961 result res = E_SUCCESS;
966 unsigned long ipAddr = 0;
967 unsigned short port = 0;
969 Ip4Address* pIp4Addr = null;
970 const _NetEndPointImpl* pRemoteEPImpl = null;
971 struct sockaddr_in remoteAddr;
972 String remoteAddrString;
974 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
975 _SocketMethodFlag flag = FLAG_NONE;
979 char* pDeviceName = null;
980 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
981 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
984 pRemoteEPImpl = _NetEndPointImpl::GetInstance(remoteEP);
986 if (pRemoteEPImpl->IsValid() == true)
988 pIp4Addr = static_cast <Ip4Address*>(pRemoteEPImpl->GetAddress());
989 remoteAddrString = pIp4Addr->ToString();
994 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] NetEndPoint is invalid.");
998 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
1000 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1002 if (__isLoopback == false)
1004 if (remoteAddrString == LOOPBACK_ADDRESS)
1007 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1013 if (remoteAddrString != LOOPBACK_ADDRESS)
1016 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1022 af = pRemoteEPImpl->GetNetAddressFamily();
1023 SysTryReturnResult(NID_NET_SOCK, af == NET_AF_IPV4, E_INVALID_ARG, "Address family is invalid.");
1025 r = pIp4Addr->GetAddress(ipAddr);
1026 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "[E_INVALID_ARG] Address is invalid.");
1028 port = pRemoteEPImpl->GetPort();
1030 memset(&remoteAddr, 0, sizeof(remoteAddr));
1031 remoteAddr.sin_family = AF_INET;
1032 remoteAddr.sin_addr.s_addr = htonl(ipAddr);
1033 remoteAddr.sin_port = htons(port);
1035 unsigned int offset = buffer.GetPosition();
1036 unsigned int length = buffer.GetRemaining();
1041 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Address is invalid.");
1045 if (__pManagedNetConnection != null)
1047 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
1049 if (__isNonblock == false)
1051 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
1053 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
1058 connState = pMNCConnImpl->QueryConnectionState(deviceName);
1062 case NET_CONNECTION_STATE_STARTED:
1064 case NET_CONNECTION_STATE_RESUMED:
1066 case NET_CONNECTION_STATE_SUSPENDED:
1067 SysLog(NID_NET_SOCK, "Network is available.");
1069 if (deviceName.IsEmpty())
1071 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
1075 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
1077 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
1078 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1080 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
1081 delete[] pDeviceName;
1085 ConvertErrorToResult(errno);
1086 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind the device name.");
1090 sentLength = sendto(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1093 case NET_CONNECTION_STATE_STARTING:
1095 case NET_CONNECTION_STATE_NONE:
1097 case NET_CONNECTION_STATE_STOPPING:
1099 case NET_CONNECTION_STATE_STOPPED:
1100 SysLog(NID_NET_SOCK, "Network is not available.");
1104 SysLog(NID_NET_SOCK, "Should not come here.");
1108 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
1113 if (connState == NET_CONNECTION_STATE_STOPPED)
1115 r = E_INVALID_CONNECTION;
1116 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
1122 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
1125 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout.");
1130 Thread::Sleep(milliseconds);
1135 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
1137 if (__pManagedNetConnectionEventListener->__isStarted)
1139 sentLength = sendto(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1145 __pManagedNetConnectionEventListener->SetSendToParams(__socketFd, flag);
1151 sentLength = sendto(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1156 r = ConvertErrorToResult(errno);
1158 if (r != E_WOULD_BLOCK)
1160 SysLogException(NID_NET_SOCK, r, "[%s] Failed to sendto() the socket.", GetErrorMessage(r));
1161 if (__isAsync == true)
1163 __isConnectFired = false;
1164 __isWriteFired = true;
1170 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to sendto() the socket.", GetErrorMessage(r));
1171 if (__isAsync == true)
1173 __isConnectFired = true;
1174 __isWriteFired = false;
1175 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1176 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
1177 SysTryCatch(NID_NET_SOCK, r != E_SUCCESS, r = E_WOULD_BLOCK, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
1183 SysLog(NID_NET_SOCK, "Actually send byte : %d byte.", sentLength);
1185 if (__isAsync == true)
1187 __isConnectFired = true;
1188 __isWriteFired = false;
1189 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1190 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
1191 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
1194 r = buffer.SetPosition(offset + sentLength);
1195 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1200 res = buffer.SetPosition(offset);
1201 SysTryReturnResult(NID_NET_SOCK, res == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1207 _SocketImpl::SendTo(void* pBuffer, int length, const NetEndPoint& remoteEP, int& sentLength)
1209 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
1211 result r = E_SUCCESS;
1213 NetAddressFamily af;
1214 unsigned long ipAddr = 0;
1215 unsigned short port = 0;
1216 Ip4Address* pIp4Addr = null;
1217 const _NetEndPointImpl* pRemoteEPImpl = null;
1218 struct sockaddr_in remoteAddr;
1219 String remoteAddrString;
1221 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
1222 _SocketMethodFlag flag = FLAG_NONE;
1226 char* pDeviceName = null;
1227 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
1228 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
1231 pRemoteEPImpl = _NetEndPointImpl::GetInstance(remoteEP);
1233 if (!pBuffer || length <= 0)
1236 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] The buffer is null or the length is invalid.");
1240 if (pRemoteEPImpl->IsValid() == true)
1242 pIp4Addr = static_cast <Ip4Address*>(pRemoteEPImpl->GetAddress());
1243 remoteAddrString = pIp4Addr->ToString();
1248 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] NetEndPoint is invalid.");
1252 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
1254 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1256 if (__isLoopback == false)
1258 if (remoteAddrString == LOOPBACK_ADDRESS)
1261 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1267 if (remoteAddrString != LOOPBACK_ADDRESS)
1270 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1276 af = pRemoteEPImpl->GetNetAddressFamily();
1277 SysTryReturnResult(NID_NET_SOCK, af == NET_AF_IPV4, E_INVALID_ARG, "Address family is invalid.");
1279 r = pIp4Addr->GetAddress(ipAddr);
1280 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_INVALID_ARG, "[E_INVALID_ARG] Address is invalid.");
1282 port = pRemoteEPImpl->GetPort();
1284 memset(&remoteAddr, 0, sizeof(remoteAddr));
1285 remoteAddr.sin_family = AF_INET;
1286 remoteAddr.sin_addr.s_addr = htonl(ipAddr);
1287 remoteAddr.sin_port = htons(port);
1289 if (__pManagedNetConnection != null)
1291 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
1293 if (__isNonblock == false)
1295 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
1297 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
1302 connState = pMNCConnImpl->QueryConnectionState(deviceName);
1306 case NET_CONNECTION_STATE_STARTED:
1308 case NET_CONNECTION_STATE_RESUMED:
1310 case NET_CONNECTION_STATE_SUSPENDED:
1311 SysLog(NID_NET_SOCK, "Network is available.");
1313 if (deviceName.IsEmpty())
1315 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
1319 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
1321 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
1322 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1324 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
1325 delete[] pDeviceName;
1329 ConvertErrorToResult(errno);
1330 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind the device name.");
1334 sentLength = sendto(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1337 case NET_CONNECTION_STATE_STARTING:
1339 case NET_CONNECTION_STATE_NONE:
1341 case NET_CONNECTION_STATE_STOPPING:
1343 case NET_CONNECTION_STATE_STOPPED:
1344 SysLog(NID_NET_SOCK, "Network is not available.");
1348 SysLog(NID_NET_SOCK, "Should not come here.");
1352 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
1357 if (connState == NET_CONNECTION_STATE_STOPPED)
1359 r = E_INVALID_CONNECTION;
1360 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
1366 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
1369 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout.");
1374 Thread::Sleep(milliseconds);
1379 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
1381 if (__pManagedNetConnectionEventListener->__isStarted)
1383 sentLength = sendto(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1389 __pManagedNetConnectionEventListener->SetSendToParams(__socketFd, flag);
1395 sentLength = sendto(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, sizeof(remoteAddr));
1400 r = ConvertErrorToResult(errno);
1402 if (r != E_WOULD_BLOCK)
1404 SysLogException(NID_NET_SOCK, r, "[%s] Failed to sendto() the socket.", GetErrorMessage(r));
1405 if (__isAsync == true)
1407 __isConnectFired = false;
1408 __isWriteFired = true;
1414 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to sendto() the socket.", GetErrorMessage(r));
1415 if (__isAsync == true)
1417 __isConnectFired = true;
1418 __isWriteFired = false;
1419 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1420 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
1421 SysTryReturnResult(NID_NET_SOCK, r != E_SUCCESS, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach all condition.", __socketFd);
1427 SysLog(NID_NET_SOCK, "Actually sendto() byte : %d byte.", sentLength);
1429 if (__isAsync == true)
1431 __isConnectFired = true;
1432 __isWriteFired = false;
1433 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1434 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
1435 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
1445 _SocketImpl::Receive(ByteBuffer& buffer)
1447 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
1449 result r = E_SUCCESS;
1450 result res = E_SUCCESS;
1453 unsigned int offset = buffer.GetPosition();
1454 unsigned int length = buffer.GetRemaining();
1456 SysTryReturnResult(NID_NET_SOCK, length > 0, E_INVALID_ARG, "Address is invalid.");
1458 rcvdLength = recv(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0);
1462 r = ConvertErrorToResult(errno);
1464 if (r != E_WOULD_BLOCK)
1466 SysLogException(NID_NET_SOCK, r, "[%s] Failed to receive the socket.", GetErrorMessage(r));
1467 if (__isAsync == true)
1469 __isReadFired = true;
1475 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to receive the socket.", GetErrorMessage(r));
1476 if (__isAsync == true)
1478 __isReadFired = false;
1484 SysLog(NID_NET_SOCK, "Actually receive byte : %d byte", rcvdLength);
1486 if (__isAsync == true)
1488 __isReadFired = false;
1491 r = buffer.SetPosition(offset + rcvdLength);
1492 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1497 res = buffer.SetPosition(offset);
1498 SysTryReturnResult(NID_NET_SOCK, res == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1504 _SocketImpl::Receive(void* pBuffer, int length, int& rcvdLength)
1506 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
1508 result r = E_SUCCESS;
1510 if (!pBuffer || length <= 0)
1513 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] The buffer is null or the length is invalid.");
1517 rcvdLength = recv(__socketFd, static_cast <char*>(pBuffer), length, 0);
1521 r = ConvertErrorToResult(errno);
1523 if (r != E_WOULD_BLOCK)
1525 SysLogException(NID_NET_SOCK, r, "[%s] Failed to receive the socket.", GetErrorMessage(r));
1526 if (__isAsync == true)
1528 __isReadFired = true;
1534 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to receive the socket.", GetErrorMessage(r));
1535 if (__isAsync == true)
1537 __isReadFired = false;
1543 SysLog(NID_NET_SOCK, "Actually receive byte : %d byte.", rcvdLength);
1545 if (__isAsync == true)
1547 __isReadFired = false;
1557 _SocketImpl::ReceiveFrom(ByteBuffer& buffer, NetEndPoint& remoteEP)
1559 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
1561 result r = E_SUCCESS;
1562 result res = E_SUCCESS;
1565 unsigned int fromSize = 0;
1566 unsigned long ipAddr = 0;
1567 unsigned short port = 0;
1568 Ip4Address* pIp4Addr = null;
1569 _NetEndPointImpl* pRemoteEPImpl = null;
1570 struct sockaddr_in remoteAddr;
1571 String remoteAddrString;
1573 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
1574 _SocketMethodFlag flag = FLAG_NONE;
1578 char* pDeviceName = null;
1579 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
1580 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
1583 pRemoteEPImpl = _NetEndPointImpl::GetInstance(remoteEP);
1585 memset(&remoteAddr, 0, sizeof(remoteAddr));
1586 remoteAddr.sin_family = AF_INET;
1587 remoteAddr.sin_addr.s_addr = htonl(ipAddr);
1588 remoteAddr.sin_port = htons(port);
1590 if (pRemoteEPImpl->IsValid() == true)
1592 pIp4Addr = static_cast <Ip4Address*>(pRemoteEPImpl->GetAddress());
1593 remoteAddrString = pIp4Addr->ToString();
1598 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] NetEndPoint is invalid.");
1602 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
1604 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1606 if (__isLoopback == false)
1608 if (remoteAddrString == LOOPBACK_ADDRESS)
1611 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1617 if (remoteAddrString != LOOPBACK_ADDRESS)
1620 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1626 fromSize = sizeof(remoteAddr);
1628 unsigned int offset = buffer.GetPosition();
1629 unsigned int length = buffer.GetRemaining();
1634 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Address is invalid.");
1638 if (__pManagedNetConnection != null)
1640 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
1642 if (__isNonblock == false)
1644 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
1646 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
1651 connState = pMNCConnImpl->QueryConnectionState(deviceName);
1655 case NET_CONNECTION_STATE_STARTED:
1657 case NET_CONNECTION_STATE_RESUMED:
1659 case NET_CONNECTION_STATE_SUSPENDED:
1660 SysLog(NID_NET_SOCK, "Network is available.");
1662 if (deviceName.IsEmpty())
1664 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
1668 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
1670 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
1671 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1673 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
1674 delete[] pDeviceName;
1678 ConvertErrorToResult(errno);
1679 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[%s] Failed to bind the device name.", GetErrorMessage(E_INVALID_CONNECTION));
1683 rcvdLength = recvfrom(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
1686 case NET_CONNECTION_STATE_STARTING:
1688 case NET_CONNECTION_STATE_NONE:
1690 case NET_CONNECTION_STATE_STOPPING:
1692 case NET_CONNECTION_STATE_STOPPED:
1693 SysLog(NID_NET_SOCK, "Network is not available.");
1697 SysLog(NID_NET_SOCK, "Should not come here.");
1701 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
1706 if (connState == NET_CONNECTION_STATE_STOPPED)
1708 r = E_INVALID_CONNECTION;
1709 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
1715 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
1718 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout.");
1723 Thread::Sleep(milliseconds);
1728 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
1730 if (__pManagedNetConnectionEventListener->__isStarted)
1732 rcvdLength = recvfrom(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
1737 flag = FLAG_RECEIVEFROM;
1738 __pManagedNetConnectionEventListener->SetReceiveFromParams(__socketFd, flag);
1744 rcvdLength = recvfrom(__socketFd, (char*) (buffer.GetPointer() + offset), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
1749 r = ConvertErrorToResult(errno);
1751 if (r != E_WOULD_BLOCK)
1753 SysLogException(NID_NET_SOCK, r, "[%s] Failed to recvfrom() the socket.", GetErrorMessage(r));
1754 if (__isAsync == true)
1756 __isConnectFired = false;
1757 __isReadFired = true;
1763 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to recvfrom() the socket.", GetErrorMessage(r));
1765 if (__isAsync == true)
1767 __isConnectFired = true;
1768 __isReadFired = false;
1769 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1770 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
1771 SysTryCatch(NID_NET_SOCK, r != E_SUCCESS, r = E_WOULD_BLOCK, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
1777 SysLog(NID_NET_SOCK, "Actually recvfrom() byte : %d byte", rcvdLength);
1779 if (__isAsync == true)
1781 __isConnectFired = true;
1782 __isReadFired = false;
1783 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
1784 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
1785 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
1788 r = buffer.SetPosition(offset + rcvdLength);
1789 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1791 // Implementation : change from remoteAddr to NetEndPoint
1792 ipAddr = ntohl(remoteAddr.sin_addr.s_addr);
1793 port = ntohs(remoteAddr.sin_port);
1795 pIp4Addr = new (std::nothrow) Ip4Address(ipAddr);
1796 SysTryReturnResult(NID_NET_SOCK, pIp4Addr, E_OUT_OF_MEMORY, "Memory allocation failed.");
1797 r = pRemoteEPImpl->Update(*pIp4Addr, port);
1800 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
1801 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS || r == E_SYSTEM, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1802 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
1807 res = buffer.SetPosition(offset);
1808 SysTryReturnResult(NID_NET_SOCK, res == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to Set the position.");
1814 _SocketImpl::ReceiveFrom(void* pBuffer, int length, NetEndPoint& remoteEP, int& rcvdLength)
1816 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
1818 result r = E_SUCCESS;
1819 unsigned int fromSize = 0;
1820 unsigned long ipAddr = 0;
1821 unsigned short port = 0;
1822 Ip4Address* pIp4Addr = null;
1823 _NetEndPointImpl* pRemoteEPImpl = null;
1824 struct sockaddr_in remoteAddr;
1825 String remoteAddrString;
1827 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
1828 _SocketMethodFlag flag = FLAG_NONE;
1832 char* pDeviceName = null;
1833 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
1834 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
1837 pRemoteEPImpl = _NetEndPointImpl::GetInstance(remoteEP);
1839 memset(&remoteAddr, 0, sizeof(remoteAddr));
1840 remoteAddr.sin_family = AF_INET;
1841 remoteAddr.sin_addr.s_addr = htonl(ipAddr);
1842 remoteAddr.sin_port = htons(port);
1844 if (!pBuffer || length <= 0)
1847 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] The buffer is null or the length is invalid.");
1851 if (pRemoteEPImpl->IsValid() == true)
1853 pIp4Addr = static_cast <Ip4Address*>(pRemoteEPImpl->GetAddress());
1854 remoteAddrString = pIp4Addr->ToString();
1859 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] NetEndPoint is invalid.");
1863 _ApiVersion apiVersion = _AppInfo::GetApiVersion();
1865 if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
1867 if (__isLoopback == false)
1869 if (remoteAddrString == LOOPBACK_ADDRESS)
1872 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1878 if (remoteAddrString != LOOPBACK_ADDRESS)
1881 SysLogException(NID_NET_SOCK, r, "[E_SYSTEM] Failed to assign the requested address.");
1887 fromSize = sizeof(remoteAddr);
1889 if (__pManagedNetConnection != null)
1891 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
1893 if (__isNonblock == false)
1895 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
1897 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
1902 connState = pMNCConnImpl->QueryConnectionState(deviceName);
1906 case NET_CONNECTION_STATE_STARTED:
1908 case NET_CONNECTION_STATE_RESUMED:
1910 case NET_CONNECTION_STATE_SUSPENDED:
1911 SysLog(NID_NET_SOCK, "Network is available.");
1913 if (deviceName.IsEmpty())
1915 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
1919 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
1921 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
1922 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1924 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
1925 delete[] pDeviceName;
1929 ConvertErrorToResult(errno);
1930 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind the device name.");
1934 rcvdLength = recvfrom(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
1937 case NET_CONNECTION_STATE_STARTING:
1939 case NET_CONNECTION_STATE_NONE:
1941 case NET_CONNECTION_STATE_STOPPING:
1943 case NET_CONNECTION_STATE_STOPPED:
1944 SysLog(NID_NET_SOCK, "Network is not available.");
1948 SysLog(NID_NET_SOCK, "Should not come here.");
1952 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
1957 if (connState == NET_CONNECTION_STATE_STOPPED)
1959 r = E_INVALID_CONNECTION;
1960 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
1966 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
1969 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout.");
1974 Thread::Sleep(milliseconds);
1979 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
1981 if (__pManagedNetConnectionEventListener->__isStarted)
1983 rcvdLength = recvfrom(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
1988 flag = FLAG_RECEIVEFROM;
1989 __pManagedNetConnectionEventListener->SetReceiveFromParams(__socketFd, flag);
1995 rcvdLength = recvfrom(__socketFd, static_cast <char*>(pBuffer), length, 0, (struct sockaddr*) &remoteAddr, &fromSize);
2000 r = ConvertErrorToResult(errno);
2002 if (r != E_WOULD_BLOCK)
2004 SysLogException(NID_NET_SOCK, r, "[%s] Failed to recvfrom() the socket.", GetErrorMessage(r));
2005 if (__isAsync == true)
2007 __isConnectFired = false;
2008 __isReadFired = true;
2014 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to recvfrom() the socket.", GetErrorMessage(r));
2016 if (__isAsync == true)
2018 __isConnectFired = true;
2019 __isReadFired = false;
2020 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
2021 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
2022 SysTryReturnResult(NID_NET_SOCK, r != E_SUCCESS, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach all condition.", __socketFd);
2028 SysLog(NID_NET_SOCK, "Actually recvfrom() byte : %d byte", rcvdLength);
2030 if (__isAsync == true)
2032 __isConnectFired = true;
2033 __isReadFired = false;
2034 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
2035 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
2036 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
2039 // Implementation : change from remoteAddr to NetEndPoint.
2040 ipAddr = ntohl(remoteAddr.sin_addr.s_addr);
2041 port = ntohs(remoteAddr.sin_port);
2043 pIp4Addr = new (std::nothrow) Ip4Address(ipAddr);
2044 SysTryReturnResult(NID_NET_SOCK, pIp4Addr, E_OUT_OF_MEMORY, "Memory allocation failed.");
2045 r = pRemoteEPImpl->Update(*pIp4Addr, port);
2048 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
2049 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS || r == E_SYSTEM, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2050 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
2059 _SocketImpl::Listen(int backLog)
2061 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2063 result r = E_SUCCESS;
2066 _SocketMethodFlag flag = FLAG_NONE;
2067 NetConnectionState connState = NET_CONNECTION_STATE_NONE;
2070 char* pDeviceName = null;
2071 const _ManagedNetConnectionImpl* pMNCConnImpl = null;
2072 long milliseconds = _THREAD_SLEEP_TIMER; // 2000 milliseconds (sleep time)
2075 if (__pManagedNetConnection != null)
2077 SysLog(NID_NET_SOCK, "This socket uses ManagedNetConnection internally. [Fd : %d]", __socketFd);
2079 if (__isNonblock == false)
2081 SysLog(NID_NET_SOCK, "This socket is blocking mode.");
2083 pMNCConnImpl = _ManagedNetConnectionImpl::GetInstance(*__pManagedNetConnection);
2088 connState = pMNCConnImpl->QueryConnectionState(deviceName);
2092 case NET_CONNECTION_STATE_STARTED:
2094 case NET_CONNECTION_STATE_RESUMED:
2096 case NET_CONNECTION_STATE_SUSPENDED:
2097 SysLog(NID_NET_SOCK, "Network is available.");
2099 if (deviceName.IsEmpty())
2101 SysLog(NID_NET_SOCK, "Device Name is empty string. - Emulator Case");
2105 SysLog(NID_NET_SOCK, "Device Name is not empty string. [Device Name : %ls] - Target Case", deviceName.GetPointer());
2107 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
2108 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2110 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
2111 delete[] pDeviceName;
2114 ConvertErrorToResult(errno);
2115 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind the device name.");
2119 err = listen(__socketFd, backLog);
2122 case NET_CONNECTION_STATE_STARTING:
2124 case NET_CONNECTION_STATE_NONE:
2126 case NET_CONNECTION_STATE_STOPPING:
2128 case NET_CONNECTION_STATE_STOPPED:
2129 SysLog(NID_NET_SOCK, "Network is not available.");
2133 SysLog(NID_NET_SOCK, "Should not come here.");
2137 if (connState == NET_CONNECTION_STATE_STARTED || connState == NET_CONNECTION_STATE_RESUMED || connState == NET_CONNECTION_STATE_SUSPENDED)
2142 if (connState == NET_CONNECTION_STATE_STOPPED)
2144 r = E_INVALID_CONNECTION;
2145 SysLogException(NID_NET_SOCK, r, "[E_INVALID_CONNECTION] Network is stopped.");
2151 if (tryCount > _MAX_COUNT_THREAD_SLEEP_TIMER) // 2000 milliseconds * 15 count = 30 seconds timeout
2154 SysLogException(NID_NET_SOCK, r, "[E_TIMEOUT] Network is timeout. Try Again.");
2159 Thread::Sleep(milliseconds);
2164 SysLog(NID_NET_SOCK, "This socket is non-blocking mode.");
2166 if (__pManagedNetConnectionEventListener->__isStarted)
2168 err = listen(__socketFd, backLog);
2174 __pManagedNetConnectionEventListener->SetListenParams(__socketFd, backLog, flag);
2180 err = listen(__socketFd, backLog);
2185 r = ConvertErrorToResult(errno);
2187 if (r != E_WOULD_BLOCK)
2189 SysLogException(NID_NET_SOCK, r, "[%s] Failed to listen the socket.", GetErrorMessage(r));
2190 if (__isAsync == true)
2192 __isAcceptFired = true;
2198 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to listen the socket.", GetErrorMessage(r));
2200 if (__isAsync == true)
2202 __isAcceptFired = false;
2203 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
2204 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
2205 SysTryReturnResult(NID_NET_SOCK, r != E_SUCCESS, E_WOULD_BLOCK, "SocketFd : [%d], Set the socketEvent to attach all condition.", __socketFd);
2211 SysLog(NID_NET_SOCK, "Socket is listening. [Fd : %d, backLog : %d]", __socketFd, backLog);
2215 if (__isAsync == true)
2217 __isAcceptFired = false;
2218 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
2219 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Memory allocation failed.");
2220 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach G_IO_OUT condition.", __socketFd);
2230 _SocketImpl::AcceptN(void)
2234 result r = E_SUCCESS;
2235 Socket* pClientSocket = null;
2236 _SocketImpl* pClientSocketImpl = null;
2237 int clientFd = INVALID_HANDLE;
2238 unsigned int clientSize = 0;
2239 struct sockaddr_in clientAddr;
2241 SysTryCatch(NID_NET_SOCK, __isClosed == false, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Socket is already closed.");
2243 clientSize = sizeof(clientAddr);
2245 clientFd = accept(__socketFd, (struct sockaddr*) &clientAddr, &clientSize);
2249 r = ConvertErrorToResult(errno);
2251 if (r != E_WOULD_BLOCK)
2253 SysLogException(NID_NET_SOCK, r, "[%s] Failed to accept the socket.", GetErrorMessage(r));
2258 SysLogException(NID_NET_SOCK, r, "[%s] Returned EWOULDBLOCK to accept the socket.", GetErrorMessage(r));
2263 if (__isAsync == true)
2265 __isAcceptFired = false;
2266 r = __pGlibSocketInfo->SetSocketEvent(this, __socketFd);
2267 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
2268 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach All condition.", __socketFd);
2271 // Implementation - new Socket
2272 pClientSocket = new (std::nothrow) Socket();
2273 SysTryCatch(NID_NET_SOCK, pClientSocket != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2275 pClientSocketImpl = new (std::nothrow) _SocketImpl(pClientSocket);
2276 SysTryCatch(NID_NET_SOCK, pClientSocketImpl, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2278 // Set new SocketEvent
2279 pClientSocketImpl->__pSocketEvent = new (std::nothrow) _SocketEvent();
2280 SysTryCatch(NID_NET_SOCK, pClientSocketImpl->__pSocketEvent, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2282 r = pClientSocketImpl->__pSocketEvent->Construct(pClientSocketImpl);
2283 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct the socket event.");
2285 // New socket creation complete.
2286 pClientSocketImpl->__socketFd = clientFd;
2287 pClientSocketImpl->__isClosed = false;
2288 pClientSocketImpl->__isLoopback = this->__isLoopback;
2289 pClientSocketImpl->__isNonblock = this->__isNonblock;
2290 pClientSocketImpl->__socketEventType = this->__socketEventType;
2291 pClientSocketImpl->__isServer = false;
2292 pClientSocketImpl->__isConnectFired = true;
2293 pClientSocketImpl->__protocolFamily = this->__protocolFamily;
2294 pClientSocketImpl->__socketType = this->__socketType;
2295 pClientSocketImpl->__protocol = this->__protocol;
2296 pClientSocketImpl->__pLocal = this->__pLocal;
2297 pClientSocketImpl->__pPeer = this->__pPeer;
2298 pClientSocketImpl->__pLingerOption = this->__pLingerOption;
2299 pClientSocketImpl->__pMulticastOption = this->__pMulticastOption;
2300 pClientSocketImpl->__pGMainContext = this->__pGMainContext;
2301 pClientSocketImpl->__isConstructed = true;
2303 pClientSocket->__pSocketImpl = pClientSocketImpl;
2305 SysLog(NID_NET_SOCK, "New Socket(Child Socket) is created by Server Socket. [ServerFd : %d] [ChildFd : %d]", pClientSocket->__pSocketImpl->__socketFd, __socketFd);
2307 if (__isAsync == true)
2309 pClientSocketImpl->__pGlibSocketInfo = new (std::nothrow) _GlibSocketInfo();
2310 SysTryCatch(NID_NET_SOCK, __pGlibSocketInfo, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2312 r = pClientSocketImpl->__pGlibSocketInfo->SetSocketEvent(pClientSocketImpl, clientFd);
2313 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
2314 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to attach All condition to ClientFd.", clientFd);
2317 return pClientSocket;
2320 if (pClientSocket != null)
2322 delete pClientSocket;
2323 pClientSocket = null;
2331 _SocketImpl::Ioctl(NetSocketIoctlCmd cmd, unsigned long& pArg)
2333 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2334 SysTryReturnResult(NID_NET_SOCK, (cmd == NET_SOCKET_FIONBIO || cmd == NET_SOCKET_FIONREAD || cmd == NET_SOCKET_SIOCATMARK),
2335 E_INVALID_ARG, "IoctlCmd is invalid.");
2337 result r = E_SUCCESS;
2341 if (cmd == NET_SOCKET_FIONBIO && pArg != 0)
2343 // command = FIONBIO;
2344 flag = fcntl(__socketFd, F_GETFL);
2345 err = fcntl(__socketFd, F_SETFL, flag | O_NONBLOCK);
2346 SysLog(NID_NET_SOCK, "Changed the Socket by Non-blocking mode. [Fd : %d]", __socketFd);
2347 __isNonblock = true;
2349 else if (cmd == NET_SOCKET_FIONBIO && pArg == 0)
2351 // command = FIONBIO;
2352 flag = fcntl(__socketFd, F_GETFL);
2353 err = fcntl(__socketFd, F_SETFL, flag & ~O_NONBLOCK);
2354 SysLog(NID_NET_SOCK, "Changed the Socket by blocking mode. [Fd : %d]", __socketFd);
2355 __isNonblock = false;
2357 else if (cmd == NET_SOCKET_FIONREAD)
2359 // command = FIONREAD;
2360 err = ioctl(__socketFd, FIONREAD, (unsigned long*) &pArg);
2361 SysLog(NID_NET_SOCK, "Amount of data that can be read : %d bytes. [Fd : %d]", pArg, __socketFd);
2363 else if (cmd == NET_SOCKET_SIOCATMARK)
2365 // command = SIOCATMARK;
2366 err = ioctl(__socketFd, SIOCATMARK, (unsigned long*) &pArg);
2367 SysLog(NID_NET_SOCK, "All out of band(OOB) data has been read. [Fd : %d]", __socketFd);
2372 r = ConvertErrorToResult(errno);
2373 SysLogException(NID_NET_SOCK, r, "[%s] Failed to Ioctl the socket.", GetErrorMessage(r));
2380 _SocketImpl::AsyncSelectByListener(unsigned long socketEventType)
2382 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2384 result r = E_SUCCESS;
2386 // New _GlibSocketInfo
2387 if (__pGlibSocketInfo == null)
2389 __pGlibSocketInfo = new (std::nothrow) _GlibSocketInfo();
2390 SysTryReturnResult(NID_NET_SOCK, __pGlibSocketInfo, E_OUT_OF_MEMORY, "Memory allocation failed.");
2393 SysLog(NID_NET_SOCK, "Socket set the socketEvent by AsyncSelectByListener. [Fd : %d]", __socketFd);
2395 __socketEventType = socketEventType;
2396 __isNonblock = true;
2403 _SocketImpl::AddSocketListener(ISocketEventListener& listener)
2405 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2407 result r = E_SUCCESS;
2409 // Get the current GMainContext
2410 _EventDispatcher* pEventDispatcher = _EventDispatcher::GetCurrentEventDispatcher();
2411 SysTryReturnResult(NID_NET_SOCK, pEventDispatcher, E_SYSTEM, "GetCurrentEventDispatcher is null.");
2413 __pGMainContext = pEventDispatcher->GetGMainContext();
2414 SysTryReturnResult(NID_NET_SOCK, __pGMainContext, E_SYSTEM, "GetGMainContext is null.");
2416 r = __pSocketEvent->AddListener(listener, true);
2417 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to add the listener.");
2419 r = __socketEventListenerList.Add(const_cast<ISocketEventListener*>(&listener));
2420 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to add the listener to the list.");
2426 _SocketImpl::RemoveSocketListener(ISocketEventListener& listener)
2428 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2430 result r = E_SUCCESS;
2432 r = __pSocketEvent->RemoveListener(listener);
2433 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to remove the listener.");
2435 r = __socketEventListenerList.Remove(const_cast<ISocketEventListener*>(&listener));
2436 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to remove the listener to the list.");
2442 _SocketImpl::GetPeerEndPoint(void)
2446 result r = E_SUCCESS;
2447 Ip4Address* pIp4Addr = null;
2448 _NetEndPointImpl* pNetEndPointImpl = null;
2449 unsigned int addrSize = 0;
2450 struct sockaddr_in remoteAddr;
2452 SysTryCatch(NID_NET_SOCK, __isClosed == false, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Socket is already closed.");
2454 addrSize = sizeof(remoteAddr);
2456 if ((getpeername(__socketFd, (struct sockaddr*) &remoteAddr, &addrSize)) < 0)
2458 r = ConvertErrorToResult(errno);
2459 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the PeerEndPoint.", GetErrorMessage(r));
2463 SysLog(NID_NET_SOCK, "Peer Address, Port : [%s], [%d]", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port));
2465 // New and set Ip4Address
2466 pIp4Addr = new (std::nothrow) Ip4Address(ntohl(remoteAddr.sin_addr.s_addr));
2467 SysTryReturn(NID_NET_SOCK, pIp4Addr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2469 if (__pPeer == null)
2471 __pPeer = new (std::nothrow) NetEndPoint(*pIp4Addr, ntohs(remoteAddr.sin_port));
2472 SysTryReturn(NID_NET_SOCK, __pPeer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2476 pNetEndPointImpl = _NetEndPointImpl::GetInstance(*__pPeer);
2478 r = pNetEndPointImpl->Update(*pIp4Addr, ntohs(remoteAddr.sin_port));
2479 //r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
2480 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS || r == E_SYSTEM, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2481 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
2482 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS || r == E_INVALID_ARG, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2483 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to Set the NetEndPoint.");
2487 return (NetEndPoint*) __pPeer;
2495 _SocketImpl::GetLocalEndPoint(void)
2499 result r = E_SUCCESS;
2500 Ip4Address* pIp4Addr = null;
2501 _NetEndPointImpl* pNetEndPointImpl = null;
2502 unsigned int addrSize = 0;
2503 struct sockaddr_in localAddr;
2505 SysTryCatch(NID_NET_SOCK, __isClosed == false, r = E_INVALID_STATE, E_INVALID_STATE, "[%s] Socket is already closed.", GetErrorMessage(E_INVALID_STATE));
2507 addrSize = sizeof(localAddr);
2509 if ((getsockname(__socketFd, (struct sockaddr*) &localAddr, &addrSize)) < 0)
2511 r = ConvertErrorToResult(errno);
2512 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the LocalEndPoint.", GetErrorMessage(r));
2516 SysLog(NID_NET_SOCK, "Local Address, Port : [%s], [%d]", inet_ntoa(localAddr.sin_addr), ntohs(localAddr.sin_port));
2518 // New and set Ip4Address
2519 pIp4Addr = new (std::nothrow) Ip4Address(ntohl(localAddr.sin_addr.s_addr));
2520 SysTryReturn(NID_NET_SOCK, pIp4Addr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2522 if (__pLocal == null)
2524 __pLocal = new (std::nothrow) NetEndPoint(*pIp4Addr, ntohs(localAddr.sin_port));
2525 SysTryReturn(NID_NET_SOCK, __pLocal, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2529 pNetEndPointImpl = _NetEndPointImpl::GetInstance(*__pLocal);
2531 r = pNetEndPointImpl->Update(*pIp4Addr, ntohs(localAddr.sin_port));
2532 //r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
2533 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS || r == E_SYSTEM, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2534 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
2535 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS || r == E_INVALID_ARG, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2536 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to Set the NetEndPoint.");
2539 return (NetEndPoint*) __pLocal;
2547 _SocketImpl::GetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, int& pOptVal) const
2549 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2551 result r = E_SUCCESS;
2555 unsigned int optLen = 0;
2557 struct timeval timeout;
2559 switch (optionLevel)
2561 case NET_SOCKET_IPPROTO_TCP:
2562 optLevel = IPPROTO_TCP;
2565 case NET_SOCKET_IPPROTO_IP:
2566 optLevel = IPPROTO_IP;
2569 case NET_SOCKET_SOL_SOCKET:
2570 optLevel = SOL_SOCKET;
2575 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Invalid argument is used. Option level = %d", optionLevel);
2581 case NET_SOCKET_TCP_NODELAY:
2582 optName = TCP_NODELAY;
2585 case NET_SOCKET_TCP_MAXSEG:
2586 optName = TCP_MAXSEG;
2589 case NET_SOCKET_IP_TTL:
2593 case NET_SOCKET_IP_TOS:
2597 case NET_SOCKET_SO_ACCEPTCONN:
2598 optName = SO_ACCEPTCONN;
2601 case NET_SOCKET_SO_BROADCAST:
2602 optName = SO_BROADCAST;
2605 // case NET_SOCKET_SO_DEBUG:
2606 // optName = SO_DEBUG;
2609 case NET_SOCKET_SO_DONTROUTE:
2610 optName = SO_DONTROUTE;
2613 case NET_SOCKET_SO_ERROR:
2617 case NET_SOCKET_SO_KEEPALIVE:
2618 optName = SO_KEEPALIVE;
2621 case NET_SOCKET_SO_OOBINLINE:
2622 optName = SO_OOBINLINE;
2625 case NET_SOCKET_SO_RCVBUF:
2626 optName = SO_RCVBUF;
2629 case NET_SOCKET_SO_RCVTIMEO:
2630 optName = SO_RCVTIMEO;
2633 case NET_SOCKET_SO_REUSEADDR:
2634 optName = SO_REUSEADDR;
2637 case NET_SOCKET_SO_SNDBUF:
2638 optName = SO_SNDBUF;
2641 case NET_SOCKET_SO_SNDTIMEO:
2642 optName = SO_SNDTIMEO;
2645 case NET_SOCKET_SO_TYPE:
2650 r = E_UNSUPPORTED_OPTION;
2651 SysLogException(NID_NET_SOCK, r, "[E_UNSUPPORTED_OPTION] %d is unsupported", optionName);
2655 if (optName == SO_RCVTIMEO || optName == SO_SNDTIMEO)
2657 optLen = sizeof(timeout);
2658 err = getsockopt(__socketFd, optLevel, optName, &timeout, &optLen);
2659 pOptVal = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
2663 optLen = sizeof(pOptVal);
2664 err = getsockopt(__socketFd, optLevel, optName, (int*) &pOptVal, &optLen);
2669 r = ConvertErrorToResult(errno);
2670 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the option value.", GetErrorMessage(r));
2677 _SocketImpl::GetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, LingerOption& optionValue) const
2679 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2680 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_SOL_SOCKET, E_INVALID_ARG, "This is only for LingerOption.");
2681 SysTryReturnResult(NID_NET_SOCK, optionName == NET_SOCKET_SO_LINGER, E_INVALID_ARG, "This is only for LingerOption.");
2683 result r = E_SUCCESS;
2685 int optLevel = SOL_SOCKET;
2686 int optName = SO_LINGER;
2687 struct linger lingerOption;
2688 unsigned int optLen = sizeof(lingerOption);
2690 _LingerOptionImpl* pLingerOptionImpl = null;
2691 pLingerOptionImpl = _LingerOptionImpl::GetInstance(optionValue);
2693 if (getsockopt(__socketFd, optLevel, optName, (char*) &lingerOption, &optLen) < 0)
2695 r = ConvertErrorToResult(errno);
2696 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the linger option value.", GetErrorMessage(r));
2700 if (lingerOption.l_onoff == 0)
2702 pLingerOptionImpl->__enabled = false;
2706 pLingerOptionImpl->__enabled = true;
2709 if (lingerOption.l_linger >= 0)
2711 pLingerOptionImpl->__lingerTime = lingerOption.l_linger;
2715 pLingerOptionImpl->__lingerTime = 0;
2722 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, int optionValue)
2724 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2726 result r = E_SUCCESS;
2728 int optLevel = NET_SOCKET_OPTLEVEL_NONE;
2729 int optName = NET_SOCKET_OPTNAME_NONE;
2730 unsigned int optLen = 0;
2731 struct timeval timeout;
2733 switch (optionLevel)
2735 case NET_SOCKET_IPPROTO_TCP:
2736 optLevel = IPPROTO_TCP;
2739 case NET_SOCKET_IPPROTO_IP:
2740 optLevel = IPPROTO_IP;
2743 case NET_SOCKET_SOL_SOCKET:
2744 optLevel = SOL_SOCKET;
2749 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Invalid argument is used. Option level = %d", optionLevel);
2755 case NET_SOCKET_TCP_NODELAY:
2756 optName = TCP_NODELAY;
2759 case NET_SOCKET_TCP_MAXSEG:
2760 optName = TCP_MAXSEG;
2763 case NET_SOCKET_IP_TTL:
2767 case NET_SOCKET_IP_TOS:
2771 case NET_SOCKET_SO_BROADCAST:
2772 optName = SO_BROADCAST;
2775 // case NET_SOCKET_SO_DEBUG:
2776 // optName = SO_DEBUG;
2779 case NET_SOCKET_SO_DONTROUTE:
2780 optName = SO_DONTROUTE;
2783 case NET_SOCKET_SO_KEEPALIVE:
2784 optName = SO_KEEPALIVE;
2787 case NET_SOCKET_SO_OOBINLINE:
2788 optName = SO_OOBINLINE;
2791 case NET_SOCKET_SO_RCVBUF:
2792 optName = SO_RCVBUF;
2795 case NET_SOCKET_SO_RCVTIMEO:
2796 optName = SO_RCVTIMEO;
2798 timeout.tv_sec = optionValue / 1000;
2799 timeout.tv_usec = (optionValue % 1000) * 1000;
2802 case NET_SOCKET_SO_REUSEADDR:
2803 optName = SO_REUSEADDR;
2806 case NET_SOCKET_SO_SNDBUF:
2807 optName = SO_SNDBUF;
2810 case NET_SOCKET_SO_SNDTIMEO:
2811 optName = SO_SNDTIMEO;
2813 timeout.tv_sec = optionValue / 1000;
2814 timeout.tv_usec = (optionValue % 1000) * 1000;
2818 r = E_UNSUPPORTED_OPTION;
2819 SysLogException(NID_NET_SOCK, r, "[E_UNSUPPORTED_OPTION] %d is unsupported", optionName);
2823 if (optName == SO_RCVTIMEO || optName == SO_SNDTIMEO)
2825 optLen = sizeof(timeout);
2826 err = setsockopt(__socketFd, optLevel, optName, (struct timeval*) &timeout, optLen);
2830 optLen = sizeof(optionValue);
2831 err = setsockopt(__socketFd, optLevel, optName, (int*) &optionValue, optLen);
2836 r = ConvertErrorToResult(errno);
2837 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the option value.", GetErrorMessage(r));
2844 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, const LingerOption& optionValue)
2846 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2847 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_SOL_SOCKET, E_INVALID_ARG, "This is only for LingerOption.");
2848 SysTryReturnResult(NID_NET_SOCK, optionName == NET_SOCKET_SO_LINGER, E_INVALID_ARG, "This is only for LingerOption.");
2850 result r = E_SUCCESS;
2852 int optLevel = SOL_SOCKET;
2853 int optName = SO_LINGER;
2854 struct linger lingerOption;
2855 unsigned int optLen = sizeof(lingerOption);
2857 const _LingerOptionImpl* pLingerOptionImpl = null;
2858 pLingerOptionImpl = _LingerOptionImpl::GetInstance(optionValue);
2860 if (pLingerOptionImpl->__enabled == false)
2862 lingerOption.l_onoff = 0;
2866 lingerOption.l_onoff = 1;
2869 lingerOption.l_linger = pLingerOptionImpl->__lingerTime;
2871 if (setsockopt(__socketFd, optLevel, optName, (char*) &lingerOption, optLen) < 0)
2873 r = ConvertErrorToResult(errno);
2874 SysLogException(NID_NET_SOCK, r, "[%s] Failed to set the LingerOption value.", GetErrorMessage(r));
2881 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, const IpMulticastRequestOption& optionValue)
2883 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2884 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_IPPROTO_IP, E_INVALID_ARG, "This is only for MulticastOption.");
2885 SysTryReturnResult(NID_NET_SOCK, (optionName == NET_SOCKET_IP_ADD_MEMBERSHIP) || (optionName == NET_SOCKET_IP_DROP_MEMBERSHIP),
2886 E_INVALID_ARG, "This is only for MulticastOption.");
2888 result r = E_SUCCESS;
2889 int optLevel = SOL_SOCKET;
2890 int optName = SO_LINGER;
2891 struct ip_mreq multicastOption;
2892 unsigned int optLen = sizeof(multicastOption);
2894 const _IpMulticastRequestOptionImpl* pMulticastOptionImpl = null;
2895 Ip4Address* pIp4AddrMulti = null;
2896 Ip4Address* pIp4AddrInter = null;
2897 IpAddress* pIpAddrMulti = null;
2898 IpAddress* pIpAddrInter = null;
2899 _Ip4AddressImpl* pIp4AddrImplMulti = null;
2900 _Ip4AddressImpl* pIp4AddrImplInter = null;
2902 unsigned long ipAddrMulti = 0;
2903 unsigned long ipAddrInter = 0;
2905 pMulticastOptionImpl = _IpMulticastRequestOptionImpl::GetInstance(optionValue);
2907 pIpAddrMulti = pMulticastOptionImpl->__pMulticastAddr->GetAddress();
2908 SysTryReturnResult(NID_NET_SOCK, pIpAddrMulti, E_SYSTEM,"Multicast address is null.");
2910 pIp4AddrMulti = dynamic_cast <Ip4Address*>(pIpAddrMulti);
2911 SysTryReturnResult(NID_NET_SOCK, pIp4AddrMulti, E_SYSTEM, "Multicast IpAddress is null.");
2913 pIp4AddrImplMulti = _Ip4AddressImpl::GetInstance(*pIp4AddrMulti);
2914 pIp4AddrImplMulti->GetAddress(ipAddrMulti);
2916 pIpAddrInter = pMulticastOptionImpl->__pInterfaceAddr->GetAddress();
2917 SysTryReturnResult(NID_NET_SOCK, pIpAddrInter, E_SYSTEM, "Interface address is null.");
2919 pIp4AddrInter = dynamic_cast <Ip4Address*>(pIpAddrInter);
2920 SysTryReturnResult(NID_NET_SOCK, pIp4AddrInter, E_SYSTEM, "Interface IpAddress is null.");
2922 pIp4AddrImplInter = _Ip4AddressImpl::GetInstance(*pIp4AddrInter);
2923 pIp4AddrImplInter->GetAddress(ipAddrInter);
2925 multicastOption.imr_multiaddr.s_addr = ipAddrMulti;
2926 multicastOption.imr_interface.s_addr = ipAddrInter;
2928 if (setsockopt(__socketFd, optLevel, optName, (char*) &multicastOption, optLen) < 0)
2930 r = ConvertErrorToResult(errno);
2931 SysLogException(NID_NET_SOCK, r, "[%s] Failed to set the MulticastOption value.", GetErrorMessage(r));
2938 _SocketImpl::GetSocketFd(void)
2940 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2946 _SocketImpl::GetSocket(void) const
2952 _SocketImpl::GetInstance(Socket& socket)
2954 return socket.__pSocketImpl;
2958 _SocketImpl::GetInstance(const Socket& socket)
2960 return socket.__pSocketImpl;
2964 _SocketImpl::Dispose(void)
2966 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2968 result r = E_SUCCESS;
2971 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to close the socket.");
2980 _SocketImpl::OnGioEventCallback(GIOChannel* pSource, GIOCondition condition, gpointer pUserData)
2982 SysLog(NID_NET_SOCK, "### OnGioEventCallback ###");
2984 result errorCode = E_SUCCESS;
2985 GIOStatus status = G_IO_STATUS_NORMAL;
2987 gsize bytes_read = 0;
2991 _UserData* pRecvUserData = (_UserData*) pUserData;
2993 _SocketImpl* pSocketImpl = static_cast <_SocketImpl*>(pRecvUserData->pSocketImpl);
2994 GSource* pGSource = static_cast <GSource*>(pRecvUserData->pGSource);
2996 _SocketEventArg* pEventArg = null;
2998 SysLog(NID_NET_SOCK, "====> [Event] OnGioEventCallback : All GIO event is received.(%d).", condition);
3000 if (condition & G_IO_ERR)
3002 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_ERR)");
3004 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3006 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3007 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3008 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3009 // G_IO_ERROR Converting
3010 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3011 errorCode = ConvertGioStatus(status);
3012 pEventArg->SetError(E_SUCCESS);
3013 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3015 g_source_set_callback(pGSource, null, null, null);
3016 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3022 if (pSocketImpl->__isServer) // For Server
3024 SysLog(NID_NET_SOCK, "====> [Event] This is server");
3026 if (condition & (G_IO_IN))
3028 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_IN)");
3030 struct pollfd fds[1];
3031 fds[0].fd = pSocketImpl->__socketFd;
3032 fds[0].events = POLLRDHUP;
3035 error = poll(fds, 1, 0);
3036 SysTryCatch(NID_NET_SOCK, error != -1, , null, "Failed to the poll");
3039 if (fds[0].revents & POLLRDHUP)
3041 SysLog(NID_NET_SOCK, "====> Socket is not available.(POLLRDHUP)");
3043 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3045 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3046 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3047 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3048 // G_IO_ERROR Converting
3049 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3050 errorCode = ConvertGioStatus(status);
3051 pEventArg->SetError(errorCode);
3052 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3054 g_source_set_callback(pGSource, null, null, null);
3055 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null.(%d)", g_source_get_id(pGSource));
3062 SysLog(NID_NET_SOCK, "====> Socket is available.(Not POLLRDHUP)");
3064 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_ACCEPT) && (pSocketImpl->__isAcceptFired == false) &&
3065 (pSocketImpl->__isCloseFired == false) && (pSocketImpl->__socketType == NET_SOCKET_TYPE_STREAM))
3067 // Fire the event(NET_SOCKET_EVENT_ACCEPT)
3068 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_ACCEPT);
3069 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3070 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3077 if (condition & (G_IO_OUT))
3079 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_OUT).");
3085 SysLog(NID_NET_SOCK, "====> [Event] This is Client");
3087 if (condition & (G_IO_IN))
3089 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_IN).");
3091 struct pollfd fds[1];
3092 fds[0].fd = pSocketImpl->__socketFd;
3093 fds[0].events = POLLRDHUP;
3095 error = poll(fds, 1, 0);
3096 SysTryCatch(NID_NET_SOCK, error != -1, , null, "Failed to the poll");
3098 if (fds[0].revents & POLLRDHUP)
3100 SysLog(NID_NET_SOCK, "====> Socket is not available.(POLLRDHUP)");
3102 ioctl(pSocketImpl->__socketFd, FIONREAD, &readSize);
3103 SysLog(NID_NET_SOCK, "Amount of data that can be read : %d bytes. [Fd : %d byte]", readSize, pSocketImpl->__socketFd);
3107 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3109 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3110 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3111 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3112 // G_IO_ERROR Converting
3113 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3114 errorCode = ConvertGioStatus(status);
3115 pEventArg->SetError(errorCode);
3116 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3118 g_source_set_callback(pGSource, null, null, null);
3119 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3126 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_READ) && (pSocketImpl->__isReadFired == false) &&
3127 (pSocketImpl->__isErrorReadFired == false) && (pSocketImpl->__isCloseFired == false))
3129 // Fire the event(NET_SOCKET_EVENT_READ)
3130 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_READ);
3131 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3132 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3134 pSocketImpl->__isErrorReadFired = true;
3139 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3141 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3142 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3143 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3144 // G_IO_ERROR Converting
3145 // status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3146 // errorCode = ConvertGioStatus(status);
3147 pEventArg->SetError(E_SUCCESS);
3148 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3150 g_source_set_callback(pGSource, null, null, null);
3151 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3159 SysLog(NID_NET_SOCK, "====> Socket is available.(Not POLLRDHUP)");
3161 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_READ) && (pSocketImpl->__isReadFired == false) &&
3162 (pSocketImpl->__isCloseFired == false))
3164 // Fire the event(NET_SOCKET_EVENT_READ)
3165 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_READ);
3166 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3167 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3174 if (condition & (G_IO_OUT))
3176 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_OUT).");
3178 if (pSocketImpl->__isWriteFired == true)
3180 errorCode = pSocketImpl->__pGlibSocketInfo->SetSocketEvent(pSocketImpl, pSocketImpl->__socketFd);
3181 SysTryReturn(NID_NET_SOCK, errorCode == E_SUCCESS, true, errorCode, "[%s] Memory allocation failed.", GetErrorMessage(errorCode));
3182 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to release G_IO_OUT condition in OnGioEventCallback.", pSocketImpl->__socketFd);
3187 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CONNECT) && (pSocketImpl->__isConnectFired == false) &&
3188 (pSocketImpl->__isCloseFired == false) && (pSocketImpl->__socketType == NET_SOCKET_TYPE_STREAM))
3190 // Fire the event(NET_SOCKET_EVENT_CONNECT)
3191 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CONNECT);
3192 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3193 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3198 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_WRITE) && (pSocketImpl->__isConnectFired == true) &&
3199 (pSocketImpl->__isWriteFired == false) && (pSocketImpl->__isCloseFired == false))
3201 // Fire the event(NET_SOCKET_EVENT_WRITE)
3202 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_WRITE);
3203 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3204 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3211 if (condition & G_IO_PRI)
3213 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_PRI).");
3216 if (condition & G_IO_HUP)
3218 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_HUP).");
3221 if (condition & G_IO_NVAL)
3223 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_NVAL).");
3233 _SocketImpl::OnTimerCallback(gpointer pUserData)
3235 SysLog(NID_NET_SOCK, "### OnTimercallback ###");
3237 _SocketImpl* pSocketImpl = static_cast <_SocketImpl*>(pUserData);
3239 pSocketImpl->__isTimeout = true;
3244 _GlibSocketInfo::_GlibSocketInfo(void)
3245 : __pSocketChannel(null)
3246 , __pSocketSource(null)
3247 , __socketFd(INVALID_HANDLE)
3252 _GlibSocketInfo::~_GlibSocketInfo(void)
3257 if (__pSocketSource != null)
3259 g_source_set_callback(__pSocketSource, null, null, null);
3260 SysLog(NID_NET_SOCK, "### Deleted g_source_destroy(%d)", g_source_get_id(__pSocketSource));
3261 g_source_destroy(__pSocketSource);
3262 g_source_unref(__pSocketSource);
3263 __pSocketSource = null;
3266 if (__pSocketChannel != null)
3268 g_io_channel_unref(__pSocketChannel);
3269 __pSocketChannel = null;
3274 _GlibSocketInfo::SetSocketEvent(_SocketImpl* pSocketImpl, HSocket socketFd)
3276 result r = E_SUCCESS;
3278 GIOCondition condition = (GIOCondition)0;
3280 if (__pSocketChannel == null)
3282 __pUserData = new (std::nothrow) _UserData;
3283 SysTryReturnResult(NID_NET_SOCK, __pUserData, E_OUT_OF_MEMORY, "Memory allocation failed.");
3285 __pSocketChannel = g_io_channel_unix_new(socketFd);
3286 SysLog(NID_NET_SOCK, "Created the SocketChannel. [Fd : %d]", socketFd);
3289 // Set masking of wanted socket Event
3290 if (pSocketImpl->__isWriteFired == false)
3292 condition = static_cast <GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
3296 condition = static_cast <GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
3299 // G_IO_IN : There is data to read. 1
3300 // G_IO_OUT : Data can be written (without blocking). 4
3301 // G_IO_PRI : There is urgent data to read. 2
3302 // G_IO_ERR : Error condition. 8
3303 // G_IO_HUP : Hung up (the connection has been broken, usually for pipes and sockets). 16
3304 // G_IO_NVAL : Invalid request. The file descriptor is not open. 32
3306 if (__pSocketSource != null)
3308 g_source_set_callback(__pSocketSource, null, null, null);
3309 SysLog(NID_NET_SOCK, "### Deleted g_source_destroy(%d)", g_source_get_id(__pSocketSource));
3310 g_source_destroy(__pSocketSource);
3311 g_source_unref(__pSocketSource);
3312 __pSocketSource = null;
3313 SysLog(NID_NET_SOCK, "Unref the SocketSource.");
3316 __pSocketSource = g_io_create_watch(__pSocketChannel, condition);
3318 __pUserData->pSocketImpl = (_SocketImpl*) pSocketImpl;
3319 __pUserData->pGSource = (GSource*) __pSocketSource;
3321 g_source_set_callback(__pSocketSource, (GSourceFunc) _SocketImpl::OnGioEventCallback, __pUserData, null);
3322 g_source_attach(__pSocketSource, pSocketImpl->__pGMainContext);
3324 SysLog(NID_NET_SOCK, "Created the GSource Id : [%d]", g_source_get_id(__pSocketSource));
3325 SysLog(NID_NET_SOCK, "Created the SocketSource for receiving the event. [%d]", condition);
3330 } } } // Tizen::Net::Sockets