Prevent fix
[platform/framework/native/channel-service.git] / src / IpcServer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file  FIo_IpcServer.cpp
20  * @brief This is the implementation file for the IpcServer class.
21  *
22  */
23
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <iostream>
29 #include <new>
30
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/un.h>
36
37 #include <sys/smack.h>
38
39 #include <ipc/ipc_message.h>
40
41 #include <FBaseRtMutex.h>
42 #include <FBaseSysLog.h>
43 #include <FBase_StringConverter.h>
44 #include <FBaseRt_EventDispatcher.h>
45
46 #include "IIpcServerEventListener.h"
47 #include "IpcServer.h"
48
49 using namespace std;
50 using namespace IPC;
51
52 using namespace Tizen::Base;
53 using namespace Tizen::Base::Runtime;
54 using namespace Tizen::App;
55
56 IpcServer::_ChannelInfo::_ChannelInfo(void)
57         : pClientInfo(null)
58         , pGIOChannel(null)
59         , pGSource(null)
60         , destroySource(true)
61 {
62
63 }
64
65 IpcServer::_ChannelInfo::~_ChannelInfo(void)
66 {
67         if (pGIOChannel != null)
68         {
69                 g_io_channel_unref(pGIOChannel);
70         }
71
72         if (pGSource != null)
73         {
74                 if (destroySource)
75                 {
76                         g_source_destroy(pGSource);
77                 }
78
79                 g_source_unref(pGSource);
80         }
81 }
82
83 IpcServer::_ClientInfo::_ClientInfo(void)
84         : clientId(-1)
85         , pIpcServer(null)
86         , pReverseChannel(null)
87 {
88
89 }
90
91 IpcServer::_ClientInfo::~_ClientInfo(void)
92 {
93         if (pReverseChannel != null)
94         {
95                 g_io_channel_unref(pReverseChannel);
96         }
97
98         channels.clear();
99 }
100
101 IpcServer::IpcServer(void)
102         : __runOnCallerThread(false)
103         , __pEventDispatcher(null)
104         , __pListener(null)
105         , __handlerThread(0)
106         , __pHandlerGMainContext(null)
107         , __pHandlerGMainLoop(null)
108         , __pConnectGSource(null)
109         , __pCurrentChannel(null)
110         , __pCurrentClientInfo(null)
111 {
112         __messageBuffer[0] = '\0';
113 }
114
115
116 IpcServer::~IpcServer(void)
117 {
118         if (__pConnectGSource != null)
119         {
120                 g_source_destroy(__pConnectGSource);
121                 g_source_unref(__pConnectGSource);
122                 __pConnectGSource = null;
123         }
124
125         if (!__runOnCallerThread)
126         {
127                 if (__pEventDispatcher)
128                 {
129                         delete __pEventDispatcher;
130                         __pEventDispatcher = null;
131                 }
132
133                 if (__pHandlerGMainLoop)
134                 {
135                         g_main_loop_unref(__pHandlerGMainLoop);
136                         __pHandlerGMainLoop = null;
137                 }
138
139                 if (__pHandlerGMainContext)
140                 {
141                         g_main_context_unref(__pHandlerGMainContext);
142                         __pHandlerGMainContext = null;
143                 }
144         }
145
146         // clean-up clients
147         __clients.clear();
148 }
149
150 result
151 IpcServer::Construct(const String& name, const IIpcServerEventListener& listener, bool runOnCallerThread)
152 {
153         result r = E_SUCCESS;
154
155         GIOChannel* pGIOChannel = null;
156         GSource* pGSource = null;
157
158         struct sockaddr_un serverAddress;
159         int serverSocket = -1;
160         int serverLen = 0;
161         int ret = 0;
162         std::string socketName;
163         char* pName = null;
164         size_t socketNameLength = 0;
165
166         __name = name;
167         __pListener = const_cast <IIpcServerEventListener*>(&listener);
168         __runOnCallerThread = runOnCallerThread;
169
170         pName = _StringConverter::CopyToCharArrayN(name);
171         SysTryReturnResult(NID_IO, pName != null, E_OUT_OF_MEMORY, "Not enough memory");
172
173         socketName.append("/tmp/");
174         socketName.append(pName);
175
176         delete[] pName;
177
178         socketNameLength = socketName.size() + 1;
179         SysTryReturnResult(NID_IO, socketNameLength < 108, E_INVALID_ARG, "Server name is too long");
180
181         if (!__runOnCallerThread)
182         {
183                 __pHandlerGMainContext = g_main_context_new();
184                 __pHandlerGMainLoop = g_main_loop_new(__pHandlerGMainContext, FALSE);
185
186         }
187         else
188         {
189                 __pHandlerGMainContext = g_main_context_get_thread_default();
190                 if (__pHandlerGMainContext == null) // is global?
191                 {
192                         __pHandlerGMainContext = g_main_context_default();
193                         if (__pHandlerGMainContext == null)
194                         {
195                                 return E_SYSTEM;
196                         }
197                 }
198         }
199
200         ret = unlink(socketName.c_str());
201         SysTryLog(NID_IO, ret == 0, "Unlink a socket has failed.. but it is not a problem.");
202
203         serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
204         SysTryReturnResult(NID_IO, serverSocket != -1, E_SYSTEM, "Failed to create a socket.");
205
206         // SMACK (Add a * label to socket)
207         if(smack_fsetlabel(serverSocket, "@", SMACK_LABEL_IPOUT) != 0)
208         {
209                 SysTryCatch(NID_IO, errno == EOPNOTSUPP, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] SMACK labeling failed");
210                 SysLog(NID_IO, "Kernel doesn't have Smack.");
211         }
212
213         if(smack_fsetlabel(serverSocket, "*", SMACK_LABEL_IPIN) != 0)
214         {
215                 SysTryCatch(NID_IO, errno == EOPNOTSUPP, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] SMACK labeling failed");
216                 SysLog(NID_IO, "Kernel doesn't have Smack.");
217         }
218
219         bzero(&serverAddress, sizeof(serverAddress));
220         serverAddress.sun_family = AF_UNIX;
221         strncpy(serverAddress.sun_path, socketName.c_str(), socketNameLength);
222         serverLen = sizeof(serverAddress);
223
224         ret = bind(serverSocket, (const struct sockaddr*) &serverAddress, serverLen);
225         SysTryCatch(NID_IO, ret != -1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to bind a socket(%d, %s): %s", serverSocket,
226                            socketName.c_str(), strerror(errno));
227
228         ret = chmod(socketName.c_str(), 0666);
229         SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to change permission of a socket(%d, %s): %s", serverSocket,
230                            socketName.c_str(), strerror(errno));
231
232         ret = listen(serverSocket, 15);
233         SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to listen a socket(%d, %s): %s", serverSocket,
234                            socketName.c_str(), strerror(errno));
235
236         pGIOChannel = g_io_channel_unix_new(serverSocket);
237         SysTryCatch(NID_IO, pGIOChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
238
239         // socket will be closed when pGIOChannel is deleted.
240         g_io_channel_set_close_on_unref(pGIOChannel, TRUE);
241         serverSocket = -1;
242
243         pGSource = g_io_create_watch(pGIOChannel, (GIOCondition) (G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
244         SysTryCatch(NID_IO, pGSource != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
245                            "[E_OUT_OF_MEMORY] Failed to create watch for socket.");
246
247         // channel will be delete when pGSource is deleted.
248         g_io_channel_unref(pGIOChannel);
249         pGIOChannel = null;
250
251         g_source_set_callback(pGSource, (GSourceFunc) OnConnectionRequest, this, NULL);
252         g_source_attach(pGSource, __pHandlerGMainContext);
253
254         if (__runOnCallerThread)
255         {
256                 __pListener->OnIpcServerStarted(*this);
257         }
258         else
259         {
260                 ret = pthread_create(&__handlerThread, null, &ThreadProc, (void*) this);
261                 SysTryCatch(NID_IO, ret == 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to an IPC thread");
262         }
263
264         __pConnectGSource = pGSource;
265
266         return E_SUCCESS;
267
268 CATCH:
269         if (pGIOChannel != null)
270         {
271                 g_io_channel_unref(pGIOChannel);
272         }
273
274         if (serverSocket != -1)
275         {
276                 close(serverSocket);
277         }
278
279         if (runOnCallerThread && __pHandlerGMainContext)
280         {
281                 g_main_context_unref(__pHandlerGMainContext);
282                 __pHandlerGMainContext = null;
283         }
284
285         return r;
286 }
287
288 struct HelloMessage
289 {
290         int pid;
291         bool reverse;  // true if the connection is for reverse message
292         char appId[256];
293 };
294
295 gboolean
296 IpcServer::OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data)
297 {
298         IpcServer* pIpcServer = (IpcServer*) data;
299         GError* pGError = null;
300         HelloMessage helloMessage;
301         _ClientInfo* pClientInfo = null;
302         _ChannelInfo* pChannelInfo = null;
303         GSource* pGSource = null;
304         GIOChannel* pChannel = null;
305
306         int server = -1;
307         int client = -1;
308         struct sockaddr_un clientAddress;
309         socklen_t clientLen = sizeof(clientAddress);
310
311         SysAssertf(pIpcServer != null, "Not yet constructed. Construct() should be called before use.\n");
312         SysAssertf(pIpcServer->__pListener != null, "Listener is null.\n");
313
314         server = g_io_channel_unix_get_fd(source);
315
316         client = accept(server, (struct sockaddr*) &clientAddress, &clientLen);
317         SysTryCatch(NID_IO, client != -1, , E_SYSTEM, "[E_SYSTEM] Accept failed.");
318
319         read(client, &helloMessage, sizeof(helloMessage));
320         helloMessage.appId[255] = '\0';
321
322         pChannel = g_io_channel_unix_new(client);
323         SysTryCatch(NID_IO, pChannel != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
324
325         g_io_channel_set_encoding(pChannel, NULL, &pGError);
326         g_io_channel_set_flags(pChannel, G_IO_FLAG_NONBLOCK, &pGError);
327
328         g_io_channel_set_close_on_unref(pChannel, TRUE);
329         client = -1;
330
331         pClientInfo = pIpcServer->__clients[helloMessage.pid];
332         if (pClientInfo == null) // first connection request from this client
333         {
334                 pClientInfo = new (std::nothrow) _ClientInfo;
335                 SysTryCatch(NID_IO, pClientInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
336
337                 pClientInfo->pIpcServer = pIpcServer;
338                 pClientInfo->clientId = helloMessage.pid;
339                 pClientInfo->appId.Append((wchar_t*) helloMessage.appId);
340                 pClientInfo->pReverseChannel = null;
341
342                 pIpcServer->__clients[helloMessage.pid] = pClientInfo;
343
344                 pIpcServer->__pCurrentClientInfo = pClientInfo;
345                 pIpcServer->__pListener->OnIpcClientConnected(*pIpcServer, helloMessage.pid);
346                 pIpcServer->__pCurrentClientInfo = null;
347         }
348
349         if (helloMessage.reverse)
350         {
351                 pClientInfo->pReverseChannel = pChannel;
352         }
353         else
354         {
355                 pChannelInfo = new (std::nothrow) _ChannelInfo;
356                 SysTryCatch(NID_IO, pChannelInfo != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
357
358                 pGSource = g_io_create_watch(pChannel, (GIOCondition) (G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
359                 g_source_set_callback(pGSource, (GSourceFunc) OnReadMessage, pChannelInfo, NULL);
360                 g_source_attach(pGSource, pIpcServer->__pHandlerGMainContext);
361
362                 pChannelInfo->pClientInfo = pClientInfo;
363                 pChannelInfo->pGIOChannel = pChannel;
364                 pChannelInfo->pGSource = pGSource;
365
366                 pClientInfo->channels.push_back(pChannelInfo);
367         }
368
369         return true;
370
371 CATCH:
372         if (pChannel != null)
373         {
374                 g_io_channel_unref(pChannel);
375         }
376
377         if (client != -1)
378         {
379                 close(client);
380         }
381
382         return true;
383 }
384
385 int
386 IpcServer::GetClientId(void) const
387 {
388         if (__pCurrentClientInfo)
389         {
390                 return __pCurrentClientInfo->clientId;
391         }
392
393         return -1;
394 }
395
396 AppId
397 IpcServer::GetClientApplicationId(void) const
398 {
399         static String nullString;
400
401         if (__pCurrentClientInfo)
402         {
403                 return __pCurrentClientInfo->appId;
404         }
405
406         return nullString;
407 }
408
409 gboolean
410 IpcServer::HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data)
411 {
412         GError* pGError = NULL;
413         GIOStatus status;
414         IPC::Message* pMessage = NULL;
415         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
416         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
417
418         if (condition & G_IO_HUP)
419         {
420                 SysLog(NID_IO, "Connection closed");
421                 int clientId = pClientInfo->clientId;
422
423                 g_io_channel_shutdown(source, FALSE, &pGError);
424
425                 for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
426                 {
427                         if (pChannelInfo == pClientInfo->channels[i])
428                         {
429                                 pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
430
431                                 // Do not destroy a source in a dispatch callback
432                                 // because main loop will do it if the callback return FALSE.
433                                 pChannelInfo->destroySource = false;
434                                 delete pChannelInfo;
435
436                                 break;
437                         }
438                 }
439
440                 if (pClientInfo->channels.size() == 0)
441                 {
442                         SysLog(NID_IO, "All connections of client(%d) are closed. delete client info", clientId);
443
444                         __pListener->OnIpcClientDisconnected(*this, clientId);
445
446                         __clients[clientId] = null;
447
448                         delete pClientInfo;
449                 }
450
451                 return FALSE;
452         }
453         else if (condition & G_IO_IN)
454         {
455                 gsize readSize = 0;
456                 const char* pStart = NULL;
457                 const char* pEnd = NULL;
458                 const char* pEndOfMessage = NULL;
459
460                 while (true)
461                 {
462                         pGError = null;
463                         status = g_io_channel_read_chars(source, (char*) __messageBuffer, __MAX_MESSAGE_BUFFER_SIZE, &readSize, &pGError);
464                         if (status != G_IO_STATUS_NORMAL)
465                         {
466                                 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
467                                 {
468                                         if (status == G_IO_STATUS_EOF)
469                                         {
470                                                 SysLog(NID_IO, "G_IO_STATUS_EOF, the connection is closed.");
471                                         }
472                                         else
473                                         {
474                                                 SysLog(NID_IO, "G_IO_STATUS_ERROR, the connection is closed. ");
475                                         }
476
477                                         pGError = null;
478                                         g_io_channel_shutdown(source, FALSE, &pGError);
479
480                                         int clientId = pClientInfo->clientId;
481
482                                         for (unsigned int i = 0; i < pClientInfo->channels.size(); i++)
483                                         {
484                                                 if (pChannelInfo == pClientInfo->channels[i])
485                                                 {
486                                                         pClientInfo->channels.erase(pClientInfo->channels.begin() + i);
487
488                                                         pChannelInfo->destroySource = false;
489                                                         delete pChannelInfo;
490                                                         break;
491                                                 }
492                                         }
493
494                                         if (pClientInfo->channels.size() == 0)
495                                         {
496                                                 SysLog(NID_IO, "All connections of client(%d) are closed normally by the client.", clientId);
497
498                                                 if (__pListener)
499                                                 {
500                                                         __pListener->OnIpcClientDisconnected(*this, clientId);
501                                                 }
502
503                                                 __clients[clientId] = null;
504
505                                                 delete pClientInfo;
506                                         }
507
508                                         return FALSE;
509                                 }
510                         }
511
512                         if (readSize == 0)
513                         {
514                                 break;
515                         }
516
517                         if (__pending.empty())
518                         {
519                                 pStart = __messageBuffer;
520                                 pEnd = pStart + readSize;
521                         }
522                         else
523                         {
524                                 __pending.append(__messageBuffer, readSize);
525                                 pStart = __pending.data();
526                                 pEnd = pStart + __pending.size();
527                         }
528
529                         while (true)
530                         {
531                                 pEndOfMessage = IPC::Message::FindNext(pStart, pEnd);
532                                 if (pEndOfMessage == NULL)
533                                 {
534                                         __pending.assign(pStart, pEnd - pStart);
535                                         break;
536                                 }
537
538                                 pMessage = new (std::nothrow) IPC::Message(pStart, pEndOfMessage - pStart);
539                                 SysTryReturn(NID_IO, pMessage != null, FALSE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
540
541                                 __pCurrentChannel = source;
542
543                                 if (__pListener)
544                                 {
545                                         __pListener->OnIpcRequestReceived(*this, *pMessage);
546                                 }
547
548                                 delete pMessage;
549
550                                 __pCurrentChannel = NULL;
551
552                                 pStart = pEndOfMessage;
553                         }
554                 }
555         }
556         else
557         {
558                 // empty statement
559         }
560
561         return TRUE;
562 }
563
564 gboolean
565 IpcServer::OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data)
566 {
567         gboolean ret = FALSE;
568         _ChannelInfo* pChannelInfo = (_ChannelInfo*) data;
569         _ClientInfo* pClientInfo = pChannelInfo->pClientInfo;
570         IpcServer* pIpcServer = (IpcServer*) pClientInfo->pIpcServer;
571
572         pIpcServer->__pCurrentClientInfo = pClientInfo;
573         ret = pIpcServer->HandleReceivedMessage(source, condition, data);
574         pIpcServer->__pCurrentClientInfo = null;
575
576         return ret;
577 }
578
579 void*
580 IpcServer::ThreadProc(void* pParam)
581 {
582         IpcServer* pIpcServer = (IpcServer*) pParam;
583         if (pIpcServer != NULL)
584         {
585                 pIpcServer->Run(NULL);
586         }
587
588         return NULL;
589 }
590
591 void
592 IpcServer::Run(void* pParam)
593 {
594         result r = E_SUCCESS;
595
596         if (__pListener == null)
597         {
598                 return;
599         }
600
601         __pEventDispatcher = new (std::nothrow) _EventDispatcher;
602         SysTryReturnVoidResult(NID_IO, __pEventDispatcher != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
603
604         r = __pEventDispatcher->Construct(__pHandlerGMainContext);
605         if (IsFailed(r))
606         {
607                 delete __pEventDispatcher;
608                 __pEventDispatcher = null;
609         }
610
611         __pListener->OnIpcServerStarted(*this);
612
613         g_main_loop_run(__pHandlerGMainLoop);
614
615         __pListener->OnIpcServerStopped(*this);
616 }
617
618 result
619 IpcServer::Start(void)
620 {
621         return E_SUCCESS;
622 }
623
624 String
625 IpcServer::GetName(void) const
626 {
627         return __name;
628 }
629
630 result
631 IpcServer::Stop(void)
632 {
633         result r = E_SUCCESS;
634         int ret = 0;
635
636         SysTryReturnResult(NID_IO, __pListener != null, E_SYSTEM, "Listener is null.");
637
638         if (!__runOnCallerThread)
639         {
640                 pthread_t self = pthread_self();
641
642                 if (__pHandlerGMainLoop)
643                 {
644                         g_main_loop_quit(__pHandlerGMainLoop);
645                 }
646
647                 if (__handlerThread != self)
648                 {
649                         ret = pthread_join(__handlerThread, null);
650                         SysTryLog(NID_IO, ret == 0, "Join an IPC thread returns an error");
651                 }
652         }
653         else
654         {
655                 __pListener->OnIpcServerStopped(*this);
656         }
657
658         return r;
659 }
660
661 bool
662 IpcServer::Send(IPC::Message* msg)
663 {
664         gsize remain = 0;
665         gsize written = 0;
666         char* pData = NULL;
667         GError* pGError = NULL;
668
669
670         pData = (char*) msg->data();
671         remain = msg->size();
672
673         if (msg->is_reply())
674         {
675                 while (remain > 0)
676                 {
677                         pGError = NULL;
678                         g_io_channel_write_chars(__pCurrentChannel, (char*) pData, remain, &written, &pGError);
679
680                         remain -= written;
681                         pData += written;
682                 }
683
684                 g_io_channel_flush(__pCurrentChannel, &pGError);
685         }
686         else
687         {
688                 // empty statement;
689         }
690
691         return true;
692 }
693
694 result
695 IpcServer::SendResponse(int client, IPC::Message* pMessage)
696 {
697         result r = E_SUCCESS;
698         gsize remain = 0;
699         gsize written = 0;
700         char* pData = null;
701         GError* pGError = null;
702         _ClientInfo* pClientInfo = null;
703
704         SysTryReturn(NID_IO, client >= 0 && pMessage != null, false, E_INVALID_ARG,
705                                 "[E_INVALID_ARG] pMessage(0x%x) is null or clinet(%d) < 0", pMessage,
706                                 client);
707         SysTryCatch(NID_IO, !pMessage->is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
708
709         pClientInfo = __clients[client];
710         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
711                            "[E_INVALID_ARG] client(%d) has not been registered.",
712                            client);
713
714         pData = (char*) pMessage->data();
715         remain = pMessage->size();
716
717         while (remain > 0)
718         {
719                 pGError = NULL;
720                 g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
721                 SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
722
723                 remain -= written;
724                 pData += written;
725         }
726
727         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
728
729         delete pMessage;
730
731         return E_SUCCESS;
732
733 CATCH:
734         delete pMessage;
735         return r;
736 }
737
738 result
739 IpcServer::SendResponse(int client, const IPC::Message& message)
740 {
741         result r = E_SUCCESS;
742         gsize remain = 0;
743         gsize written = 0;
744         char* pData = null;
745         GError* pGError = null;
746         _ClientInfo* pClientInfo = null;
747
748         SysTryReturn(NID_IO, client >= 0, false, E_INVALID_ARG, "[E_INVALID_ARG] clinet(%d) < 0", client);
749         SysTryCatch(NID_IO, !message.is_sync(), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Can't send sync. messagee.");
750
751         pClientInfo = __clients[client];
752         SysTryCatch(NID_IO, pClientInfo != null, r = E_INVALID_ARG, E_INVALID_ARG,
753                            "[E_INVALID_ARG] client(%d) has not been registered.",
754                            client);
755
756         pData = (char*) message.data();
757         remain = message.size();
758
759         while (remain > 0)
760         {
761                 pGError = NULL;
762                 g_io_channel_write_chars(pClientInfo->pReverseChannel, (char*) pData, remain, &written, &pGError);
763                 SysTryCatch(NID_IO, pGError == null, , E_SYSTEM, "[E_SYSTEM] Error occurred during writing message to socket.");
764
765                 remain -= written;
766                 pData += written;
767         }
768
769         g_io_channel_flush(pClientInfo->pReverseChannel, &pGError);
770
771         return E_SUCCESS;
772
773 CATCH:
774         return r;
775 }