1/*
2 * Copyright 2023, Trung Nguyen, trungnt282910@gmail.com.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef UNIX_DATAGRAM_ENDPOINT_H
6#define UNIX_DATAGRAM_ENDPOINT_H
7
8
9#include <Referenceable.h>
10
11#include "UnixEndpoint.h"
12
13
14class UnixFifo;
15
16
17class UnixDatagramEndpoint : public UnixEndpoint, public BReferenceable {
18public:
19								UnixDatagramEndpoint(net_socket* socket);
20	virtual						~UnixDatagramEndpoint() override;
21
22			status_t			Init() override;
23			void				Uninit() override;
24
25			status_t			Open() override;
26			status_t			Close() override;
27			status_t			Free() override;
28
29			status_t			Bind(const struct sockaddr* _address) override;
30			status_t			Unbind() override;
31			status_t			Listen(int backlog) override;
32			status_t			Connect(const struct sockaddr* address) override;
33			status_t			Accept(net_socket** _acceptedSocket) override;
34
35			ssize_t				Send(const iovec* vecs, size_t vecCount,
36									ancillary_data_container* ancillaryData,
37									const struct sockaddr* address,
38									socklen_t addressLength, int flags) override;
39			ssize_t				Receive(const iovec* vecs, size_t vecCount,
40									ancillary_data_container** _ancillaryData,
41									struct sockaddr* _address,
42									socklen_t* _addressLength, int flags) override;
43
44			ssize_t				Sendable() override;
45			ssize_t				Receivable() override;
46
47			status_t			SetReceiveBufferSize(size_t size) override;
48			status_t			GetPeerCredentials(ucred* credentials) override;
49
50			status_t			Shutdown(int direction) override;
51
52	bool IsBound() const
53	{
54		return fAddress.IsValid();
55	}
56
57private:
58	static	status_t			_InitializeEndpoint(const struct sockaddr* _address,
59									BReference<UnixDatagramEndpoint> &outEndpoint);
60
61			status_t			_Disconnect();
62			void				_UnsetReceiveFifo();
63
64private:
65		UnixDatagramEndpoint*	fTargetEndpoint;
66		UnixFifo*				fReceiveFifo;
67		bool					fShutdownWrite:1;
68		bool					fShutdownRead:1;
69};
70
71
72#endif	// UNIX_DATAGRAM_ENDPOINT_H
73