Add an E_INVALID_CONNECTION in _IpcClient::SendRequest()
[platform/framework/native/appfw.git] / src / io / inc / FIo_IpcClient.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FIo_IpcClient.h
19  * @brief       This is the header file for the _IpcClient class.
20  *
21  * This file contains the declarations of _IpcClient.
22  */
23
24
25 #ifndef _FIO_INTERNAL_IPC_CLIENT_H_
26 #define _FIO_INTERNAL_IPC_CLIENT_H_
27
28 #include <string>
29 #include <pthread.h>
30 #include <glib.h>
31
32 #include <ipc/ipc_message_macros.h>
33 #include <ipc/ipc_message_utils.h>
34
35 #include <FBaseResult.h>
36 #include <FBaseObject.h>
37 #include <FBaseString.h>
38 #include <FOspConfig.h>
39
40
41 namespace Tizen { namespace Base { namespace Runtime
42 {
43 class Mutex;
44 }}}
45 namespace Tizen { namespace Io
46 {
47
48 class _IIpcClientEventListener;
49
50 /**
51  * @class _IpcClient
52  * @brief This class provides methods for sending a message to an IPC server.
53  *
54  */
55 class _OSP_EXPORT_ _IpcClient
56         : Tizen::Base::Object
57 {
58 public:
59         _IpcClient(void);
60
61         virtual ~_IpcClient(void);
62
63         /**
64          * Constructs the instance of this class.
65          *
66          * @return An error code
67          * @param[in] serverName        The name of the server
68          * @param[in] pListener Set if the client want to handle a message from the IPC server.
69          *                                 @c null, otherwise.
70          * @exception E_SUCCESS         The method was successful.
71          * @exception E_OUT_OF_MEMORY   Insufficient memory.
72          * @exception E_SYSTEM          A system error occurred.
73          */
74         result Construct(const Tizen::Base::String& serverName, const _IIpcClientEventListener* pListener = null);
75
76         /**
77          * Returns the name of the IPC server.
78          *
79          * @return The name of the IPC server.
80          */
81         Tizen::Base::String GetName(void) const;
82
83         /**
84          * Sends a request message to an IPC server.
85          *
86          * @code
87          *
88          * int
89          * CalculatorProxy::Add(int a , int b)
90          * {
91          *    int c = 0;
92          *
93          *    My_sum mySum(a, b, &c);
94          *
95          *    __pIpcClient->SendRequest(mySum);
96          *
97          *    return c;
98          * }
99          *
100          * @endcode
101          * @return An error code
102          * @param[in] message   The message to send
103          * @exception E_SUCCESS         The method was successful.
104          * @exception E_RESOURCE_UNAVAILABLE    The socket buffer is full.
105          * @exception E_INVALID_CONNECTION      The socket connection is closed.
106          * @exception E_OUT_OF_MEMORY   Insufficient memory.
107          * @exception E_SYSTEM          A system error occurred.
108          *
109          */
110         result SendRequest(const IPC::Message& message);
111
112         result SendRequest(IPC::Message* pMessage);
113
114 private:
115         _IpcClient(const _IpcClient& value);
116
117         _IpcClient& operator =(const _IpcClient& value);
118
119         result Send(IPC::Message* pMessage);
120
121         result SendAsync(IPC::Message* pMessage);
122
123         result SendSync(IPC::Message* pMessage);
124
125         result MakeConnection(bool forReverse = false);
126
127         int AcquireFd(void);
128
129         void ReleaseFd(int fd);
130
131         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
132
133         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition);
134
135 private:
136         GSource* __pReverseSource;
137
138         std::vector <int> __fds;
139         int __fdCount;
140         Tizen::Base::Runtime::Mutex* __pFdLock;
141         Tizen::Base::String __name;
142         _IIpcClientEventListener* __pListener;
143
144         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
145         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
146         std::string __pending;
147 };
148
149 } } // Tizen::Io
150
151 #endif // _FIO_INTERNAL_IPC_CLIENT_H_