2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.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://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;
2325 _SocketImpl::Ioctl(NetSocketIoctlCmd cmd, unsigned long& pArg)
2327 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2328 SysTryReturnResult(NID_NET_SOCK, (cmd == NET_SOCKET_FIONBIO || cmd == NET_SOCKET_FIONREAD || cmd == NET_SOCKET_SIOCATMARK),
2329 E_INVALID_ARG, "IoctlCmd is invalid.");
2331 result r = E_SUCCESS;
2335 if (cmd == NET_SOCKET_FIONBIO && pArg != 0)
2337 // command = FIONBIO;
2338 flag = fcntl(__socketFd, F_GETFL);
2339 err = fcntl(__socketFd, F_SETFL, flag | O_NONBLOCK);
2340 SysLog(NID_NET_SOCK, "Changed the Socket by Non-blocking mode. [Fd : %d]", __socketFd);
2341 __isNonblock = true;
2343 else if (cmd == NET_SOCKET_FIONBIO && pArg == 0)
2345 // command = FIONBIO;
2346 flag = fcntl(__socketFd, F_GETFL);
2347 err = fcntl(__socketFd, F_SETFL, flag & ~O_NONBLOCK);
2348 SysLog(NID_NET_SOCK, "Changed the Socket by blocking mode. [Fd : %d]", __socketFd);
2349 __isNonblock = false;
2351 else if (cmd == NET_SOCKET_FIONREAD)
2353 // command = FIONREAD;
2354 err = ioctl(__socketFd, FIONREAD, (unsigned long*) &pArg);
2355 SysLog(NID_NET_SOCK, "Amount of data that can be read : %d bytes. [Fd : %d]", pArg, __socketFd);
2357 else if (cmd == NET_SOCKET_SIOCATMARK)
2359 // command = SIOCATMARK;
2360 err = ioctl(__socketFd, SIOCATMARK, (unsigned long*) &pArg);
2361 SysLog(NID_NET_SOCK, "All out of band(OOB) data has been read. [Fd : %d]", __socketFd);
2366 r = ConvertErrorToResult(errno);
2367 SysLogException(NID_NET_SOCK, r, "[%s] Failed to Ioctl the socket.", GetErrorMessage(r));
2374 _SocketImpl::AsyncSelectByListener(unsigned long socketEventType)
2376 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2378 result r = E_SUCCESS;
2380 // New _GlibSocketInfo
2381 if (__pGlibSocketInfo == null)
2383 __pGlibSocketInfo = new (std::nothrow) _GlibSocketInfo();
2384 SysTryReturnResult(NID_NET_SOCK, __pGlibSocketInfo, E_OUT_OF_MEMORY, "Memory allocation failed.");
2387 SysLog(NID_NET_SOCK, "Socket set the socketEvent by AsyncSelectByListener. [Fd : %d]", __socketFd);
2389 __socketEventType = socketEventType;
2390 __isNonblock = true;
2397 _SocketImpl::AddSocketListener(ISocketEventListener& listener)
2399 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2401 result r = E_SUCCESS;
2403 // Get the current GMainContext
2404 _EventDispatcher* pEventDispatcher = _EventDispatcher::GetCurrentEventDispatcher();
2405 SysTryReturnResult(NID_NET_SOCK, pEventDispatcher, E_SYSTEM, "GetCurrentEventDispatcher is null.");
2407 __pGMainContext = pEventDispatcher->GetGMainContext();
2408 SysTryReturnResult(NID_NET_SOCK, __pGMainContext, E_SYSTEM, "GetGMainContext is null.");
2410 r = __pSocketEvent->AddListener(listener, true);
2411 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to add the listener.");
2413 r = __socketEventListenerList.Add(const_cast<ISocketEventListener*>(&listener));
2414 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to add the listener to the list.");
2420 _SocketImpl::RemoveSocketListener(ISocketEventListener& listener)
2422 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2424 result r = E_SUCCESS;
2426 r = __pSocketEvent->RemoveListener(listener);
2427 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to remove the listener.");
2429 r = __socketEventListenerList.Remove(const_cast<ISocketEventListener*>(&listener));
2430 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to remove the listener to the list.");
2436 _SocketImpl::GetPeerEndPoint(void)
2440 result r = E_SUCCESS;
2441 Ip4Address* pIp4Addr = null;
2442 _NetEndPointImpl* pNetEndPointImpl = null;
2443 unsigned int addrSize = 0;
2444 struct sockaddr_in remoteAddr;
2446 SysTryCatch(NID_NET_SOCK, __isClosed == false, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Socket is already closed.");
2448 addrSize = sizeof(remoteAddr);
2450 if ((getpeername(__socketFd, (struct sockaddr*) &remoteAddr, &addrSize)) < 0)
2452 r = ConvertErrorToResult(errno);
2453 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the PeerEndPoint.", GetErrorMessage(r));
2457 SysLog(NID_NET_SOCK, "Peer Address, Port : [%s], [%d]", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port));
2459 // New and set Ip4Address
2460 pIp4Addr = new (std::nothrow) Ip4Address(ntohl(remoteAddr.sin_addr.s_addr));
2461 SysTryReturn(NID_NET_SOCK, pIp4Addr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2463 if (__pPeer == null)
2465 __pPeer = new (std::nothrow) NetEndPoint(*pIp4Addr, ntohs(remoteAddr.sin_port));
2466 SysTryReturn(NID_NET_SOCK, __pPeer, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2470 pNetEndPointImpl = _NetEndPointImpl::GetInstance(*__pPeer);
2472 r = pNetEndPointImpl->Update(*pIp4Addr, ntohs(remoteAddr.sin_port));
2473 //r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
2474 //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.");
2475 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
2476 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.");
2477 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to Set the NetEndPoint.");
2481 return (NetEndPoint*) __pPeer;
2489 _SocketImpl::GetLocalEndPoint(void)
2493 result r = E_SUCCESS;
2494 Ip4Address* pIp4Addr = null;
2495 _NetEndPointImpl* pNetEndPointImpl = null;
2496 unsigned int addrSize = 0;
2497 struct sockaddr_in localAddr;
2499 SysTryCatch(NID_NET_SOCK, __isClosed == false, r = E_INVALID_STATE, E_INVALID_STATE, "[%s] Socket is already closed.", GetErrorMessage(E_INVALID_STATE));
2501 addrSize = sizeof(localAddr);
2503 if ((getsockname(__socketFd, (struct sockaddr*) &localAddr, &addrSize)) < 0)
2505 r = ConvertErrorToResult(errno);
2506 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the LocalEndPoint.", GetErrorMessage(r));
2510 SysLog(NID_NET_SOCK, "Local Address, Port : [%s], [%d]", inet_ntoa(localAddr.sin_addr), ntohs(localAddr.sin_port));
2512 // New and set Ip4Address
2513 pIp4Addr = new (std::nothrow) Ip4Address(ntohl(localAddr.sin_addr.s_addr));
2514 SysTryReturn(NID_NET_SOCK, pIp4Addr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2516 if (__pLocal == null)
2518 __pLocal = new (std::nothrow) NetEndPoint(*pIp4Addr, ntohs(localAddr.sin_port));
2519 SysTryReturn(NID_NET_SOCK, __pLocal, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
2523 pNetEndPointImpl = _NetEndPointImpl::GetInstance(*__pLocal);
2525 r = pNetEndPointImpl->Update(*pIp4Addr, ntohs(localAddr.sin_port));
2526 //r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
2527 //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.");
2528 //SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set the NetEndPoint.");
2529 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.");
2530 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to Set the NetEndPoint.");
2533 return (NetEndPoint*) __pLocal;
2541 _SocketImpl::GetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, int& pOptVal) const
2543 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2545 result r = E_SUCCESS;
2549 unsigned int optLen = 0;
2551 struct timeval timeout;
2553 switch (optionLevel)
2555 case NET_SOCKET_IPPROTO_TCP:
2556 optLevel = IPPROTO_TCP;
2559 case NET_SOCKET_IPPROTO_IP:
2560 optLevel = IPPROTO_IP;
2563 case NET_SOCKET_SOL_SOCKET:
2564 optLevel = SOL_SOCKET;
2569 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Invalid argument is used. Option level = %d", optionLevel);
2575 case NET_SOCKET_TCP_NODELAY:
2576 optName = TCP_NODELAY;
2579 case NET_SOCKET_TCP_MAXSEG:
2580 optName = TCP_MAXSEG;
2583 case NET_SOCKET_IP_TTL:
2587 case NET_SOCKET_IP_TOS:
2591 case NET_SOCKET_SO_ACCEPTCONN:
2592 optName = SO_ACCEPTCONN;
2595 case NET_SOCKET_SO_BROADCAST:
2596 optName = SO_BROADCAST;
2599 // case NET_SOCKET_SO_DEBUG:
2600 // optName = SO_DEBUG;
2603 case NET_SOCKET_SO_DONTROUTE:
2604 optName = SO_DONTROUTE;
2607 case NET_SOCKET_SO_ERROR:
2611 case NET_SOCKET_SO_KEEPALIVE:
2612 optName = SO_KEEPALIVE;
2615 case NET_SOCKET_SO_OOBINLINE:
2616 optName = SO_OOBINLINE;
2619 case NET_SOCKET_SO_RCVBUF:
2620 optName = SO_RCVBUF;
2623 case NET_SOCKET_SO_RCVTIMEO:
2624 optName = SO_RCVTIMEO;
2627 case NET_SOCKET_SO_REUSEADDR:
2628 optName = SO_REUSEADDR;
2631 case NET_SOCKET_SO_SNDBUF:
2632 optName = SO_SNDBUF;
2635 case NET_SOCKET_SO_SNDTIMEO:
2636 optName = SO_SNDTIMEO;
2639 case NET_SOCKET_SO_TYPE:
2644 r = E_UNSUPPORTED_OPTION;
2645 SysLogException(NID_NET_SOCK, r, "[E_UNSUPPORTED_OPTION] %d is unsupported", optionName);
2649 if (optName == SO_RCVTIMEO || optName == SO_SNDTIMEO)
2651 optLen = sizeof(timeout);
2652 err = getsockopt(__socketFd, optLevel, optName, &timeout, &optLen);
2653 pOptVal = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
2657 optLen = sizeof(pOptVal);
2658 err = getsockopt(__socketFd, optLevel, optName, (int*) &pOptVal, &optLen);
2663 r = ConvertErrorToResult(errno);
2664 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the option value.", GetErrorMessage(r));
2671 _SocketImpl::GetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, LingerOption& optionValue) const
2673 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2674 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_SOL_SOCKET, E_INVALID_ARG, "This is only for LingerOption.");
2675 SysTryReturnResult(NID_NET_SOCK, optionName == NET_SOCKET_SO_LINGER, E_INVALID_ARG, "This is only for LingerOption.");
2677 result r = E_SUCCESS;
2679 int optLevel = SOL_SOCKET;
2680 int optName = SO_LINGER;
2681 struct linger lingerOption;
2682 unsigned int optLen = sizeof(lingerOption);
2684 _LingerOptionImpl* pLingerOptionImpl = null;
2685 pLingerOptionImpl = _LingerOptionImpl::GetInstance(optionValue);
2687 if (getsockopt(__socketFd, optLevel, optName, (char*) &lingerOption, &optLen) < 0)
2689 r = ConvertErrorToResult(errno);
2690 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the linger option value.", GetErrorMessage(r));
2694 if (lingerOption.l_onoff == 0)
2696 pLingerOptionImpl->__enabled = false;
2700 pLingerOptionImpl->__enabled = true;
2703 if (lingerOption.l_linger >= 0)
2705 pLingerOptionImpl->__lingerTime = lingerOption.l_linger;
2709 pLingerOptionImpl->__lingerTime = 0;
2716 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, int optionValue)
2718 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2720 result r = E_SUCCESS;
2722 int optLevel = NET_SOCKET_OPTLEVEL_NONE;
2723 int optName = NET_SOCKET_OPTNAME_NONE;
2724 unsigned int optLen = 0;
2725 struct timeval timeout;
2727 switch (optionLevel)
2729 case NET_SOCKET_IPPROTO_TCP:
2730 optLevel = IPPROTO_TCP;
2733 case NET_SOCKET_IPPROTO_IP:
2734 optLevel = IPPROTO_IP;
2737 case NET_SOCKET_SOL_SOCKET:
2738 optLevel = SOL_SOCKET;
2743 SysLogException(NID_NET_SOCK, r, "[E_INVALID_ARG] Invalid argument is used. Option level = %d", optionLevel);
2749 case NET_SOCKET_TCP_NODELAY:
2750 optName = TCP_NODELAY;
2753 case NET_SOCKET_TCP_MAXSEG:
2754 optName = TCP_MAXSEG;
2757 case NET_SOCKET_IP_TTL:
2761 case NET_SOCKET_IP_TOS:
2765 case NET_SOCKET_SO_BROADCAST:
2766 optName = SO_BROADCAST;
2769 // case NET_SOCKET_SO_DEBUG:
2770 // optName = SO_DEBUG;
2773 case NET_SOCKET_SO_DONTROUTE:
2774 optName = SO_DONTROUTE;
2777 case NET_SOCKET_SO_KEEPALIVE:
2778 optName = SO_KEEPALIVE;
2781 case NET_SOCKET_SO_OOBINLINE:
2782 optName = SO_OOBINLINE;
2785 case NET_SOCKET_SO_RCVBUF:
2786 optName = SO_RCVBUF;
2789 case NET_SOCKET_SO_RCVTIMEO:
2790 optName = SO_RCVTIMEO;
2792 timeout.tv_sec = optionValue / 1000;
2793 timeout.tv_usec = (optionValue % 1000) * 1000;
2796 case NET_SOCKET_SO_REUSEADDR:
2797 optName = SO_REUSEADDR;
2800 case NET_SOCKET_SO_SNDBUF:
2801 optName = SO_SNDBUF;
2804 case NET_SOCKET_SO_SNDTIMEO:
2805 optName = SO_SNDTIMEO;
2807 timeout.tv_sec = optionValue / 1000;
2808 timeout.tv_usec = (optionValue % 1000) * 1000;
2812 r = E_UNSUPPORTED_OPTION;
2813 SysLogException(NID_NET_SOCK, r, "[E_UNSUPPORTED_OPTION] %d is unsupported", optionName);
2817 if (optName == SO_RCVTIMEO || optName == SO_SNDTIMEO)
2819 optLen = sizeof(timeout);
2820 err = setsockopt(__socketFd, optLevel, optName, (struct timeval*) &timeout, optLen);
2824 optLen = sizeof(optionValue);
2825 err = setsockopt(__socketFd, optLevel, optName, (int*) &optionValue, optLen);
2830 r = ConvertErrorToResult(errno);
2831 SysLogException(NID_NET_SOCK, r, "[%s] Failed to get the option value.", GetErrorMessage(r));
2838 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, const LingerOption& optionValue)
2840 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2841 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_SOL_SOCKET, E_INVALID_ARG, "This is only for LingerOption.");
2842 SysTryReturnResult(NID_NET_SOCK, optionName == NET_SOCKET_SO_LINGER, E_INVALID_ARG, "This is only for LingerOption.");
2844 result r = E_SUCCESS;
2846 int optLevel = SOL_SOCKET;
2847 int optName = SO_LINGER;
2848 struct linger lingerOption;
2849 unsigned int optLen = sizeof(lingerOption);
2851 const _LingerOptionImpl* pLingerOptionImpl = null;
2852 pLingerOptionImpl = _LingerOptionImpl::GetInstance(optionValue);
2854 if (pLingerOptionImpl->__enabled == false)
2856 lingerOption.l_onoff = 0;
2860 lingerOption.l_onoff = 1;
2863 lingerOption.l_linger = pLingerOptionImpl->__lingerTime;
2865 if (setsockopt(__socketFd, optLevel, optName, (char*) &lingerOption, optLen) < 0)
2867 r = ConvertErrorToResult(errno);
2868 SysLogException(NID_NET_SOCK, r, "[%s] Failed to set the LingerOption value.", GetErrorMessage(r));
2875 _SocketImpl::SetSockOpt(NetSocketOptLevel optionLevel, NetSocketOptName optionName, const IpMulticastRequestOption& optionValue)
2877 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2878 SysTryReturnResult(NID_NET_SOCK, optionLevel == NET_SOCKET_IPPROTO_IP, E_INVALID_ARG, "This is only for MulticastOption.");
2879 SysTryReturnResult(NID_NET_SOCK, (optionName == NET_SOCKET_IP_ADD_MEMBERSHIP) || (optionName == NET_SOCKET_IP_DROP_MEMBERSHIP),
2880 E_INVALID_ARG, "This is only for MulticastOption.");
2882 result r = E_SUCCESS;
2883 int optLevel = SOL_SOCKET;
2884 int optName = SO_LINGER;
2885 struct ip_mreq multicastOption;
2886 unsigned int optLen = sizeof(multicastOption);
2888 const _IpMulticastRequestOptionImpl* pMulticastOptionImpl = null;
2889 Ip4Address* pIp4AddrMulti = null;
2890 Ip4Address* pIp4AddrInter = null;
2891 IpAddress* pIpAddrMulti = null;
2892 IpAddress* pIpAddrInter = null;
2893 _Ip4AddressImpl* pIp4AddrImplMulti = null;
2894 _Ip4AddressImpl* pIp4AddrImplInter = null;
2896 unsigned long ipAddrMulti = 0;
2897 unsigned long ipAddrInter = 0;
2899 pMulticastOptionImpl = _IpMulticastRequestOptionImpl::GetInstance(optionValue);
2901 pIpAddrMulti = pMulticastOptionImpl->__pMulticastAddr->GetAddress();
2902 SysTryReturnResult(NID_NET_SOCK, pIpAddrMulti, E_SYSTEM,"Multicast address is null.");
2904 pIp4AddrMulti = dynamic_cast <Ip4Address*>(pIpAddrMulti);
2905 SysTryReturnResult(NID_NET_SOCK, pIp4AddrMulti, E_SYSTEM, "Multicast IpAddress is null.");
2907 pIp4AddrImplMulti = _Ip4AddressImpl::GetInstance(*pIp4AddrMulti);
2908 pIp4AddrImplMulti->GetAddress(ipAddrMulti);
2910 pIpAddrInter = pMulticastOptionImpl->__pInterfaceAddr->GetAddress();
2911 SysTryReturnResult(NID_NET_SOCK, pIpAddrInter, E_SYSTEM, "Interface address is null.");
2913 pIp4AddrInter = dynamic_cast <Ip4Address*>(pIpAddrInter);
2914 SysTryReturnResult(NID_NET_SOCK, pIp4AddrInter, E_SYSTEM, "Interface IpAddress is null.");
2916 pIp4AddrImplInter = _Ip4AddressImpl::GetInstance(*pIp4AddrInter);
2917 pIp4AddrImplInter->GetAddress(ipAddrInter);
2919 multicastOption.imr_multiaddr.s_addr = ipAddrMulti;
2920 multicastOption.imr_interface.s_addr = ipAddrInter;
2922 if (setsockopt(__socketFd, optLevel, optName, (char*) &multicastOption, optLen) < 0)
2924 r = ConvertErrorToResult(errno);
2925 SysLogException(NID_NET_SOCK, r, "[%s] Failed to set the MulticastOption value.", GetErrorMessage(r));
2932 _SocketImpl::GetSocketFd(void)
2934 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2940 _SocketImpl::GetSocket(void) const
2946 _SocketImpl::GetInstance(Socket& socket)
2948 return socket.__pSocketImpl;
2952 _SocketImpl::GetInstance(const Socket& socket)
2954 return socket.__pSocketImpl;
2958 _SocketImpl::Dispose(void)
2960 SysTryReturnResult(NID_NET_SOCK, __isClosed == false, E_INVALID_STATE, "Socket is already closed.");
2962 result r = E_SUCCESS;
2965 SysTryReturnResult(NID_NET_SOCK, r == E_SUCCESS, r, "Failed to close the socket.");
2974 _SocketImpl::OnGioEventCallback(GIOChannel* pSource, GIOCondition condition, gpointer pUserData)
2976 SysLog(NID_NET_SOCK, "### OnGioEventCallback ###");
2978 result errorCode = E_SUCCESS;
2979 GIOStatus status = G_IO_STATUS_NORMAL;
2981 gsize bytes_read = 0;
2985 _UserData* pRecvUserData = (_UserData*) pUserData;
2987 _SocketImpl* pSocketImpl = static_cast <_SocketImpl*>(pRecvUserData->pSocketImpl);
2988 GSource* pGSource = static_cast <GSource*>(pRecvUserData->pGSource);
2990 _SocketEventArg* pEventArg = null;
2992 SysLog(NID_NET_SOCK, "====> [Event] OnGioEventCallback : All GIO event is received.(%d).", condition);
2994 if (condition & G_IO_ERR)
2996 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_ERR)");
2998 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3000 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3001 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3002 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3003 // G_IO_ERROR Converting
3004 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3005 errorCode = ConvertGioStatus(status);
3006 pEventArg->SetError(E_SUCCESS);
3007 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3009 g_source_set_callback(pGSource, null, null, null);
3010 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3016 if (pSocketImpl->__isServer) // For Server
3018 SysLog(NID_NET_SOCK, "====> [Event] This is server");
3020 if (condition & (G_IO_IN))
3022 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_IN)");
3024 struct pollfd fds[1];
3025 fds[0].fd = pSocketImpl->__socketFd;
3026 fds[0].events = POLLRDHUP;
3029 error = poll(fds, 1, 0);
3030 SysTryCatch(NID_NET_SOCK, error != -1, , null, "Failed to the poll");
3033 if (fds[0].revents & POLLRDHUP)
3035 SysLog(NID_NET_SOCK, "====> Socket is not available.(POLLRDHUP)");
3037 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3039 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3040 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3041 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3042 // G_IO_ERROR Converting
3043 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3044 errorCode = ConvertGioStatus(status);
3045 pEventArg->SetError(errorCode);
3046 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3048 g_source_set_callback(pGSource, null, null, null);
3049 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null.(%d)", g_source_get_id(pGSource));
3056 SysLog(NID_NET_SOCK, "====> Socket is available.(Not POLLRDHUP)");
3058 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_ACCEPT) && (pSocketImpl->__isAcceptFired == false) &&
3059 (pSocketImpl->__isCloseFired == false) && (pSocketImpl->__socketType == NET_SOCKET_TYPE_STREAM))
3061 // Fire the event(NET_SOCKET_EVENT_ACCEPT)
3062 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_ACCEPT);
3063 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3064 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3071 if (condition & (G_IO_OUT))
3073 SysLog(NID_NET_SOCK, "====> [Event] Socket event is received.(G_IO_OUT).");
3079 SysLog(NID_NET_SOCK, "====> [Event] This is Client");
3081 if (condition & (G_IO_IN))
3083 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_IN).");
3085 struct pollfd fds[1];
3086 fds[0].fd = pSocketImpl->__socketFd;
3087 fds[0].events = POLLRDHUP;
3089 error = poll(fds, 1, 0);
3090 SysTryCatch(NID_NET_SOCK, error != -1, , null, "Failed to the poll");
3092 if (fds[0].revents & POLLRDHUP)
3094 SysLog(NID_NET_SOCK, "====> Socket is not available.(POLLRDHUP)");
3096 ioctl(pSocketImpl->__socketFd, FIONREAD, &readSize);
3097 SysLog(NID_NET_SOCK, "Amount of data that can be read : %d bytes. [Fd : %d byte]", readSize, pSocketImpl->__socketFd);
3101 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3103 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3104 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3105 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3106 // G_IO_ERROR Converting
3107 status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3108 errorCode = ConvertGioStatus(status);
3109 pEventArg->SetError(errorCode);
3110 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3112 g_source_set_callback(pGSource, null, null, null);
3113 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3120 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_READ) && (pSocketImpl->__isReadFired == false) &&
3121 (pSocketImpl->__isErrorReadFired == false) && (pSocketImpl->__isCloseFired == false))
3123 // Fire the event(NET_SOCKET_EVENT_READ)
3124 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_READ);
3125 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3126 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3128 pSocketImpl->__isErrorReadFired = true;
3133 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CLOSE) && (pSocketImpl->__isCloseFired == false))
3135 // Fire the event(NET_SOCKET_EVENT_CLOSE)
3136 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
3137 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3138 // G_IO_ERROR Converting
3139 // status = g_io_channel_read_chars(pSource, buffer, sizeof(buffer), &bytes_read, null);
3140 // errorCode = ConvertGioStatus(status);
3141 pEventArg->SetError(E_SUCCESS);
3142 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3144 g_source_set_callback(pGSource, null, null, null);
3145 SysLog(NID_NET_SOCK, "### Set giocondition for GSource to null (%d)", g_source_get_id(pGSource));
3153 SysLog(NID_NET_SOCK, "====> Socket is available.(Not POLLRDHUP)");
3155 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_READ) && (pSocketImpl->__isReadFired == false) &&
3156 (pSocketImpl->__isCloseFired == false))
3158 // Fire the event(NET_SOCKET_EVENT_READ)
3159 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_READ);
3160 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3161 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3168 if (condition & (G_IO_OUT))
3170 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_OUT).");
3172 if (pSocketImpl->__isWriteFired == true)
3174 errorCode = pSocketImpl->__pGlibSocketInfo->SetSocketEvent(pSocketImpl, pSocketImpl->__socketFd);
3175 SysTryReturn(NID_NET_SOCK, errorCode == E_SUCCESS, true, errorCode, "[%s] Memory allocation failed.", GetErrorMessage(errorCode));
3176 SysLog(NID_NET_SOCK, "SocketFd : [%d], Set the socketEvent to release G_IO_OUT condition in OnGioEventCallback.", pSocketImpl->__socketFd);
3181 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_CONNECT) && (pSocketImpl->__isConnectFired == false) &&
3182 (pSocketImpl->__isCloseFired == false) && (pSocketImpl->__socketType == NET_SOCKET_TYPE_STREAM))
3184 // Fire the event(NET_SOCKET_EVENT_CONNECT)
3185 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_CONNECT);
3186 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3187 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3192 if ((pSocketImpl->__socketEventType & NET_SOCKET_EVENT_WRITE) && (pSocketImpl->__isConnectFired == true) &&
3193 (pSocketImpl->__isWriteFired == false) && (pSocketImpl->__isCloseFired == false))
3195 // Fire the event(NET_SOCKET_EVENT_WRITE)
3196 pEventArg = new (std::nothrow) _SocketEventArg(pSocketImpl->__socketFd, NET_SOCKET_EVENT_WRITE);
3197 SysTryCatch(NID_NET_SOCK, pEventArg, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
3198 pSocketImpl->__pSocketEvent->Fire(*pEventArg);
3205 if (condition & G_IO_PRI)
3207 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_PRI).");
3210 if (condition & G_IO_HUP)
3212 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_HUP).");
3215 if (condition & G_IO_NVAL)
3217 SysLog(NID_NET_SOCK, "====> [Event] Socket event is Received.(G_IO_NVAL).");
3227 _SocketImpl::OnTimerCallback(gpointer pUserData)
3229 SysLog(NID_NET_SOCK, "### OnTimercallback ###");
3231 _SocketImpl* pSocketImpl = static_cast <_SocketImpl*>(pUserData);
3233 pSocketImpl->__isTimeout = true;
3238 _GlibSocketInfo::_GlibSocketInfo(void)
3239 : __pSocketChannel(null)
3240 , __pSocketSource(null)
3241 , __socketFd(INVALID_HANDLE)
3246 _GlibSocketInfo::~_GlibSocketInfo(void)
3251 if (__pSocketSource != null)
3253 g_source_set_callback(__pSocketSource, null, null, null);
3254 SysLog(NID_NET_SOCK, "### Deleted g_source_destroy(%d)", g_source_get_id(__pSocketSource));
3255 g_source_destroy(__pSocketSource);
3256 g_source_unref(__pSocketSource);
3257 __pSocketSource = null;
3260 if (__pSocketChannel != null)
3262 g_io_channel_unref(__pSocketChannel);
3263 __pSocketChannel = null;
3268 _GlibSocketInfo::SetSocketEvent(_SocketImpl* pSocketImpl, HSocket socketFd)
3270 result r = E_SUCCESS;
3272 GIOCondition condition = (GIOCondition)0;
3274 if (__pSocketChannel == null)
3276 __pUserData = new (std::nothrow) _UserData;
3277 SysTryReturnResult(NID_NET_SOCK, __pUserData, E_OUT_OF_MEMORY, "Memory allocation failed.");
3279 __pSocketChannel = g_io_channel_unix_new(socketFd);
3280 SysLog(NID_NET_SOCK, "Created the SocketChannel. [Fd : %d]", socketFd);
3283 // Set masking of wanted socket Event
3284 if (pSocketImpl->__isWriteFired == false)
3286 condition = static_cast <GIOCondition>(G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
3290 condition = static_cast <GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
3293 // G_IO_IN : There is data to read. 1
3294 // G_IO_OUT : Data can be written (without blocking). 4
3295 // G_IO_PRI : There is urgent data to read. 2
3296 // G_IO_ERR : Error condition. 8
3297 // G_IO_HUP : Hung up (the connection has been broken, usually for pipes and sockets). 16
3298 // G_IO_NVAL : Invalid request. The file descriptor is not open. 32
3300 if (__pSocketSource != null)
3302 g_source_set_callback(__pSocketSource, null, null, null);
3303 SysLog(NID_NET_SOCK, "### Deleted g_source_destroy(%d)", g_source_get_id(__pSocketSource));
3304 g_source_destroy(__pSocketSource);
3305 g_source_unref(__pSocketSource);
3306 __pSocketSource = null;
3307 SysLog(NID_NET_SOCK, "Unref the SocketSource.");
3310 __pSocketSource = g_io_create_watch(__pSocketChannel, condition);
3312 __pUserData->pSocketImpl = (_SocketImpl*) pSocketImpl;
3313 __pUserData->pGSource = (GSource*) __pSocketSource;
3315 g_source_set_callback(__pSocketSource, (GSourceFunc) _SocketImpl::OnGioEventCallback, __pUserData, null);
3316 g_source_attach(__pSocketSource, pSocketImpl->__pGMainContext);
3318 SysLog(NID_NET_SOCK, "Created the GSource Id : [%d]", g_source_get_id(__pSocketSource));
3319 SysLog(NID_NET_SOCK, "Created the SocketSource for receiving the event. [%d]", condition);
3324 } } } // Tizen::Net::Sockets