remove socket

This commit is contained in:
LaGrunge 2019-10-09 16:34:10 +03:00
parent 750aadbc9a
commit 939d68fe03
7 changed files with 0 additions and 380 deletions

View file

@ -26,7 +26,6 @@ set(
preferred_languages.hpp
settings.cpp
settings.hpp
socket.hpp
string_storage_base.cpp
string_storage_base.hpp
target_os.hpp
@ -36,7 +35,6 @@ if(${PLATFORM_MAC})
append(
SRC
platform_mac.mm
socket_apple.mm
)
elseif(${PLATFORM_LINUX})
append(

View file

@ -1,5 +1,4 @@
#include "platform/platform.hpp"
#include "platform/socket.hpp"
#include "coding/file_reader.hpp"
@ -108,13 +107,6 @@ string DefaultWritableDir()
}
} // namespace
namespace platform
{
unique_ptr<Socket> CreateSocket()
{
return unique_ptr<Socket>();
}
}
Platform::Platform()
{

View file

@ -8,8 +8,6 @@ set(
scoped_file.hpp
scoped_mwm.cpp
scoped_mwm.hpp
test_socket.cpp
test_socket.hpp
writable_dir_changer.cpp
writable_dir_changer.hpp
)

View file

@ -1,69 +0,0 @@
#include "test_socket.hpp"
#include <algorithm>
#include <chrono>
using namespace std;
using namespace std::chrono;
namespace platform
{
namespace tests_support
{
TestSocket::~TestSocket() { m_isConnected = false; }
bool TestSocket::Open(string const & /*host*/, uint16_t /*port*/)
{
if (m_isConnected)
return false;
m_isConnected = true;
return true;
}
void TestSocket::Close() { m_isConnected = false; }
bool TestSocket::Read(uint8_t * data, uint32_t count)
{
if (!m_isConnected)
return false;
unique_lock<mutex> lock(m_inputMutex);
m_inputCondition.wait_for(lock, milliseconds(m_timeoutMs), [this]() { return !m_input.empty(); });
if (m_input.size() < count)
return false;
copy(m_input.begin(), m_input.end(), data);
m_input.erase(m_input.begin(), m_input.begin() + count);
return true;
}
bool TestSocket::Write(uint8_t const * data, uint32_t count)
{
if (!m_isConnected)
return false;
{
lock_guard<mutex> lg(m_outputMutex);
m_output.insert(m_output.end(), data, data + count);
}
m_outputCondition.notify_one();
return true;
}
void TestSocket::SetTimeout(uint32_t milliseconds) { m_timeoutMs = milliseconds; }
size_t TestSocket::ReadServer(vector<uint8_t> & destination)
{
unique_lock<mutex> lock(m_outputMutex);
m_outputCondition.wait_for(lock, milliseconds(m_timeoutMs),
[this]() { return !m_output.empty(); });
size_t const outputSize = m_output.size();
destination.insert(destination.end(), m_output.begin(), m_output.end());
m_output.clear();
return outputSize;
}
} // namespace tests_support
} // namespace platform

View file

@ -1,54 +0,0 @@
#pragma once
#include "platform/socket.hpp"
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <iterator>
#include <mutex>
#include <string>
#include <vector>
namespace platform
{
namespace tests_support
{
class TestSocket final : public Socket
{
public:
// Socket overrides:
~TestSocket();
bool Open(std::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;
// Simulates server reading.
// Waits for some data or timeout.
// Returns size of read data.
size_t ReadServer(std::vector<uint8_t> & destination);
template <typename Container>
void WriteServer(Container const & answer)
{
std::lock_guard<std::mutex> lg(m_inputMutex);
m_input.insert(m_input.begin(), std::begin(answer), std::end(answer));
m_inputCondition.notify_one();
}
private:
std::atomic<bool> m_isConnected = {false};
std::atomic<uint32_t> m_timeoutMs = {100};
std::deque<uint8_t> m_input;
std::mutex m_inputMutex;
std::condition_variable m_inputCondition;
std::vector<uint8_t> m_output;
std::mutex m_outputMutex;
std::condition_variable m_outputCondition;
};
} // namespace tests_support
} // namespace platform

View file

@ -1,41 +0,0 @@
#pragma once
#include <cstdint>
#include <memory>
#include <string>
namespace platform
{
class Socket
{
public:
virtual ~Socket() = default;
// 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(std::string const & host, uint16_t port) = 0;
virtual void Close() = 0;
// 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;
virtual void SetTimeout(uint32_t milliseconds) = 0;
};
class StubSocket final : public Socket
{
public:
// Socket overrides:
bool Open(std::string const &, uint16_t) override { return false; }
void Close() override {}
bool Read(uint8_t *, uint32_t) override { return false; }
bool Write(uint8_t const *, uint32_t) override { return false; }
void SetTimeout(uint32_t) override {}
};
std::unique_ptr<Socket> CreateSocket();
} // namespace platform

View file

@ -1,204 +0,0 @@
#import <Foundation/Foundation.h>
#include "platform/socket.hpp"
#include "base/logging.hpp"
@interface SocketImpl : NSObject
@property(nonatomic) NSInputStream * inputStream;
@property(nonatomic) NSOutputStream * outputStream;
@property(nonatomic) uint32_t timeout;
- (BOOL)open:(NSString *)host port:(NSUInteger)port;
- (void)close;
- (BOOL)read:(uint8_t *)data count:(NSUInteger)count;
- (BOOL)write:(uint8_t const *)data count:(NSUInteger)count;
@end
@implementation SocketImpl
- (BOOL)open:(NSString *)host port:(NSUInteger)port
{
[self close];
NSDate * openStart = [NSDate date];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(host), (UInt32)port, &readStream,
&writeStream);
NSDictionary * settings = @{
#ifndef GEOCORE_PRODUCTION
(id)kCFStreamSSLValidatesCertificateChain : @NO,
#endif
(id)kCFStreamSSLLevel : (id)kCFStreamSocketSecurityLevelNegotiatedSSL
};
CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
self.inputStream = (__bridge_transfer NSInputStream *)readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[self.inputStream open];
[self.outputStream open];
while ([[NSDate date] timeIntervalSinceDate:openStart] < self.timeout)
{
NSStreamStatus const inputStreamStatus = self.inputStream.streamStatus;
NSStreamStatus const outputStreamStatus = self.outputStream.streamStatus;
if (inputStreamStatus == NSStreamStatusError || outputStreamStatus == NSStreamStatusError)
return NO;
if (inputStreamStatus == NSStreamStatusOpen && outputStreamStatus == NSStreamStatusOpen)
return YES;
}
return NO;
}
- (void)close
{
if (self.inputStream)
{
[self.inputStream close];
self.inputStream = nil;
}
if (self.outputStream)
{
[self.outputStream close];
self.outputStream = nil;
}
}
- (BOOL)read:(uint8_t *)data count:(NSUInteger)count
{
if (!self.inputStream || self.inputStream.streamStatus != NSStreamStatusOpen)
return NO;
NSDate * readStart = [NSDate date];
uint8_t * readPtr = data;
while (count != 0 && [[NSDate date] timeIntervalSinceDate:readStart] < self.timeout)
{
NSInteger const readCount = [self.inputStream read:readPtr maxLength:count];
if (readCount > 0)
{
LOG(LDEBUG, ("Stream has read ", readCount, " bytes."));
count -= readCount;
readPtr += readCount;
}
else if (readCount == 0)
{
LOG(LDEBUG, ("The end of the read buffer was reached."));
}
else
{
LOG(LERROR, ("An error has occurred on the read stream."));
#ifdef OMIN_PRODUCTION
LOG(LERROR, (self.inputStream.streamError));
#else
NSLog(@"%@", self.inputStream.streamError);
#endif
return NO;
}
}
return count == 0;
}
- (BOOL)write:(uint8_t const *)data count:(NSUInteger)count
{
if (!self.outputStream || self.outputStream.streamStatus != NSStreamStatusOpen)
return NO;
uint8_t const * writePtr = data;
NSDate * writeStart = [NSDate date];
while (count != 0 && [[NSDate date] timeIntervalSinceDate:writeStart] < self.timeout)
{
NSInteger const writeCount = [self.outputStream write:writePtr maxLength:count];
if (writeCount > 0)
{
LOG(LDEBUG, ("Stream has written ", writeCount, " bytes."));
count -= writeCount;
writePtr += writeCount;
}
else if (writeCount == 0)
{
LOG(LDEBUG, ("The end of the write stream has been reached."));
}
else
{
LOG(LERROR, ("An error has occurred on the write stream."));
#ifdef OMIN_PRODUCTION
LOG(LERROR, (self.outputStream.streamError));
#else
NSLog(@"%@", self.outputStream.streamError);
#endif
return NO;
}
}
return count == 0;
}
@end
namespace platform
{
class PlatformSocket final : public Socket
{
public:
PlatformSocket();
// Socket overrides
~PlatformSocket();
bool Open(std::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;
private:
SocketImpl * m_socketImpl = nullptr;
};
std::unique_ptr<Socket> CreateSocket()
{
return std::make_unique<PlatformSocket>();
}
PlatformSocket::PlatformSocket() { m_socketImpl = [[SocketImpl alloc] init]; }
PlatformSocket::~PlatformSocket()
{
Close();
m_socketImpl = nullptr;
}
bool PlatformSocket::Open(std::string const & host, uint16_t port)
{
return [m_socketImpl open:@(host.c_str()) port:port];
}
void PlatformSocket::Close() { [m_socketImpl close]; }
bool PlatformSocket::Read(uint8_t * data, uint32_t count)
{
return [m_socketImpl read:data count:count];
}
bool PlatformSocket::Write(uint8_t const * data, uint32_t count)
{
return [m_socketImpl write:data count:count];
}
void PlatformSocket::SetTimeout(uint32_t milliseconds) { m_socketImpl.timeout = milliseconds; }
} // namespace platform