2016-07-09 15:04:09 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "NeoVimServer.h"
|
|
|
|
#import "server_globals.h"
|
2016-07-10 15:43:26 +03:00
|
|
|
#import "Logging.h"
|
2016-08-26 16:31:30 +03:00
|
|
|
#import "CocoaCategories.h"
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
|
2016-08-15 11:56:19 +03:00
|
|
|
// When #define'd you can execute the NeoVimServer binary and neovim will be started:
|
|
|
|
// $ ./NeoVimServer local remote
|
|
|
|
#undef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
//#define DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
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 ()
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
- (NSData *)handleMessageWithId:(SInt32)msgid data:(NSData *)data;
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
@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;
|
2016-07-18 21:16:56 +03:00
|
|
|
NSData *responseData = [neoVimServer handleMessageWithId:msgid data:(__bridge NSData *) data];
|
|
|
|
|
|
|
|
if (responseData == nil) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-07-10 15:24:45 +03:00
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
return CFDataCreateCopy(kCFAllocatorDefault, (__bridge CFDataRef) responseData);
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NeoVimServer {
|
|
|
|
NSString *_localServerName;
|
|
|
|
NSString *_remoteServerName;
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFMessagePortRef _remoteServerPort;
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
NSThread *_localServerThread;
|
|
|
|
CFMessagePortRef _localServerPort;
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopRef _localServerRunLoop;
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 15:43:26 +03:00
|
|
|
- (instancetype)initWithLocalServerName:(NSString *)localServerName remoteServerName:(NSString *)remoteServerName {
|
2016-07-09 15:04:09 +03:00
|
|
|
self = [super init];
|
|
|
|
if (self == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-07-10 15:24:45 +03:00
|
|
|
_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-08-15 11:56:19 +03:00
|
|
|
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
|
2016-07-10 15:24:45 +03:00
|
|
|
_remoteServerPort = CFMessagePortCreateRemote(kCFAllocatorDefault, (__bridge CFStringRef) _remoteServerName);
|
2016-08-15 11:56:19 +03:00
|
|
|
#endif
|
2016-07-09 15:04:09 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2016-07-18 21:16:56 +03:00
|
|
|
if (CFMessagePortIsValid(_remoteServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_remoteServerPort);
|
|
|
|
}
|
|
|
|
CFRelease(_remoteServerPort);
|
|
|
|
|
|
|
|
if (CFMessagePortIsValid(_localServerPort)) {
|
|
|
|
CFMessagePortInvalidate(_localServerPort);
|
|
|
|
}
|
2016-07-09 15:04:09 +03:00
|
|
|
CFRelease(_localServerPort);
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopStop(_localServerRunLoop);
|
2016-07-09 15:04:09 +03:00
|
|
|
[_localServerThread cancel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
_localServerRunLoop = CFRunLoopGetCurrent();
|
2016-07-10 15:10:09 +03:00
|
|
|
CFRunLoopSourceRef runLoopSrc = CFMessagePortCreateRunLoopSource(kCFAllocatorDefault, _localServerPort, 0);
|
2016-07-18 21:16:56 +03:00
|
|
|
CFRunLoopAddSource(_localServerRunLoop, runLoopSrc, kCFRunLoopCommonModes);
|
2016-07-10 15:10:09 +03:00
|
|
|
CFRelease(runLoopSrc);
|
2016-08-15 11:56:19 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
server_start_neovim();
|
|
|
|
#endif
|
|
|
|
|
2016-07-10 15:10:09 +03:00
|
|
|
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-08-15 11:56:19 +03:00
|
|
|
#ifdef DEBUG_NEOVIM_SERVER_STANDALONE
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
if (_remoteServerPort == NULL) {
|
2016-08-26 16:31:30 +03:00
|
|
|
WLOG("Remote server is null: The msg (%lu:%s) could not be sent.", (unsigned long) msgid, data.cdesc);
|
2016-07-09 23:52:59 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:31:30 +03:00
|
|
|
WLOG("The msg (%lu:%s) could not be sent: %d", (unsigned long) msgid, data.cdesc, responseCode);
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyReadiness {
|
2016-08-15 11:56:19 +03:00
|
|
|
#ifndef DEBUG_NEOVIM_SERVER_STANDALONE
|
2016-07-09 15:04:09 +03:00
|
|
|
[self sendMessageWithId:NeoVimServerMsgIdServerReady data:nil];
|
2016-08-15 11:56:19 +03:00
|
|
|
#endif
|
2016-07-09 15:04:09 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
- (NSData *)handleMessageWithId:(SInt32)msgid data:(NSData *)data {
|
2016-07-09 23:52:59 +03:00
|
|
|
switch (msgid) {
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdAgentReady:
|
2016-07-10 12:46:00 +03:00
|
|
|
server_start_neovim();
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
|
2016-08-04 21:01:03 +03:00
|
|
|
case NeoVimAgentMsgIdCommand: {
|
|
|
|
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
|
|
server_vim_command(string);
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
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
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case NeoVimAgentMsgIdDelete: {
|
|
|
|
NSInteger *values = data_to_NSInteger_array(data, 1);
|
2016-07-10 12:46:00 +03:00
|
|
|
server_delete(values[0]);
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 19:00:05 +03:00
|
|
|
case NeoVimAgentMsgIdQuit:
|
|
|
|
server_quit();
|
|
|
|
return nil;
|
|
|
|
|
2016-08-25 00:06:39 +03:00
|
|
|
case NeoVimAgentMsgIdGetDirtyDocs: {
|
2016-07-18 21:16:56 +03:00
|
|
|
bool dirty = server_has_dirty_docs();
|
|
|
|
return [NSData dataWithBytes:&dirty length:sizeof(bool)];
|
|
|
|
}
|
2016-07-09 23:52:59 +03:00
|
|
|
|
2016-08-25 00:06:39 +03:00
|
|
|
case NeoVimAgentMsgIdGetEscapeFileNames: {
|
2016-08-12 11:51:16 +03:00
|
|
|
NSArray <NSString *> *fileNames = [NSKeyedUnarchiver unarchiveObjectWithData:data];
|
|
|
|
NSMutableArray <NSString *> *result = [NSMutableArray new];
|
|
|
|
|
|
|
|
[fileNames enumerateObjectsUsingBlock:^(NSString* fileName, NSUInteger idx, BOOL *stop) {
|
|
|
|
[result addObject:server_escaped_filename(fileName)];
|
|
|
|
}];
|
|
|
|
|
|
|
|
return [NSKeyedArchiver archivedDataWithRootObject:result];
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:32:42 +03:00
|
|
|
case NeoVimAgentMsgIdGetBuffers: {
|
|
|
|
return [NSKeyedArchiver archivedDataWithRootObject:server_buffers()];
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
default:
|
2016-07-18 21:16:56 +03:00
|
|
|
return nil;
|
2016-07-09 23:52:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 15:04:09 +03:00
|
|
|
@end
|