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

socketapisocket_mac.mm « socketapi « gui « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 938494e2117faa95549c078063ee504639b8637d (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) by Jocelyn Turcotte <jturcotte@woboq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include "socketapisocket_mac.h"
#import <Cocoa/Cocoa.h>

@protocol ChannelProtocol <NSObject>

- (void)sendMessage:(NSData *)msg;

@end

@protocol RemoteEndProtocol <NSObject, ChannelProtocol>

- (void)registerTransmitter:(id)tx;

@end

@interface LocalEnd : NSObject <ChannelProtocol>

@property (atomic) SocketApiSocketPrivate *wrapper;

- (instancetype)initWithWrapper:(SocketApiSocketPrivate *)wrapper;

@end

@interface Server : NSObject

@property (atomic) SocketApiServerPrivate *wrapper;

- (instancetype)initWithWrapper:(SocketApiServerPrivate *)wrapper;
- (void)registerClient:(NSDistantObject<RemoteEndProtocol> *)remoteEnd;

@end

class SocketApiSocketPrivate
{
public:
    SocketApiSocket *q_ptr;

    SocketApiSocketPrivate(NSDistantObject<ChannelProtocol> *remoteEnd);
    ~SocketApiSocketPrivate();

    // release remoteEnd
    void disconnectRemote();

    NSDistantObject<ChannelProtocol> *remoteEnd;
    LocalEnd *localEnd;
    QByteArray inBuffer;
    bool isRemoteDisconnected = false;
};

class SocketApiServerPrivate
{
public:
    SocketApiServer *q_ptr;

    SocketApiServerPrivate();
    ~SocketApiServerPrivate();

    QList<SocketApiSocket *> pendingConnections;
    NSConnection *connection;
    Server *server;
};


@implementation LocalEnd

@synthesize wrapper = _wrapper;

- (instancetype)initWithWrapper:(SocketApiSocketPrivate *)wrapper
{
    self = [super init];
    self.wrapper = wrapper;
    return self;
}

- (void)sendMessage:(NSData *)msg
{
    if (self.wrapper) {
        self.wrapper->inBuffer += QByteArray::fromRawNSData(msg);
        emit self.wrapper->q_ptr->readyRead();
    }
}

- (void)connectionDidDie:(NSNotification *)notification
{
    // The NSConnectionDidDieNotification docs say to disconnect from NSConnection here
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    if (self.wrapper) {
        self.wrapper->disconnectRemote();
        emit self.wrapper->q_ptr->disconnected();
    }
}
@end

@implementation Server

@synthesize wrapper = _wrapper;

- (instancetype)initWithWrapper:(SocketApiServerPrivate *)wrapper
{
    self = [super init];
    self.wrapper = wrapper;
    return self;
}

- (void)registerClient:(NSDistantObject<RemoteEndProtocol> *)remoteEnd
{
    // This saves a few mach messages that would otherwise be needed to query the interface
    [remoteEnd setProtocolForProxy:@protocol(RemoteEndProtocol)];

    SocketApiServer *server = self.wrapper->q_ptr;
    SocketApiSocketPrivate *socketPrivate = new SocketApiSocketPrivate(remoteEnd);
    SocketApiSocket *socket = new SocketApiSocket(server, socketPrivate);
    self.wrapper->pendingConnections.append(socket);
    emit server->newConnection();

    [remoteEnd registerTransmitter:socketPrivate->localEnd];
}
@end


SocketApiSocket::SocketApiSocket(QObject *parent, SocketApiSocketPrivate *p)
    : QIODevice(parent)
    , d_ptr(p)
{
    Q_D(SocketApiSocket);
    d->q_ptr = this;
    open(ReadWrite);
}

SocketApiSocket::~SocketApiSocket()
{
}

qint64 SocketApiSocket::readData(char *data, qint64 maxlen)
{
    Q_D(SocketApiSocket);
    qint64 len = std::min(maxlen, static_cast<qint64>(d->inBuffer.size()));
    if (len < 0 || len > std::numeric_limits<int>::max()) {
        return -1;
    }

    memcpy(data, d->inBuffer.constData(), static_cast<size_t>(len));
    d->inBuffer.remove(0, static_cast<int>(len));
    return len;
}

qint64 SocketApiSocket::writeData(const char *data, qint64 len)
{
    Q_D(SocketApiSocket);
    if (d->isRemoteDisconnected) {
        return -1;
    }

    if (len < std::numeric_limits<NSUInteger>::min() || len > std::numeric_limits<NSUInteger>::max()) {
        return -1;
    }

    @try {
        // FIXME: The NSConnection will make this block unless the function is marked as "oneway"
        // in the protocol. This isn't async and reduces our performances but this currectly avoids
        // a Mach queue deadlock during requests bursts of the legacy OwnCloudFinder extension.
        // Since FinderSync already runs in a separate process, blocking isn't too critical.
        NSData *payload = QByteArray::fromRawData(data, static_cast<int>(len)).toRawNSData();
        [d->remoteEnd sendMessage:payload];
        return len;
    } @catch (NSException *) {
        // connectionDidDie can be notified too late, also interpret any sending exception as a disconnection.
        d->disconnectRemote();
        emit disconnected();
        return -1;
    }
}

qint64 SocketApiSocket::bytesAvailable() const
{
    Q_D(const SocketApiSocket);
    return d->inBuffer.size() + QIODevice::bytesAvailable();
}

bool SocketApiSocket::canReadLine() const
{
    Q_D(const SocketApiSocket);
    return d->inBuffer.indexOf('\n', int(pos())) != -1 || QIODevice::canReadLine();
}

SocketApiSocketPrivate::SocketApiSocketPrivate(NSDistantObject<ChannelProtocol> *remoteEnd)
    : remoteEnd(remoteEnd)
    , localEnd([[LocalEnd alloc] initWithWrapper:this])
{
    [remoteEnd retain];
    // (Ab)use our objective-c object just to catch the notification
    [[NSNotificationCenter defaultCenter] addObserver:localEnd
                                             selector:@selector(connectionDidDie:)
                                                 name:NSConnectionDidDieNotification
                                               object:[remoteEnd connectionForProxy]];
}

SocketApiSocketPrivate::~SocketApiSocketPrivate()
{
    disconnectRemote();

    // The DO vended localEnd might still be referenced by the connection
    localEnd.wrapper = nil;
    [localEnd release];
}

void SocketApiSocketPrivate::disconnectRemote()
{
    if (isRemoteDisconnected)
        return;
    isRemoteDisconnected = true;

    [remoteEnd release];
}

SocketApiServer::SocketApiServer()
    : d_ptr(new SocketApiServerPrivate)
{
    Q_D(SocketApiServer);
    d->q_ptr = this;
}

SocketApiServer::~SocketApiServer()
{
}

void SocketApiServer::close()
{
    // Assume we'll be destroyed right after
}

bool SocketApiServer::listen(const QString &name)
{
    Q_D(SocketApiServer);
    // Set the name of the root object
    return [d->connection registerName:name.toNSString()];
}

SocketApiSocket *SocketApiServer::nextPendingConnection()
{
    Q_D(SocketApiServer);
    return d->pendingConnections.takeFirst();
}

SocketApiServerPrivate::SocketApiServerPrivate()
{
    // Create the connection and server object to vend over Disributed Objects
    connection = [[NSConnection alloc] init];
    server = [[Server alloc] initWithWrapper:this];
    [connection setRootObject:server];
}

SocketApiServerPrivate::~SocketApiServerPrivate()
{
    [connection release];
    server.wrapper = nil;
    [server release];
}