xref: /haiku/headers/os/net/NetEndpoint.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef H_NETENDPOINT
6 #define H_NETENDPOINT
7 
8 
9 #include <BeBuild.h>
10 #include <Archivable.h>
11 #include <NetAddress.h>
12 #include <NetBuffer.h>
13 #include <SupportDefs.h>
14 
15 #include <sys/socket.h>
16 
17 
18 class BNetEndpoint : public BArchivable {
19 	public:
20 		BNetEndpoint(int type = SOCK_STREAM);
21 		BNetEndpoint(int family, int type, int protocol);
22 		BNetEndpoint(const BNetEndpoint& other);
23 		BNetEndpoint(BMessage* archive);
24 		virtual ~BNetEndpoint();
25 
26 		BNetEndpoint& operator=(const BNetEndpoint& other);
27 
28 		status_t InitCheck() const;
29 
30 		virtual	status_t Archive(BMessage* into, bool deep = true) const;
31 		static BArchivable* Instantiate(BMessage* archive);
32 
33 		status_t SetProtocol(int protocol);
34 		int SetOption(int32 option, int32 level, const void* data,
35 			unsigned int dataSize);
36 		int SetNonBlocking(bool on = true);
37 		int SetReuseAddr(bool on = true);
38 
39 		const BNetAddress& LocalAddr() const;
40 		const BNetAddress& RemoteAddr() const;
41 
42 		int Socket() const;
43 
44 		virtual void Close();
45 
46 		virtual status_t Bind(const BNetAddress& addr);
47 		virtual status_t Bind(int port = 0);
48 		virtual status_t Connect(const BNetAddress& addr);
49 		virtual status_t Connect(const char* addr, int port);
50 		virtual status_t Listen(int backlog = 5);
51 		virtual BNetEndpoint* Accept(int32 timeout = -1);
52 
53 		int Error() const;
54 		char* ErrorStr() const;
55 
56 		virtual int32 Send(const void* buffer, size_t size, int flags = 0);
57 		virtual int32 Send(BNetBuffer& buffer, int flags = 0);
58 
59 		virtual int32 SendTo(const void* buffer, size_t size,
60 			const BNetAddress& to, int flags = 0);
61 		virtual int32 SendTo(BNetBuffer& buffer, const BNetAddress& to,
62 			int flags = 0);
63 
64 		void SetTimeout(bigtime_t usec);
65 
66 		virtual int32 Receive(void* buffer, size_t size, int flags = 0);
67 		virtual int32 Receive(BNetBuffer& buffer, size_t size, int flags = 0);
68 		virtual int32 ReceiveFrom(void* buffer, size_t size, BNetAddress& from,
69 			int flags = 0);
70 		virtual int32 ReceiveFrom(BNetBuffer& buffer, size_t size,
71 			BNetAddress& from, int flags = 0);
72 
73 		virtual bool IsDataPending(bigtime_t timeout = 0);
74 
75 		// TODO: drop these compatibility cruft methods after R1
76 		status_t InitCheck();
77 		const BNetAddress& LocalAddr();
78 		const BNetAddress& RemoteAddr();
79 
80 	private:
81 		status_t _SetupSocket();
82 
83 		virtual	void _ReservedBNetEndpointFBCCruft1();
84 		virtual	void _ReservedBNetEndpointFBCCruft2();
85 		virtual	void _ReservedBNetEndpointFBCCruft3();
86 		virtual	void _ReservedBNetEndpointFBCCruft4();
87 		virtual	void _ReservedBNetEndpointFBCCruft5();
88 		virtual	void _ReservedBNetEndpointFBCCruft6();
89 
90 		status_t	fStatus;
91 		int			fFamily;
92 		int			fType;
93 		int			fProtocol;
94 		int			fSocket;
95 		bigtime_t	fTimeout;
96 		BNetAddress fAddr;
97 		BNetAddress fPeer;
98 
99 		int32		_reserved[16];
100 };
101 
102 
103 #endif	// H_NETENDPOINT
104