AbstractSocket.h revision c9dd7d0d
1/*
2 * Copyright 2011-2016, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _ABSTRACT_SOCKET_H
6#define _ABSTRACT_SOCKET_H
7
8
9#include <DataIO.h>
10#include <NetworkAddress.h>
11
12#include <sys/socket.h>
13
14
15class BAbstractSocket : public BDataIO {
16public:
17								BAbstractSocket();
18								BAbstractSocket(const BAbstractSocket& other);
19	virtual						~BAbstractSocket();
20
21			status_t			InitCheck() const;
22
23	virtual status_t			Bind(const BNetworkAddress& local, bool reuseAddr) = 0;
24	virtual bool				IsBound() const;
25	virtual	bool				IsListening() const;
26
27	virtual	status_t			Listen(int backlog = 10);
28	virtual	status_t			Accept(BAbstractSocket*& _socket) = 0;
29
30	virtual	status_t			Connect(const BNetworkAddress& peer,
31									bigtime_t timeout = B_INFINITE_TIMEOUT) = 0;
32	virtual	bool				IsConnected() const;
33	virtual	void				Disconnect();
34
35	virtual	status_t			SetTimeout(bigtime_t timeout);
36	virtual	bigtime_t			Timeout() const;
37
38	virtual	const BNetworkAddress& Local() const;
39	virtual	const BNetworkAddress& Peer() const;
40
41	virtual	size_t				MaxTransmissionSize() const;
42
43	virtual	status_t			WaitForReadable(bigtime_t timeout
44										= B_INFINITE_TIMEOUT) const;
45	virtual	status_t			WaitForWritable(bigtime_t timeout
46										= B_INFINITE_TIMEOUT) const;
47
48			int					Socket() const;
49
50protected:
51			status_t			Bind(const BNetworkAddress& local,
52									bool reuseAddr, int type);
53			status_t			Connect(const BNetworkAddress& peer, int type,
54									bigtime_t timeout = B_INFINITE_TIMEOUT);
55			status_t			AcceptNext(int& _acceptedSocket,
56									BNetworkAddress& _peer);
57
58private:
59			status_t			_OpenIfNeeded(int family, int type);
60			status_t			_UpdateLocalAddress();
61			status_t			_WaitFor(int flags, bigtime_t timeout) const;
62
63protected:
64			status_t			fInitStatus;
65			int					fSocket;
66			BNetworkAddress		fLocal;
67			BNetworkAddress		fPeer;
68			bool				fIsBound;
69			bool				fIsConnected;
70			bool				fIsListening;
71};
72
73
74#endif	// _ABSTRACT_SOCKET_H
75