nfs4: Add ConnectionStream and ConnectionPacket classes
This commit is contained in:
parent
0e1fd494e5
commit
b75edefd15
@ -48,10 +48,9 @@ ServerAddress::operator=(const ServerAddress& x)
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const sockaddr_in& addr, Transport proto, bool markers)
|
||||
Connection::Connection(const sockaddr_in& addr, Transport proto)
|
||||
:
|
||||
fSock(-1),
|
||||
fUseMarkers(markers),
|
||||
fProtocol(proto),
|
||||
fServerAddress(addr)
|
||||
{
|
||||
@ -59,6 +58,20 @@ Connection::Connection(const sockaddr_in& addr, Transport proto, bool markers)
|
||||
}
|
||||
|
||||
|
||||
ConnectionStream::ConnectionStream(const sockaddr_in& addr, Transport proto)
|
||||
:
|
||||
Connection(addr, proto)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ConnectionPacket::ConnectionPacket(const sockaddr_in& addr, Transport proto)
|
||||
:
|
||||
Connection(addr, proto)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Connection::~Connection()
|
||||
{
|
||||
if (fSock != -1)
|
||||
@ -85,7 +98,7 @@ Connection::GetLocalID(ServerAddress* addr)
|
||||
|
||||
|
||||
status_t
|
||||
Connection::_SendStream(const void* buffer, uint32 size)
|
||||
ConnectionStream::Send(const void* buffer, uint32 size)
|
||||
{
|
||||
status_t result;
|
||||
|
||||
@ -120,7 +133,7 @@ Connection::_SendStream(const void* buffer, uint32 size)
|
||||
|
||||
|
||||
status_t
|
||||
Connection::_SendPacket(const void* buffer, uint32 size)
|
||||
ConnectionPacket::Send(const void* buffer, uint32 size)
|
||||
{
|
||||
// send on DGRAM sockets is atomic. No need to lock.
|
||||
status_t result = send(fSock, buffer, size, 0);
|
||||
@ -132,7 +145,7 @@ Connection::_SendPacket(const void* buffer, uint32 size)
|
||||
|
||||
|
||||
status_t
|
||||
Connection::_ReceiveStream(void** pbuffer, uint32* psize)
|
||||
ConnectionStream::Receive(void** pbuffer, uint32* psize)
|
||||
{
|
||||
status_t result;
|
||||
|
||||
@ -193,7 +206,7 @@ Connection::_ReceiveStream(void** pbuffer, uint32* psize)
|
||||
|
||||
|
||||
status_t
|
||||
Connection::_ReceivePacket(void** pbuffer, uint32* psize)
|
||||
ConnectionPacket::Receive(void** pbuffer, uint32* psize)
|
||||
{
|
||||
status_t result;
|
||||
int32 size = MAX_PACKET_SIZE;
|
||||
@ -231,8 +244,11 @@ Connection::Connect(Connection **pconn, const ServerAddress& id)
|
||||
addr.sin_addr.s_addr = htonl(id.fAddress);
|
||||
addr.sin_port = htons(id.fPort);
|
||||
|
||||
Connection* conn = new(std::nothrow) Connection(addr, id.fProtocol,
|
||||
id.fProtocol == ProtocolTCP);
|
||||
Connection* conn;
|
||||
if (id.fProtocol == ProtocolTCP)
|
||||
conn = new(std::nothrow) ConnectionStream(addr, id.fProtocol);
|
||||
else
|
||||
conn = new(std::nothrow) ConnectionPacket(addr, id.fProtocol);
|
||||
if (conn == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
|
@ -35,56 +35,45 @@ class Connection {
|
||||
public:
|
||||
static status_t Connect(Connection **conn,
|
||||
const ServerAddress& id);
|
||||
~Connection();
|
||||
virtual ~Connection();
|
||||
|
||||
inline status_t Send(const void* buffer, uint32 size);
|
||||
inline status_t Receive(void** buffer, uint32* size);
|
||||
virtual status_t Send(const void* buffer, uint32 size) = 0;
|
||||
virtual status_t Receive(void** buffer, uint32* size) = 0;
|
||||
|
||||
status_t GetLocalID(ServerAddress* addr);
|
||||
|
||||
status_t Reconnect();
|
||||
void Disconnect();
|
||||
|
||||
private:
|
||||
protected:
|
||||
Connection(const sockaddr_in& addr,
|
||||
Transport proto, bool markers);
|
||||
Transport proto);
|
||||
status_t _Connect();
|
||||
|
||||
status_t _SendStream(const void* buffer, uint32 size);
|
||||
status_t _SendPacket(const void* buffer, uint32 size);
|
||||
|
||||
status_t _ReceiveStream(void** buffer, uint32* size);
|
||||
status_t _ReceivePacket(void** buffer, uint32* size);
|
||||
|
||||
int fSock;
|
||||
mutex fSockLock;
|
||||
|
||||
const bool fUseMarkers;
|
||||
|
||||
const Transport fProtocol;
|
||||
const sockaddr_in fServerAddress;
|
||||
};
|
||||
|
||||
class ConnectionStream : public Connection {
|
||||
public:
|
||||
ConnectionStream(const sockaddr_in& addr,
|
||||
Transport proto);
|
||||
|
||||
inline status_t
|
||||
Connection::Send(const void* buffer, uint32 size)
|
||||
{
|
||||
if (fUseMarkers)
|
||||
return _SendStream(buffer, size);
|
||||
else
|
||||
return _SendPacket(buffer, size);
|
||||
}
|
||||
virtual status_t Send(const void* buffer, uint32 size);
|
||||
virtual status_t Receive(void** buffer, uint32* size);
|
||||
};
|
||||
|
||||
class ConnectionPacket : public Connection {
|
||||
public:
|
||||
ConnectionPacket(const sockaddr_in& addr,
|
||||
Transport proto);
|
||||
|
||||
inline status_t
|
||||
Connection::Receive(void** buffer, uint32* size)
|
||||
{
|
||||
if (fUseMarkers)
|
||||
return _ReceiveStream(buffer, size);
|
||||
else
|
||||
return _ReceivePacket(buffer, size);
|
||||
}
|
||||
|
||||
virtual status_t Send(const void* buffer, uint32 size);
|
||||
virtual status_t Receive(void** buffer, uint32* size);
|
||||
};
|
||||
|
||||
#endif // CONNECTION_H
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user