2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "NeoVimServer.h"
|
|
|
|
#import "server_globals.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const double qTimeout = 10.0;
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
#define data_to_array(type) \
|
|
|
|
static type *data_to_ ## type ## _array(NSData *data, NSUInteger count) { \
|
|
|
|
NSUInteger length = count * sizeof( type ); \
|
|
|
|
if (data.length != length) { \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
return ( type *) data.bytes; \
|
|
|
|
}
|
|
|
|
|
|
|
|
data_to_array(int)
|
|
|
|
data_to_array(NSInteger)
|
|
|
|
|
|
|
|
@interface NeoVimServer ()
|
|
|
|
|
|
|
|
- (void)handleMessageWithId:(SInt32)msgid data:(NSData *)data;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) {
|
|
|
|
@autoreleasepool {
|
2016-07-10 15:24:45 +03:00
|
|
|
NeoVimServer *neoVimServer = (__bridge NeoVimServer *) info;
|
|
|
|
[neoVimServer handleMessageWithId:msgid data:(__bridge NSData *) data];
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
2016-07-10 15:24:45 +03:00
|
|
|
|
|
|
|
return NULL;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NeoVimServer {
|
|
|
|
NSString *_uuid;
|
|
|
|
NSString *_localServerName;
|
|
|
|
NSString *_remoteServerName;
|
|
|
|
|
|
|
|
NSThread *_localServerThread;
|
|
|
|
CFMessagePortRef _localServerPort;
|
|
|
|
|
|
|
|
CFMessagePortRef _remoteServerPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithUuid:(NSString *)uuid
|
|
|
|
localServerName:(NSString *)localServerName
|
|
|
|
remoteServerName:(NSString *)remoteServerName
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-07-10 15:24:45 +03:00
|
|
|
_uuid = uuid;
|
|
|
|
_localServerName = localServerName;
|
|
|
|
_remoteServerName = remoteServerName;
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
_localServerThread = [[NSThread alloc] initWithTarget:self selector:@selector(runLocalServer) object:nil];
|
|
|
|
[_localServerThread start];
|
|
|
|
|
2016-07-10 15:24:45 +03:00
|
|
|
_remoteServerPort = CFMessagePortCreateRemote(kCFAllocatorDefault, (__bridge CFStringRef) _remoteServerName);
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
CFMessagePortInvalidate(_localServerPort);
|
|
|
|
CFRelease(_localServerPort);
|
|
|
|
|
|
|
|
[_localServerThread cancel];
|
|
|
|
|
|
|
|
CFMessagePortInvalidate(_remoteServerPort);
|
|
|
|
CFRelease(_remoteServerPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)runLocalServer {
|
|
|
|
@autoreleasepool {
|
|
|
|
unsigned char shouldFree = false;
|
|
|
|
CFMessagePortContext localContext = {
|
|
|
|
.version = 0,
|
2016-07-10 15:24:45 +03:00
|
|
|
.info = (__bridge void *) self,
|
2016-07-09 15:04:09 +03:00
|
|
|
.retain = NULL,
|
|
|
|
.release = NULL,
|
|
|
|
.copyDescription = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
_localServerPort = CFMessagePortCreateLocal(
|
|
|
|
kCFAllocatorDefault,
|
2016-07-10 15:24:45 +03:00
|
|
|
(__bridge CFStringRef) _localServerName,
|
2016-07-09 15:04:09 +03:00
|
|
|
local_server_callback,
|
|
|
|
&localContext,
|
|
|
|
&shouldFree
|
|
|
|
);
|
|
|
|
|
|
|
|
// FIXME: handle shouldFree == true
|
|
|
|
}
|
2016-07-10 15:10:09 +03:00
|
|
|
|
|
|
|
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
|
|
|
|
CFRunLoopSourceRef runLoopSrc = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, _localServerPort, 0);
|
|
|
|
CFRunLoopAddSource(runLoop, runLoopSrc, kCFRunLoopCommonModes);
|
|
|
|
CFRelease(runLoopSrc);
|
|
|
|
CFRunLoopRun();
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendMessageWithId:(NeoVimServerMsgId)msgid {
|
|
|
|
[self sendMessageWithId:msgid data:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendMessageWithId:(NeoVimServerMsgId)msgid data:(NSData *)data {
|
2016-07-09 23:52:59 +03:00
|
|
|
if (_remoteServerPort == NULL) {
|
|
|
|
NSLog(@"WARNING: remote server is null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
SInt32 responseCode = CFMessagePortSendRequest(
|
2016-07-10 15:24:45 +03:00
|
|
|
_remoteServerPort, msgid, (__bridge CFDataRef) data, qTimeout, qTimeout, NULL, NULL
|
2016-07-09 15:04:09 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (responseCode == kCFMessagePortSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog(@"WARNING: (%d:%@) could not be sent!", (int) msgid, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyReadiness {
|
|
|
|
[self sendMessageWithId:NeoVimServerMsgIdServerReady data:nil];
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
- (void)handleMessageWithId:(SInt32)msgid data:(NSData *)data {
|
|
|
|
switch (msgid) {
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdAgentReady:
|
2016-07-10 12:46:00 +03:00
|
|
|
server_start_neovim();
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdInput: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2016-07-10 12:46:00 +03:00
|
|
|
server_vim_input(string);
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdInputMarked: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2016-07-10 12:46:00 +03:00
|
|
|
server_vim_input_marked_text(string);
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdDelete: {
|
|
|
|
NSInteger *values = data_to_NSInteger_array(data, 1);
|
2016-07-10 12:46:00 +03:00
|
|
|
server_delete(values[0]);
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdResize: {
|
|
|
|
int *values = data_to_int_array(data, 2);
|
2016-07-10 12:46:00 +03:00
|
|
|
server_resize(values[0], values[1]);
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdRedraw:
|
2016-07-10 12:46:00 +03:00
|
|
|
server_redraw();
|
2016-07-09 23:52:59 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
@end
|