Merge pull request #4478 from dobriy-eeh/socket

socket base interface, mock implementation
This commit is contained in:
Sergey Yershov 2016-10-12 13:09:16 +03:00 committed by GitHub
commit 39b46f097a
2 changed files with 42 additions and 30 deletions

View file

@ -2,30 +2,29 @@
#include "std/string.hpp"
#include "std/target_os.hpp"
#if defined(OMIM_OS_IPHONE) || defined(OMIM_OS_MAC)
@class SocketImpl;
#else
class SocketImpl;
#endif
#include "std/unique_ptr.hpp"
namespace platform
{
class Socket
{
public:
Socket();
~Socket();
virtual ~Socket() = default;
bool Open(string const & host, uint16_t port);
void Close();
// Open/Close contract:
// 1. You can call Open+Close pair multiple times for the same Socket instance.
// 2. There are should be Close call after each Open call.
// 3. Open+Open: second Open does nothing and returns false.
// 4. Close+Close: second Close does nothing.
virtual bool Open(string const & host, uint16_t port) = 0;
virtual void Close() = 0;
bool Read(uint8_t * data, uint32_t count);
bool Write(uint8_t const * data, uint32_t count);
// Read is blocking, it waits for the 'count' data size.
virtual bool Read(uint8_t * data, uint32_t count) = 0;
virtual bool Write(uint8_t const * data, uint32_t count) = 0;
void SetTimeout(uint32_t milliseconds);
private:
SocketImpl * m_socketImpl = nullptr;
virtual void SetTimeout(uint32_t milliseconds) = 0;
};
unique_ptr<Socket> createSocket();
} // namespace platform

View file

@ -22,22 +22,35 @@
namespace platform
{
Socket::Socket() { m_socketImpl = [[SocketImpl alloc] init]; }
Socket::~Socket()
class PlatformSocket final : public Socket
{
Close();
m_socketImpl = nil;
public:
PlatformSocket();
// Socket overrides
~PlatformSocket();
bool Open(string const & host, uint16_t port) override;
void Close() override;
bool Read(uint8_t * data, uint32_t count) override;
bool Write(uint8_t const * data, uint32_t count) override;
void SetTimeout(uint32_t milliseconds) override;
};
unique_ptr<Socket> createSocket()
{
return make_unique<PlatformSocket>();
}
bool Socket::Open(string const & host, uint16_t port)
{
return [m_socketImpl open:@(host.c_str()) port:port];
}
PlatformSocket::PlatformSocket() {}
void Socket::Close() { [m_socketImpl close]; }
bool Socket::Read(uint8_t * data, uint32_t count) { return [m_socketImpl read:data count:count]; }
bool Socket::Write(uint8_t const * data, uint32_t count)
{
return [m_socketImpl write:data count:count];
}
PlatformSocket::~PlatformSocket() { Close(); }
bool PlatformSocket::Open(string const & host, uint16_t port) { return false; }
void PlatformSocket::Close() {}
bool PlatformSocket::Read(uint8_t * data, uint32_t count) { return false; }
bool PlatformSocket::Write(uint8_t const * data, uint32_t count) { return false; }
void PlatformSocket::SetTimeout(uint32_t milliseconds) {}
} // namespace platform