Skip to content

Commit

Permalink
some optimizations - you will have to restart all your stuff to use t…
Browse files Browse the repository at this point in the history
…hem though!
  • Loading branch information
adamdruppe committed Apr 27, 2017
1 parent 3323454 commit 2e2e962
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 77 deletions.
6 changes: 3 additions & 3 deletions Makefile
@@ -1,9 +1,9 @@
all:
dmd main.d terminalemulator.d -L-lutil /home/me/arsd/simpledisplay.d /home/me/arsd/color.d /home/me/arsd/eventloop.d -version=with_eventloop -debug -g /home/me/arsd/ttf.d -J/home/me/arsd /home/me/arsd/png.d /home/me/arsd/bmp.d -m64 # -version=use_libssh2 ~/arsd/libssh2.d -ofsshmain
#dmd main.d terminalemulator.d -L-lutil /home/me/arsd/simpledisplay.d /home/me/arsd/color.d /home/me/arsd/eventloop.d -version=with_eventloop -debug -g /home/me/arsd/ttf.d -J/home/me/arsd /home/me/arsd/png.d /home/me/arsd/bmp.d -m64 # -version=use_libssh2 ~/arsd/libssh2.d -ofsshmain
#dmdw main.d /home/me/arsd/simpledisplay.d /home/me/arsd/color.d -debug /home/me/arsd/ttf.d terminalemulator.d -J/home/me/arsd /home/me/arsd/png.d /home/me/arsd/bmp.d -L/SUBSYSTEM:WINDOWS:5.0 -version=use_libssh2 /home/me/arsd/libssh2.d /home/me/arsd/libssh2.lib
#dmd -m64 attach message.d ~/arsd/terminal detachable.d terminalemulator.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop -debug -g utility.d ~/arsd/png.d -version=arsd_te_conservative_draws
dmd -m64 attach message.d ~/arsd/terminal detachable.d terminalemulator.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop -debug -g utility.d ~/arsd/png.d -version=arsd_te_conservative_draws
#dmd utility ~/arsd/terminal terminalemulator ~/arsd/color -version=terminalextensions_commandline ~/arsd/png.d
#dmd nestedterminalemulator.d terminalemulator.d ~/arsd/terminal.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop
#dmdw nestedterminalemulator.d terminalemulator.d ~/arsd/terminal.d ~/arsd/color.d ~/arsd/simpledisplay.d # -version=use_libssh2 /home/me/arsd/libssh2.d /home/me/arsd/libssh2.lib
#dmd -m64 detachable.d terminalemulator.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop message.d ~/arsd/terminal.d -version=standalone_detachable -debug -g # this separate detachable is for debugging, this is also available in attach so the separate thing isn't strictly needed
dmd -m64 detachable.d terminalemulator.d ~/arsd/color.d ~/arsd/eventloop.d -version=with_eventloop message.d ~/arsd/terminal.d -version=standalone_detachable -debug -g # this separate detachable is for debugging, this is also available in attach so the separate thing isn't strictly needed

