xref: /haiku/headers/os/net/NetEndpoint.h (revision be012e21222c4d8d70082d12353acb163dc60ba8)
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 		BNetEndpoint(const BNetEndpoint& other, int socket,
82 			const struct sockaddr_in& localAddress,
83 			const struct sockaddr_in& peerAddress);
84 
85 		status_t _SetupSocket();
86 
87 		virtual	void _ReservedBNetEndpointFBCCruft1();
88 		virtual	void _ReservedBNetEndpointFBCCruft2();
89 		virtual	void _ReservedBNetEndpointFBCCruft3();
90 		virtual	void _ReservedBNetEndpointFBCCruft4();
91 		virtual	void _ReservedBNetEndpointFBCCruft5();
92 		virtual	void _ReservedBNetEndpointFBCCruft6();
93 
94 		status_t	fStatus;
95 		int			fFamily;
96 		int			fType;
97 		int			fProtocol;
98 		int			fSocket;
99 		bigtime_t	fTimeout;
100 		BNetAddress fAddr;
101 		BNetAddress fPeer;
102 
103 		int32		_reserved[16];
104 };
105 
106 
107 #endif	// H_NETENDPOINT
108