Network/TcpClient: Make TcpClient a Stream
Former-commit-id: 2176748166ae84b609d5a336495e6ff3550a765d
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
namespace Nz
|
||||
{
|
||||
File::File() :
|
||||
Stream(OpenMode_NotOpen),
|
||||
m_impl(nullptr)
|
||||
{
|
||||
}
|
||||
@@ -75,6 +74,8 @@ namespace Nz
|
||||
m_impl->Close();
|
||||
delete m_impl;
|
||||
m_impl = nullptr;
|
||||
|
||||
m_openMode = OpenMode_NotOpen;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,20 +209,18 @@ namespace Nz
|
||||
if (m_filePath.IsEmpty())
|
||||
return false;
|
||||
|
||||
if (openMode != 0)
|
||||
m_openMode = openMode;
|
||||
|
||||
if (m_openMode == 0)
|
||||
if (openMode == OpenMode_NotOpen)
|
||||
return false;
|
||||
|
||||
std::unique_ptr<FileImpl> impl(new FileImpl(this));
|
||||
if (!impl->Open(m_filePath, m_openMode))
|
||||
if (!impl->Open(m_filePath, openMode))
|
||||
{
|
||||
ErrorFlags flags(ErrorFlag_Silent); // Silencieux par défaut
|
||||
NazaraError("Failed to open \"" + m_filePath + "\": " + Error::GetLastSystemError());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_openMode = openMode;
|
||||
m_impl = impl.release();
|
||||
|
||||
if (m_openMode & OpenMode_Text)
|
||||
|
||||
@@ -94,6 +94,22 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
bool TcpClient::EndOfStream() const
|
||||
{
|
||||
return QueryAvailableBytes() == 0;
|
||||
}
|
||||
|
||||
UInt64 TcpClient::GetCursorPos() const
|
||||
{
|
||||
NazaraError("GetCursorPos() cannot be used on sequential streams");
|
||||
return 0;
|
||||
}
|
||||
|
||||
UInt64 TcpClient::GetSize() const
|
||||
{
|
||||
return QueryAvailableBytes();
|
||||
}
|
||||
|
||||
bool TcpClient::Receive(void* buffer, std::size_t size, std::size_t* received)
|
||||
{
|
||||
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
|
||||
@@ -165,6 +181,12 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TcpClient::SetCursorPos(UInt64 offset)
|
||||
{
|
||||
NazaraError("SetCursorPos() cannot be used on sequential streams");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TcpClient::WaitForConnected(UInt64 msTimeout)
|
||||
{
|
||||
switch (m_state)
|
||||
@@ -209,10 +231,15 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
void TcpClient::FlushStream()
|
||||
{
|
||||
}
|
||||
|
||||
void TcpClient::OnClose()
|
||||
{
|
||||
AbstractSocket::OnClose();
|
||||
|
||||
m_openMode = OpenMode_NotOpen;
|
||||
m_peerAddress = IpAddress::Invalid;
|
||||
}
|
||||
|
||||
@@ -229,12 +256,56 @@ namespace Nz
|
||||
NazaraWarning("Failed to set socket keep alive mode (0x" + String::Number(errorCode, 16) + ')');
|
||||
|
||||
m_peerAddress = IpAddress::Invalid;
|
||||
m_openMode = OpenMode_ReadWrite;
|
||||
}
|
||||
|
||||
std::size_t TcpClient::ReadBlock(void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
|
||||
|
||||
CallOnExit restoreBlocking;
|
||||
if (!m_isBlockingEnabled)
|
||||
{
|
||||
SocketImpl::SetBlocking(m_handle, true);
|
||||
restoreBlocking.Reset([this] ()
|
||||
{
|
||||
SocketImpl::SetBlocking(m_handle, false);
|
||||
});
|
||||
}
|
||||
|
||||
std::size_t received;
|
||||
if (!Receive(buffer, size, &received))
|
||||
received = 0;
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
void TcpClient::Reset(SocketHandle handle, const IpAddress& peerAddress)
|
||||
{
|
||||
Open(handle);
|
||||
m_peerAddress = peerAddress;
|
||||
m_openMode = OpenMode_ReadWrite;
|
||||
UpdateState(SocketState_Connected);
|
||||
}
|
||||
|
||||
std::size_t TcpClient::WriteBlock(const void* buffer, std::size_t size)
|
||||
{
|
||||
NazaraAssert(m_handle != SocketImpl::InvalidHandle, "Invalid handle");
|
||||
|
||||
CallOnExit restoreBlocking;
|
||||
if (!m_isBlockingEnabled)
|
||||
{
|
||||
SocketImpl::SetBlocking(m_handle, true);
|
||||
restoreBlocking.Reset([this] ()
|
||||
{
|
||||
SocketImpl::SetBlocking(m_handle, false);
|
||||
});
|
||||
}
|
||||
|
||||
std::size_t sent;
|
||||
if (!Send(buffer, size, &sent))
|
||||
sent = 0;
|
||||
|
||||
return sent;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user