Update User Agent String
[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 {
33 namespace Socket
34 {
35 namespace AbstractSocketEvents
36 {
37 // Successfuly connected to server socket
38 DECLARE_GENERIC_EVENT_0(ConnectedEvent)
39
40 // New connection occurred and need to be accepted
41 DECLARE_GENERIC_EVENT_0(AcceptEvent)
42
43 // Connection has read data waiting
44 DECLARE_GENERIC_EVENT_0(ReadEvent)
45
46 // Connection write buffer is now empty again and ready to write more
47 DECLARE_GENERIC_EVENT_0(WriteEvent)
48 } // namespace AbstractSocketEvents
49
50 class AbstractSocket
51     : public AbstractWaitableInputOutput,
52       public DPL::Event::EventSupport<AbstractSocketEvents::ConnectedEvent>,
53       public DPL::Event::EventSupport<AbstractSocketEvents::AcceptEvent>,
54       public DPL::Event::EventSupport<AbstractSocketEvents::ReadEvent>,
55       public DPL::Event::EventSupport<AbstractSocketEvents::WriteEvent>
56 {
57 public:
58     class Exception
59     {
60     public:
61         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)       ///< Base abstract socket exception
62
63         DECLARE_EXCEPTION_TYPE(Base, OpenFailed)           ///< Fatal error occurred during open socket descriptor. Socket state is inconsistent and it should be not used anymore.
64
65         DECLARE_EXCEPTION_TYPE(Base, ConnectFailed)        ///< Fatal error occurred during connect. Socket state is inconsistent and it should be not used anymore.
66                                                            ///< Warning: This exception does not mean that socket did not succeed to connect, see CannotConnect
67                                                            ///< for this specific scenario
68
69         DECLARE_EXCEPTION_TYPE(Base, SetNonBlockingFailed) ///< Fatal error occurred during setting socket to non-blocking mode. Socket state is inconsistent and it should be not used anymore.
70
71         DECLARE_EXCEPTION_TYPE(Base, BindFailed)           ///< Fatal error occurred during bind. Socket state is inconsistent and it should be not used anymore.
72
73         DECLARE_EXCEPTION_TYPE(Base, AcceptFailed)         ///< Fatal error occurred during accept. Socket state is inconsistent and it should be not used anymore.
74
75         DECLARE_EXCEPTION_TYPE(Base, ListenFailed)         ///< Fatal error occurred during listen. Socket state is inconsistent and it should be not used anymore.
76
77         DECLARE_EXCEPTION_TYPE(Base, CloseFailed)          ///< Fatal error occurred during close. Socket state is inconsistent and it should be not used anymore.
78
79         DECLARE_EXCEPTION_TYPE(Base, ReadFailed)           ///< Fatal error occurred during read. Socket state is inconsistent and it should be not used anymore.
80                                                            ///< Warning: This exception does not mean that connection was broken, see ConnectionBroken
81                                                            ///< for this specific scenario
82
83         DECLARE_EXCEPTION_TYPE(Base, WriteFailed)          ///< Fatal error occurred during write. Socket state is inconsistent and it should be not used anymore.
84                                                            ///< Warning: This exception does not mean that connection was broken, see ConnectionBroken
85                                                            ///< for this specific scenario
86
87         DECLARE_EXCEPTION_TYPE(Base, GetPeerNameFailed)    ///< Fatal error occurred during getpeername or getsockname. Socket state is inconsistent and it should be not used anymore.
88
89         DECLARE_EXCEPTION_TYPE(Base, CannotConnect)        ///< Cannot connect to remote socket. This is not fatal error, socket state is still consistent and it can be reconnected.
90
91         DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)     ///< Connection was broken. This is not fatal error, socket state is still consistent and it can be reconnected.
92     };
93
94 public:
95     virtual ~AbstractSocket() {}
96
97     // Connect to remote host
98     virtual void Connect(const Address &address) = 0;
99
100     // Open empty, unconnected socket
101     virtual void Open() = 0;
102
103     // Close connection
104     virtual void Close() = 0;
105
106     // Bind server socket address
107     virtual void Bind(const Address &address) = 0;
108
109     // Begin listening for incoming connections
110     virtual void Listen(int backlog) = 0;
111
112     // Accept waiting connection and create derived class connection
113     // One should cast resulting pointer to derived class
114     virtual AbstractSocket *Accept() = 0;
115
116     // Local socket address
117     virtual Address GetLocalAddress() const = 0;
118
119     // Remote socket address
120     virtual Address GetRemoteAddress() const = 0;
121 };
122
123 }
124 } // namespace DPL
125
126 #endif // DPL_ABSTRACT_SOCKET_H