Welcome to mirror list, hosted at ThFree Co, Russian Federation.

socket_apple.mm « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d38fd9ba2d6b1f7815444b70f9ab7aae648c4c0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#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 = @{(id)kCFStreamSSLValidatesCertificateChain : @NO,
                              (id)kCFStreamSSLLevel : (id)kCFStreamSocketSecurityLevelTLSv1,
                              };

  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(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;
};

unique_ptr<Socket> CreateSocket()
{
  return make_unique<PlatformSocket>();
}

PlatformSocket::PlatformSocket() { m_socketImpl = [[SocketImpl alloc] init]; }

PlatformSocket::~PlatformSocket()
{
  Close();
  m_socketImpl = nullptr;
}

bool PlatformSocket::Open(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