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),
|
fSock(-1),
|
||||||
fUseMarkers(markers),
|
|
||||||
fProtocol(proto),
|
fProtocol(proto),
|
||||||
fServerAddress(addr)
|
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()
|
Connection::~Connection()
|
||||||
{
|
{
|
||||||
if (fSock != -1)
|
if (fSock != -1)
|
||||||
@ -85,7 +98,7 @@ Connection::GetLocalID(ServerAddress* addr)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Connection::_SendStream(const void* buffer, uint32 size)
|
ConnectionStream::Send(const void* buffer, uint32 size)
|
||||||
{
|
{
|
||||||
status_t result;
|
status_t result;
|
||||||
|
|
||||||
@ -120,7 +133,7 @@ Connection::_SendStream(const void* buffer, uint32 size)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
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.
|
// send on DGRAM sockets is atomic. No need to lock.
|
||||||
status_t result = send(fSock, buffer, size, 0);
|
status_t result = send(fSock, buffer, size, 0);
|
||||||
@ -132,7 +145,7 @@ Connection::_SendPacket(const void* buffer, uint32 size)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Connection::_ReceiveStream(void** pbuffer, uint32* psize)
|
ConnectionStream::Receive(void** pbuffer, uint32* psize)
|
||||||
{
|
{
|
||||||
status_t result;
|
status_t result;
|
||||||
|
|
||||||
@ -193,7 +206,7 @@ Connection::_ReceiveStream(void** pbuffer, uint32* psize)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Connection::_ReceivePacket(void** pbuffer, uint32* psize)
|
ConnectionPacket::Receive(void** pbuffer, uint32* psize)
|
||||||
{
|
{
|
||||||
status_t result;
|
status_t result;
|
||||||
int32 size = MAX_PACKET_SIZE;
|
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_addr.s_addr = htonl(id.fAddress);
|
||||||
addr.sin_port = htons(id.fPort);
|
addr.sin_port = htons(id.fPort);
|
||||||
|
|
||||||
Connection* conn = new(std::nothrow) Connection(addr, id.fProtocol,
|
Connection* conn;
|
||||||
id.fProtocol == ProtocolTCP);
|
if (id.fProtocol == ProtocolTCP)
|
||||||
|
conn = new(std::nothrow) ConnectionStream(addr, id.fProtocol);
|
||||||
|
else
|
||||||
|
conn = new(std::nothrow) ConnectionPacket(addr, id.fProtocol);
|
||||||
if (conn == NULL)
|
if (conn == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
@ -35,56 +35,45 @@ class Connection {
|
|||||||
public:
|
public:
|
||||||
static status_t Connect(Connection **conn,
|
static status_t Connect(Connection **conn,
|
||||||
const ServerAddress& id);
|
const ServerAddress& id);
|
||||||
~Connection();
|
virtual ~Connection();
|
||||||
|
|
||||||
inline status_t Send(const void* buffer, uint32 size);
|
virtual status_t Send(const void* buffer, uint32 size) = 0;
|
||||||
inline status_t Receive(void** buffer, uint32* size);
|
virtual status_t Receive(void** buffer, uint32* size) = 0;
|
||||||
|
|
||||||
status_t GetLocalID(ServerAddress* addr);
|
status_t GetLocalID(ServerAddress* addr);
|
||||||
|
|
||||||
status_t Reconnect();
|
status_t Reconnect();
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
Connection(const sockaddr_in& addr,
|
Connection(const sockaddr_in& addr,
|
||||||
Transport proto, bool markers);
|
Transport proto);
|
||||||
status_t _Connect();
|
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;
|
int fSock;
|
||||||
mutex fSockLock;
|
mutex fSockLock;
|
||||||
|
|
||||||
const bool fUseMarkers;
|
|
||||||
|
|
||||||
const Transport fProtocol;
|
const Transport fProtocol;
|
||||||
const sockaddr_in fServerAddress;
|
const sockaddr_in fServerAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ConnectionStream : public Connection {
|
||||||
|
public:
|
||||||
|
ConnectionStream(const sockaddr_in& addr,
|
||||||
|
Transport proto);
|
||||||
|
|
||||||
inline status_t
|
virtual status_t Send(const void* buffer, uint32 size);
|
||||||
Connection::Send(const void* buffer, uint32 size)
|
virtual status_t Receive(void** buffer, uint32* size);
|
||||||
{
|
};
|
||||||
if (fUseMarkers)
|
|
||||||
return _SendStream(buffer, size);
|
|
||||||
else
|
|
||||||
return _SendPacket(buffer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
class ConnectionPacket : public Connection {
|
||||||
|
public:
|
||||||
|
ConnectionPacket(const sockaddr_in& addr,
|
||||||
|
Transport proto);
|
||||||
|
|
||||||
inline status_t
|
virtual status_t Send(const void* buffer, uint32 size);
|
||||||
Connection::Receive(void** buffer, uint32* size)
|
virtual status_t Receive(void** buffer, uint32* size);
|
||||||
{
|
};
|
||||||
if (fUseMarkers)
|
|
||||||
return _ReceiveStream(buffer, size);
|
|
||||||
else
|
|
||||||
return _ReceivePacket(buffer, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif // CONNECTION_H
|
#endif // CONNECTION_H
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user