2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file abstract_socket.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the header file of abstract socket
22 #ifndef DPL_ABSTRACT_SOCKET_H
23 #define DPL_ABSTRACT_SOCKET_H
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>
35 namespace AbstractSocketEvents
37 // Successfuly connected to server socket
38 DECLARE_GENERIC_EVENT_0(ConnectedEvent)
40 // New connection occurred and need to be accepted
41 DECLARE_GENERIC_EVENT_0(AcceptEvent)
43 // Connection has read data waiting
44 DECLARE_GENERIC_EVENT_0(ReadEvent)
46 // Connection write buffer is now empty again and ready to write more
47 DECLARE_GENERIC_EVENT_0(WriteEvent)
48 } // namespace AbstractSocketEvents
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>
61 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base) ///< Base abstract socket exception
63 DECLARE_EXCEPTION_TYPE(Base, OpenFailed) ///< Fatal error occurred during open socket descriptor. Socket state is inconsistent and it should be not used anymore.
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
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.
71 DECLARE_EXCEPTION_TYPE(Base, BindFailed) ///< Fatal error occurred during bind. Socket state is inconsistent and it should be not used anymore.
73 DECLARE_EXCEPTION_TYPE(Base, AcceptFailed) ///< Fatal error occurred during accept. Socket state is inconsistent and it should be not used anymore.
75 DECLARE_EXCEPTION_TYPE(Base, ListenFailed) ///< Fatal error occurred during listen. Socket state is inconsistent and it should be not used anymore.
77 DECLARE_EXCEPTION_TYPE(Base, CloseFailed) ///< Fatal error occurred during close. Socket state is inconsistent and it should be not used anymore.
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
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
87 DECLARE_EXCEPTION_TYPE(Base, GetPeerNameFailed) ///< Fatal error occurred during getpeername or getsockname. Socket state is inconsistent and it should be not used anymore.
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.
91 DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken) ///< Connection was broken. This is not fatal error, socket state is still consistent and it can be reconnected.
95 virtual ~AbstractSocket() {}
97 // Connect to remote host
98 virtual void Connect(const Address &address) = 0;
100 // Open empty, unconnected socket
101 virtual void Open() = 0;
104 virtual void Close() = 0;
106 // Bind server socket address
107 virtual void Bind(const Address &address) = 0;
109 // Begin listening for incoming connections
110 virtual void Listen(int backlog) = 0;
112 // Accept waiting connection and create derived class connection
113 // One should cast resulting pointer to derived class
114 virtual AbstractSocket *Accept() = 0;
116 // Local socket address
117 virtual Address GetLocalAddress() const = 0;
119 // Remote socket address
120 virtual Address GetRemoteAddress() const = 0;
126 #endif // DPL_ABSTRACT_SOCKET_H