forked from onflapp/gs-webbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalWebView.m
129 lines (105 loc) · 3.89 KB
/
ExternalWebView.m
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
/*
Project: WebBrowser
Copyright (C) 2022 Free Software Foundation
Author: Ondrej Florian,,,
Created: 2022-04-09 19:44:48 +0200 by oflorian
This application 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 application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#import "ExternalWebView.h"
#include <netinet/in.h>
@implementation ExternalWebView
- (void) dealloc {
[self disconnectController];
[super dealloc];
}
- (void) connectController:(NSFileHandle*) fh {
/*
int sock, reuse = 1;
int port = 0;
struct sockaddr_in sockaddr;
socklen_t socklen = sizeof(sockaddr);
buff = [NSMutableString new];
memset(&sockaddr, 0, sizeof(struct sockaddr_in));
sockaddr.sin_addr.s_addr = GSSwapHostI32ToBig(INADDR_ANY);
sockaddr.sin_port = GSSwapHostI16ToBig(port);
if((sock = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) == -1)
NSLog(@"Unable to create socket - %s\n", strerror(errno));
else if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(int)) == -1)
NSLog(@"Couldn't set reuse on socket - %s\n", strerror(errno));
else if(bind(sock, (struct sockaddr*)&sockaddr, sizeof(sockaddr)))
NSLog(@"Couldn't bind to port %d - %s\n", port, strerror(errno));
else if(listen(sock, 5) == -1)
NSLog(@"Couldn't listen %d - %s\n", port, strerror(errno));
else if (getsockname(sock, (struct sockaddr*)& sockaddr, &socklen) == -1)
NSLog(@"Couldn't get name %s\n", strerror(errno));
else {
listener_port = GSSwapBigI16ToHost(sockaddr.sin_port);
listener = [[NSFileHandle alloc] initWithFileDescriptor:sock closeOnDealloc:YES];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(acceptConnection:)
name: NSFileHandleConnectionAcceptedNotification
object: listener];
[listener acceptConnectionInBackgroundAndNotify];
}
*/
[self disconnectController];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(handleDataOnConnection:)
name: NSFileHandleReadCompletionNotification
object: fh];
remote = [fh retain];
[remote readInBackgroundAndNotify];
[self sendCommand:@"HELLO:"];
}
- (void) disconnectController {
[[NSNotificationCenter defaultCenter] removeObserver: self];
[remote release];
remote = nil;
[buff release];
buff = [NSMutableString new];
}
- (Window) createXWindowID {
return 0;
}
- (void) sendCommand:(NSString*) cmd {
NSString* ss = [NSString stringWithFormat:@"%@\n", cmd];
NSData* data = [ss dataUsingEncoding:NSUTF8StringEncoding];
[remote writeData:data];
}
- (void) receiveCommand:(NSString*) cmd {
NSLog(@"command: %@", cmd);
}
- (void) handleDataOnConnection:(id) not {
NSData *data = [[not userInfo] objectForKey:NSFileHandleNotificationDataItem];
if ([data length] > 0) {
NSString* ss = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[buff appendString:ss];
NSLog(@"data: [%@]", buff);
NSRange r = [buff rangeOfString:@"\n"];
while (r.location != NSNotFound) {
NSString* cmd = [buff substringWithRange:NSMakeRange(0, r.location)];
if ([cmd length] > 0) {
[self receiveCommand:cmd];
}
[buff setString:[buff substringFromIndex:r.location+1]];
r = [buff rangeOfString:@"\n"];
}
[remote readInBackgroundAndNotify];
}
else {
[self disconnectController];
}
}
@end