1 /* 2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef UNIX_ENDPOINT_H 6 #define UNIX_ENDPOINT_H 7 8 #include <sys/stat.h> 9 10 #include <Referenceable.h> 11 12 #include <lock.h> 13 #include <util/DoublyLinkedList.h> 14 #include <util/OpenHashTable.h> 15 #include <vfs.h> 16 17 #include <net_protocol.h> 18 #include <net_socket.h> 19 #include <ProtocolUtilities.h> 20 21 #include "unix.h" 22 #include "UnixAddress.h" 23 24 25 class UnixEndpoint; 26 class UnixFifo; 27 28 29 enum unix_endpoint_state { 30 UNIX_ENDPOINT_NOT_CONNECTED, 31 UNIX_ENDPOINT_LISTENING, 32 UNIX_ENDPOINT_CONNECTED, 33 UNIX_ENDPOINT_CLOSED 34 }; 35 36 37 typedef AutoLocker<UnixEndpoint> UnixEndpointLocker; 38 39 40 class UnixEndpoint : public net_protocol, public ProtocolSocket, 41 public Referenceable { 42 public: 43 UnixEndpoint(net_socket* socket); 44 virtual ~UnixEndpoint(); 45 46 status_t Init(); 47 void Uninit(); 48 49 status_t Open(); 50 status_t Close(); 51 status_t Free(); 52 53 bool Lock() 54 { 55 return benaphore_lock(&fLock) == B_OK; 56 } 57 58 void Unlock() 59 { 60 benaphore_unlock(&fLock); 61 } 62 63 status_t Bind(const struct sockaddr *_address); 64 status_t Unbind(); 65 status_t Listen(int backlog); 66 status_t Connect(const struct sockaddr *address); 67 status_t Accept(net_socket **_acceptedSocket); 68 69 status_t Send(net_buffer *buffer); 70 status_t Receive(size_t numBytes, uint32 flags, net_buffer **_buffer); 71 72 ssize_t Sendable(); 73 ssize_t Receivable(); 74 75 void SetReceiveBufferSize(size_t size); 76 77 status_t Shutdown(int direction); 78 79 bool IsBound() const 80 { 81 return !fIsChild && fAddress.IsValid(); 82 } 83 84 const UnixAddress& Address() const 85 { 86 return fAddress; 87 } 88 89 HashTableLink<UnixEndpoint>* HashTableLink() 90 { 91 return &fAddressHashLink; 92 } 93 94 private: 95 void _Spawn(UnixEndpoint* connectingEndpoint, UnixFifo* fifo); 96 void _Disconnect(); 97 status_t _LockConnectedEndpoints(UnixEndpointLocker& locker, 98 UnixEndpointLocker& peerLocker); 99 100 status_t _Bind(struct vnode* vnode); 101 status_t _Bind(int32 internalID); 102 status_t _Unbind(); 103 104 void _UnsetReceiveFifo(); 105 void _StopListening(); 106 107 private: 108 benaphore fLock; 109 UnixAddress fAddress; 110 ::HashTableLink<UnixEndpoint> fAddressHashLink; 111 UnixEndpoint* fPeerEndpoint; 112 UnixFifo* fReceiveFifo; 113 unix_endpoint_state fState; 114 sem_id fAcceptSemaphore; 115 bool fIsChild; 116 }; 117 118 119 #endif // UNIX_ENDPOINT_H 120