78 changes: 66 additions & 12 deletions attach.d
Expand Up @@ -89,6 +89,8 @@ struct Session {
// the filename
string sname;

bool mouseTrackingOn;

// not preserved in the file
ChildTerminal[] children;
int activeScreen;
Expand Down Expand Up @@ -186,7 +188,7 @@ void main(string[] args) {

Session session;

if(args.length > 1) {
if(args.length > 1 && args[1] != "--list") {
import std.algorithm : endsWith;

if(args.length == 2 && !endsWith(args[1], ".socket")) {
Expand All @@ -202,12 +204,37 @@ void main(string[] args) {
import std.file, std.process, std.stdio;
bool found = false;
auto dirName = socketDirectoryName();

string[] sessions;
string[] sockets;

foreach(string name; dirEntries(dirName, SpanMode.shallow)) {
writeln(name[dirName.length + 1 .. $]);
name = name[dirName.length + 1 .. $];
if(name[$-1] == 'n')
sessions ~= name[0 .. $ - ".session".length];
else
sockets ~= name[0 .. $ - ".socket".length];
found = true;
}
if(!found)
if(found) {
string[string] associations;
foreach(sessionName; sessions) {
auto sess = Session();
sess.sname = sessionName;
sess.readFromFile();

foreach(s; sess.screens)
associations[s] = sessionName;

writefln("%20s\t%d\t%d", sessionName, sess.pid, sess.screens.length);
}

foreach(socketName; sockets) {
writefln("%s.socket\t\t%s", socketName, (socketName in associations) ? associations[socketName] : "[detached]");
}
} else {
writeln("No screens found");
}
return;
}

Expand Down Expand Up @@ -250,8 +277,10 @@ void main(string[] args) {
auto socket = connectTo(sname);
if(socket is null)
sname = "[failed]";
else
else {
good = true;
sendSimpleMessage(socket, InputMessage.Type.Attach);
}
session.children ~= ChildTerminal(socket, sname, sname);

// we should scan inactive sockets for:
Expand All @@ -271,6 +300,8 @@ void main(string[] args) {
if(session.children[0].socketName == "[vacant]" || session.children[0].socketName == "[failed]")
session.children[0].socketName = null;
session.children[0].socket = connectTo(session.children[0].socketName);
if(auto socket = session.children[0].socket)
sendSimpleMessage(socket, InputMessage.Type.Attach);
}

void saveUpdatedSessionToFile() {
Expand Down Expand Up @@ -495,7 +526,12 @@ void main(string[] args) {
// but the idea is if one is remote detached, they all are,
// so we should just terminate immediately as to not write a new file
return;

break;
case OutputMessageType.mouseTrackingOn:
session.mouseTrackingOn = true;
break;
case OutputMessageType.mouseTrackingOff:
session.mouseTrackingOn = false;
break;
}
} else {
Expand Down Expand Up @@ -741,11 +777,23 @@ void attach(Terminal* terminal, ref Session session, string sname) {

auto newSocket = connectTo(sname);
if(newSocket) {
sendSimpleMessage(newSocket, InputMessage.Type.Attach);

session.children[position] = ChildTerminal(newSocket, sname, sname);
setActiveScreen(terminal, session, position);
}
}

void sendSimpleMessage(Socket socket, InputMessage.Type type) {
InputMessage im;
im.eventLength = InputMessage.type.offsetof + InputMessage.type.sizeof;
im.type = type;
auto len = socket.send((cast(ubyte*)&im)[0 .. im.eventLength]);
if(len <= 0) {
throw new Exception("wtf");
}
}

void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Socket socket) {
// FIXME: UI stuff
static bool escaping;
Expand Down Expand Up @@ -833,6 +881,7 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock
} else if(session.escapeCharacter != dchar.init && ce.character == session.escapeCharacter + 'a' - 1) {
im.type = InputMessage.Type.CharacterPressed;
im.characterEvent.character = 1;
im.eventLength = im.characterEvent.offsetof + im.CharacterEvent.sizeof;
eventToSend = &im;
} else switch(ce.character) {
case 'q': debugMode = !debugMode; break;
Expand Down Expand Up @@ -895,6 +944,7 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock
escaping = true;
} else {
im.type = InputMessage.Type.CharacterPressed;
im.eventLength = im.characterEvent.offsetof + im.CharacterEvent.sizeof;
im.characterEvent.character = ce.character;
eventToSend = &im;
}
Expand Down Expand Up @@ -972,7 +1022,9 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock
auto ev = event.get!(InputEvent.Type.PasteEvent);
auto data = new ubyte[](ev.pastedText.length + InputMessage.sizeof);
auto msg = cast(InputMessage*) data.ptr;
msg.pasteEvent.pastedTextLength = cast(int) ev.pastedText.length;
if(ev.pastedText.length > 4000)
break; // FIXME
msg.pasteEvent.pastedTextLength = cast(short) ev.pastedText.length;

//terminal.writeln(ev.pastedText);

Expand All @@ -981,7 +1033,7 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock
msg.pasteEvent.pastedText.ptr[i] = b;

msg.type = InputMessage.Type.DataPasted;
msg.eventLength = cast(int) data.length;
msg.eventLength = cast(short) data.length;
eventToSend = msg;
break;
case InputEvent.Type.MouseEvent:
Expand All @@ -1001,6 +1053,8 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock
final switch(me.eventType) {
case MouseEvent.Type.Moved:
im.type = InputMessage.Type.MouseMoved;
if(!session.mouseTrackingOn && me.buttons == 0)
return;
break;
case MouseEvent.Type.Pressed:
im.type = InputMessage.Type.MousePressed;
Expand All @@ -1014,9 +1068,9 @@ void handleEvent(Terminal* terminal, ref Session session, InputEvent event, Sock

eventToSend = &im;

im.mouseEvent.x = me.x;
im.mouseEvent.y = me.y;
im.mouseEvent.button = cast(int) me.buttons;
im.mouseEvent.x = cast(short) me.x;
im.mouseEvent.y = cast(short) me.y;
im.mouseEvent.button = cast(ubyte) me.buttons;
im.mouseEvent.modifiers = 0;
if(me.modifierState & ModifierState.shift)
im.mouseEvent.modifiers |= InputMessage.Shift;
Expand Down Expand Up @@ -1067,8 +1121,8 @@ void setActiveScreen(Terminal* terminal, ref Session session, int s, bool force
{
InputMessage im;
im.eventLength = im.sizeof;
im.sizeEvent.width = terminal.width;
im.sizeEvent.height = terminal.height - (session.showingTaskbar ? 1 : 0);
im.sizeEvent.width = cast(short) terminal.width;
im.sizeEvent.height = cast(short) (terminal.height - (session.showingTaskbar ? 1 : 0));
im.type = InputMessage.Type.SizeChanged;
import core.sys.posix.unistd;
write(socket.handle, &im, im.eventLength);
Expand Down
91 changes: 63 additions & 28 deletions detachable.d
Expand Up @@ -61,6 +61,10 @@ import arsd.eventloop;

import std.socket;

import psock = core.sys.posix.sys.socket;
import core.stdc.errno;
import core.sys.posix.unistd;

extern(C)
void detachable_terminal_sigint_handler(int sigNumber) nothrow @nogc {
import arsd.eventloop;
Expand Down Expand Up @@ -127,7 +131,7 @@ class DetachableTerminalEmulator : TerminalEmulator {
}

final void sendOutputMessage(OutputMessageType type, const(void)[] data) {
if(socket !is null) {
if(socket != -1) {
if(data.length)
while(data.length) {
auto sending = data;
Expand All @@ -146,15 +150,15 @@ class DetachableTerminalEmulator : TerminalEmulator {
auto mustSend = frame[0 .. 2 + sending.length];

try_again:
auto sent = socket.send(frame[0 .. 2 + sending.length]);
if(sent < 0 && wouldHaveBlocked()) {
auto sent = psock.send(socket, frame.ptr, 2 + sending.length, 0);
if(sent < 0 && (.errno == EAGAIN || .errno == EWOULDBLOCK)) {
import core.thread;
Thread.sleep(1.msecs);
goto try_again;
}
if(sent != 2 + sending.length) {
//, lastSocketError());
socket = null;
socket = -1;
return;
}
/*
Expand All @@ -174,7 +178,7 @@ class DetachableTerminalEmulator : TerminalEmulator {
ubyte[2] frame;
frame[0] = type;
frame[1] = 0;
socket.send(frame[0 .. 2]);
psock.send(socket, frame.ptr, 2, 0);
/*
again:
if(socket.send(frame) <= 0)
Expand All @@ -185,48 +189,54 @@ class DetachableTerminalEmulator : TerminalEmulator {
}
}

override void mouseMotionTracking(bool b) {
super.mouseMotionTracking(b);
sendOutputMessage(b ? OutputMessageType.mouseTrackingOn : OutputMessageType.mouseTrackingOff, null);
}

mixin ForwardVirtuals!(writer);

Socket listeningSocket;
Socket socket;
int socket = -1;
void acceptConnection() {
assert(listeningSocket !is null);

if(socket !is null) {
sendOutputMessage(OutputMessageType.remoteDetached, null);
socket.close();
removeFileEventListeners(socket.handle);
}
import std.stdio; writeln("accept");

socket = listeningSocket.accept();
auto socket = listeningSocket.accept();
socket.blocking = false; // blocking is bad with the event loop, cuz it is edge triggered
addFileEventListeners(cast(int) socket.handle, &socketReady, null, null);

socket.tupleof[0] = cast(socket_t) -1; // wipe out the internal Phobos socket fd, so it doesn't get closed when it gets GC reaped
}

void socketError() {
socket = null;
socket = -1;
}

void socketReady() {
assert(socket !is null);
void socketReady(int fd) {
ubyte[4096] buffer;
int l2 = 0;
get_more:
auto len = socket.receive(buffer[l2 .. $]);
auto len = psock.recv(fd, buffer.ptr + l2, buffer.length - l2, 0);
//import std.stdio; writeln(fd, " recv ", len);
if(len <= 0) {
// we got it all if it would have blocked
if(wouldHaveBlocked())
if(.errno == EAGAIN || .errno == EWOULDBLOCK)
return;
// they closed, so we'll detach too
socket.shutdown(SocketShutdown.BOTH);
socket.close();
removeFileEventListeners(cast(int) socket.handle);
socket = null;
import std.stdio;
writeln("closing ", fd, " ", .errno, " l2 =", l2);
psock.shutdown(fd, psock.SHUT_RDWR);
close(fd);
removeFileEventListeners(fd);
if(socket == fd)
socket = -1;
return;
}

auto got = buffer[0 .. len + l2];
assert(len > 4);
assert(len >= InputMessage.type.offsetof + InputMessage.type.sizeof);

while(got.length) {
InputMessage* im = cast(InputMessage*) got.ptr;
Expand Down Expand Up @@ -280,8 +290,6 @@ class DetachableTerminalEmulator : TerminalEmulator {
(im.mouseEvent.modifiers & InputMessage.Ctrl) ? true : false
))
redraw;


break;
case InputMessage.Type.DataPasted:
sendPasteData(im.pasteEvent.pastedText.ptr[0 .. im.pasteEvent.pastedTextLength]);
Expand All @@ -300,9 +308,35 @@ class DetachableTerminalEmulator : TerminalEmulator {
connectionActive = false;
break;
case InputMessage.Type.Detach:
// FIXME
break;
case InputMessage.Type.Attach:
// FIXME
if(this.socket != -1) {
import std.stdio; writeln("detached from ", this.socket);
sendOutputMessage(OutputMessageType.remoteDetached, null);
close(this.socket);
removeFileEventListeners(this.socket);
}

socket = fd;

import std.stdio; writeln("attached to ", fd);
break;
case InputMessage.Type.RequestStatus:
// FIXME
// status should give: 1) current title, 2) last 3 lines of output (or something), 3) where it is attached
string message;
message ~= windowTitle;
message ~= "\n";

psock.send(fd, message.ptr, message.length, 0);

import std.stdio; writeln("request status");

close(fd);
removeFileEventListeners(fd);
break;
}
}

Expand All @@ -329,9 +363,10 @@ class DetachableTerminalEmulator : TerminalEmulator {
listeningSocket.shutdown(SocketShutdown.BOTH);
listeningSocket.close();
}
if(socket !is null) {
socket.shutdown(SocketShutdown.BOTH);
socket.close();
if(socket != -1) {
psock.shutdown(socket, psock.SHUT_RDWR);
close(socket);
socket = -1;
}
}

Expand All @@ -346,7 +381,7 @@ class DetachableTerminalEmulator : TerminalEmulator {
bool lastDrawAlternativeScreen;
// FIXME: a lot of code duplication between this and nestedterminalemulator
void redraw(bool forceRedraw = false) {
if(socket is null || !connectionActive)
if(socket == -1 || !connectionActive)
return;


Expand Down

0 comments on commit 2e2e962

Please sign in to comment.