BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / file_systems / nfs4 / Connection.h
blob3c01daa6dd872f40a1ed5755e0e9e46ab0fd7b71
1 /*
2 * Copyright 2012-2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Paweł Dziepak, pdziepak@quarnos.org
7 */
8 #ifndef CONNECTION_H
9 #define CONNECTION_H
12 #include <netinet/in.h>
14 #include <lock.h>
15 #include <SupportDefs.h>
18 struct PeerAddress {
19 sockaddr_storage fAddress;
20 int fProtocol;
22 bool operator==(const PeerAddress& address);
23 bool operator<(const PeerAddress& address);
25 PeerAddress& operator=(const PeerAddress& address);
27 PeerAddress();
28 PeerAddress(int networkFamily);
30 inline int Family() const;
32 const char* ProtocolString() const;
33 void SetProtocol(const char* protocol);
35 char* UniversalAddress() const;
37 socklen_t AddressSize() const;
39 void SetPort(uint16 port);
40 uint16 Port() const;
42 const void* InAddr() const;
43 size_t InAddrSize() const;
47 inline int
48 PeerAddress::Family() const
50 return fAddress.ss_family;
54 struct addrinfo;
56 class AddressResolver {
57 public:
58 AddressResolver(const char* name);
59 ~AddressResolver();
61 status_t GetNextAddress(PeerAddress* address);
63 void ForceProtocol(const char* protocol);
64 void ForcePort(uint16 port);
66 protected:
67 status_t ResolveAddress(const char* name);
69 private:
70 addrinfo* fHead;
71 addrinfo* fCurrent;
73 PeerAddress fAddress;
75 uint16 fForcedPort;
76 int fForcedProtocol;
78 status_t fStatus;
81 class ConnectionBase {
82 public:
83 ConnectionBase(const PeerAddress& address);
84 virtual ~ConnectionBase();
86 status_t GetLocalAddress(PeerAddress* address);
88 void Disconnect();
90 protected:
91 sem_id fWaitCancel;
92 int fSocket;
93 mutex fSocketLock;
95 const PeerAddress fPeerAddress;
98 class Connection : public ConnectionBase {
99 public:
100 static status_t Connect(Connection **connection,
101 const PeerAddress& address);
102 static status_t SetTo(Connection **connection, int socket,
103 const PeerAddress& address);
105 virtual status_t Send(const void* buffer, uint32 size) = 0;
106 virtual status_t Receive(void** buffer, uint32* size) = 0;
108 status_t Reconnect();
110 protected:
111 static Connection* CreateObject(const PeerAddress& address);
113 Connection(const PeerAddress& address);
114 status_t Connect();
118 class ConnectionStream : public Connection {
119 public:
120 ConnectionStream(const PeerAddress& address);
122 virtual status_t Send(const void* buffer, uint32 size);
123 virtual status_t Receive(void** buffer, uint32* size);
126 class ConnectionPacket : public Connection {
127 public:
128 ConnectionPacket(const PeerAddress& address);
130 virtual status_t Send(const void* buffer, uint32 size);
131 virtual status_t Receive(void** buffer, uint32* size);
134 class ConnectionListener : public ConnectionBase {
135 public:
136 static status_t Listen(ConnectionListener** listener, int networkFamily,
137 uint16 port = 0);
139 status_t AcceptConnection(Connection** connection);
141 protected:
142 ConnectionListener(const PeerAddress& address);
145 #endif // CONNECTION_H