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>
33 namespace AbstractSocketEvents {
34 // Successfuly connected to server socket
35 DECLARE_GENERIC_EVENT_0(ConnectedEvent)
37 // New connection occurred and need to be accepted
38 DECLARE_GENERIC_EVENT_0(AcceptEvent)
40 // Connection has read data waiting
41 DECLARE_GENERIC_EVENT_0(ReadEvent)
43 // Connection write buffer is now empty again and ready to write more
44 DECLARE_GENERIC_EVENT_0(WriteEvent)
45 } // namespace AbstractSocketEvents
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>
58 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base) ///< Base abstract
61 DECLARE_EXCEPTION_TYPE(Base, OpenFailed) ///< Fatal error
70 DECLARE_EXCEPTION_TYPE(Base, ConnectFailed) ///< Fatal error
86 DECLARE_EXCEPTION_TYPE(Base, SetNonBlockingFailed) ///< Fatal error
95 DECLARE_EXCEPTION_TYPE(Base, BindFailed) ///< Fatal error
102 DECLARE_EXCEPTION_TYPE(Base, AcceptFailed) ///< Fatal error
110 DECLARE_EXCEPTION_TYPE(Base, ListenFailed) ///< Fatal error
118 DECLARE_EXCEPTION_TYPE(Base, CloseFailed) ///< Fatal error
126 DECLARE_EXCEPTION_TYPE(Base, ReadFailed) ///< Fatal error
128 // read. Socket state
133 // exception does not
141 DECLARE_EXCEPTION_TYPE(Base, WriteFailed) ///< Fatal error
149 // exception does not
157 DECLARE_EXCEPTION_TYPE(Base, GetPeerNameFailed) ///< Fatal error
166 DECLARE_EXCEPTION_TYPE(Base, CannotConnect) ///< Cannot connect
175 DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken) ///< Connection was
185 virtual ~AbstractSocket() {}
187 // Connect to remote host
188 virtual void Connect(const Address &address) = 0;
190 // Open empty, unconnected socket
191 virtual void Open() = 0;
194 virtual void Close() = 0;
196 // Bind server socket address
197 virtual void Bind(const Address &address) = 0;
199 // Begin listening for incoming connections
200 virtual void Listen(int backlog) = 0;
202 // Accept waiting connection and create derived class connection
203 // One should cast resulting pointer to derived class
204 virtual AbstractSocket *Accept() = 0;
206 // Local socket address
207 virtual Address GetLocalAddress() const = 0;
209 // Remote socket address
210 virtual Address GetRemoteAddress() const = 0;
215 #endif // DPL_ABSTRACT_SOCKET_H