xref: /haiku/headers/os/net/AbstractSocket.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2011, 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 
15 class BAbstractSocket : public BDataIO {
16 public:
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) = 0;
24 	virtual bool				IsBound() const;
25 
26 	virtual	status_t			Connect(const BNetworkAddress& peer,
27 									bigtime_t timeout = B_INFINITE_TIMEOUT) = 0;
28 	virtual	bool				IsConnected() const;
29 	virtual	void				Disconnect();
30 
31 	virtual	status_t			SetTimeout(bigtime_t timeout);
32 	virtual	bigtime_t			Timeout() const;
33 
34 	virtual	const BNetworkAddress& Local() const;
35 	virtual	const BNetworkAddress& Peer() const;
36 
37 	virtual	size_t				MaxTransmissionSize() const;
38 
39 	virtual	status_t			WaitForReadable(bigtime_t timeout
40 										= B_INFINITE_TIMEOUT) const;
41 	virtual	status_t			WaitForWritable(bigtime_t timeout
42 										= B_INFINITE_TIMEOUT) const;
43 
44 			int					Socket() const;
45 
46 protected:
47 			status_t			Bind(const BNetworkAddress& local, int type);
48 			status_t			Connect(const BNetworkAddress& peer, int type,
49 									bigtime_t timeout = B_INFINITE_TIMEOUT);
50 
51 private:
52 			status_t			_OpenIfNeeded(int family, int type);
53 			status_t			_UpdateLocalAddress();
54 			status_t			_WaitFor(int flags, bigtime_t timeout) const;
55 
56 protected:
57 			status_t			fInitStatus;
58 			int					fSocket;
59 			BNetworkAddress		fLocal;
60 			BNetworkAddress		fPeer;
61 			bool				fIsBound;
62 			bool				fIsConnected;
63 };
64 
65 
66 #endif	// _ABSTRACT_SOCKET_H
67