tizen 2.4 release
[framework/web/wrt-commons.git] / modules / socket / include / dpl / socket / abstract_socket.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        abstract_socket.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file of abstract socket
21  */
22 #ifndef DPL_ABSTRACT_SOCKET_H
23 #define DPL_ABSTRACT_SOCKET_H
24
25 #include <dpl/abstract_waitable_input_output.h>
26 #include <dpl/event/event_support.h>
27 #include <dpl/generic_event.h>
28 #include <dpl/exception.h>
29 #include <dpl/address.h>
30
31 namespace DPL {
32 namespace Socket {
33 namespace AbstractSocketEvents {
34 // Successfuly connected to server socket
35 DECLARE_GENERIC_EVENT_0(ConnectedEvent)
36
37 // New connection occurred and need to be accepted
38 DECLARE_GENERIC_EVENT_0(AcceptEvent)
39
40 // Connection has read data waiting
41 DECLARE_GENERIC_EVENT_0(ReadEvent)
42
43 // Connection write buffer is now empty again and ready to write more
44 DECLARE_GENERIC_EVENT_0(WriteEvent)
45 } // namespace AbstractSocketEvents
46
47 class AbstractSocket :
48     public AbstractWaitableInputOutput,
49     public DPL::Event::EventSupport<AbstractSocketEvents::ConnectedEvent>,
50     public DPL::Event::EventSupport<AbstractSocketEvents::AcceptEvent>,
51     public DPL::Event::EventSupport<AbstractSocketEvents::ReadEvent>,
52     public DPL::Event::EventSupport<AbstractSocketEvents::WriteEvent>
53 {
54   public:
55     class Exception
56     {
57       public:
58         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)       ///< Base abstract
59                                                            // socket exception
60
61         DECLARE_EXCEPTION_TYPE(Base, OpenFailed)           ///< Fatal error
62                                                            // occurred during
63                                                            // open socket
64                                                            // descriptor. Socket
65                                                            // state is
66                                                            // inconsistent and
67                                                            // it should be not
68                                                            // used anymore.
69
70         DECLARE_EXCEPTION_TYPE(Base, ConnectFailed)        ///< Fatal error
71                                                            // occurred during
72                                                            // connect. Socket
73                                                            // state is
74                                                            // inconsistent and
75                                                            // it should be not
76                                                            // used anymore.
77                                                            ///< Warning: This
78                                                            // exception does not
79                                                            // mean that socket
80                                                            // did not succeed to
81                                                            // connect, see
82                                                            // CannotConnect
83                                                            ///< for this
84                                                            // specific scenario
85
86         DECLARE_EXCEPTION_TYPE(Base, SetNonBlockingFailed) ///< Fatal error
87                                                            // occurred during
88                                                            // setting socket to
89                                                            // non-blocking mode.
90                                                            // Socket state is
91                                                            // inconsistent and
92                                                            // it should be not
93                                                            // used anymore.
94
95         DECLARE_EXCEPTION_TYPE(Base, BindFailed)           ///< Fatal error
96                                                            // occurred during
97                                                            // bind. Socket state
98                                                            // is inconsistent
99                                                            // and it should be
100                                                            // not used anymore.
101
102         DECLARE_EXCEPTION_TYPE(Base, AcceptFailed)         ///< Fatal error
103                                                            // occurred during
104                                                            // accept. Socket
105                                                            // state is
106                                                            // inconsistent and
107                                                            // it should be not
108                                                            // used anymore.
109
110         DECLARE_EXCEPTION_TYPE(Base, ListenFailed)         ///< Fatal error
111                                                            // occurred during
112                                                            // listen. Socket
113                                                            // state is
114                                                            // inconsistent and
115                                                            // it should be not
116                                                            // used anymore.
117
118         DECLARE_EXCEPTION_TYPE(Base, CloseFailed)          ///< Fatal error
119                                                            // occurred during
120                                                            // close. Socket
121                                                            // state is
122                                                            // inconsistent and
123                                                            // it should be not
124                                                            // used anymore.
125
126         DECLARE_EXCEPTION_TYPE(Base, ReadFailed)           ///< Fatal error
127                                                            // occurred during
128                                                            // read. Socket state
129                                                            // is inconsistent
130                                                            // and it should be
131                                                            // not used anymore.
132                                                            ///< Warning: This
133                                                            // exception does not
134                                                            // mean that
135                                                            // connection was
136                                                            // broken, see
137                                                            // ConnectionBroken
138                                                            ///< for this
139                                                            // specific scenario
140
141         DECLARE_EXCEPTION_TYPE(Base, WriteFailed)          ///< Fatal error
142                                                            // occurred during
143                                                            // write. Socket
144                                                            // state is
145                                                            // inconsistent and
146                                                            // it should be not
147                                                            // used anymore.
148                                                            ///< Warning: This
149                                                            // exception does not
150                                                            // mean that
151                                                            // connection was
152                                                            // broken, see
153                                                            // ConnectionBroken
154                                                            ///< for this
155                                                            // specific scenario
156
157         DECLARE_EXCEPTION_TYPE(Base, GetPeerNameFailed)    ///< Fatal error
158                                                            // occurred during
159                                                            // getpeername or
160                                                            // getsockname.
161                                                            // Socket state is
162                                                            // inconsistent and
163                                                            // it should be not
164                                                            // used anymore.
165
166         DECLARE_EXCEPTION_TYPE(Base, CannotConnect)        ///< Cannot connect
167                                                            // to remote socket.
168                                                            // This is not fatal
169                                                            // error, socket
170                                                            // state is still
171                                                            // consistent and it
172                                                            // can be
173                                                            // reconnected.
174
175         DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)     ///< Connection was
176                                                            // broken. This is
177                                                            // not fatal error,
178                                                            // socket state is
179                                                            // still consistent
180                                                            // and it can be
181                                                            // reconnected.
182     };
183
184   public:
185     virtual ~AbstractSocket() {}
186
187     // Connect to remote host
188     virtual void Connect(const Address &address) = 0;
189
190     // Open empty, unconnected socket
191     virtual void Open() = 0;
192
193     // Close connection
194     virtual void Close() = 0;
195
196     // Bind server socket address
197     virtual void Bind(const Address &address) = 0;
198
199     // Begin listening for incoming connections
200     virtual void Listen(int backlog) = 0;
201
202     // Accept waiting connection and create derived class connection
203     // One should cast resulting pointer to derived class
204     virtual AbstractSocket *Accept() = 0;
205
206     // Local socket address
207     virtual Address GetLocalAddress() const = 0;
208
209     // Remote socket address
210     virtual Address GetRemoteAddress() const = 0;
211 };
212 }
213 } // namespace DPL
214
215 #endif // DPL_ABSTRACT_SOCKET_H