Terminal Klasse entfernt

This commit is contained in:
Daniel Herrmann
2014-03-27 12:07:02 +01:00
parent e3c670b156
commit ff8cba68ab
2 changed files with 3 additions and 106 deletions

View File

@@ -1,9 +1,9 @@
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System;
public class Program public class Program
{ {
private Terminal Term;
private Receiver Rec; private Receiver Rec;
private Sender Send; private Sender Send;
private Thread SenderThread; private Thread SenderThread;
@@ -17,7 +17,6 @@ public class Program
public Program () public Program ()
{ {
Term = new Terminal ();
TcpClient client = new TcpClient ("localhost", 9999); TcpClient client = new TcpClient ("localhost", 9999);
Rec = new Receiver (client); Rec = new Receiver (client);
Send = new Sender (client); Send = new Sender (client);
@@ -36,16 +35,15 @@ public class Program
string line; string line;
while ((line = Rec.Receive()) != null) while ((line = Rec.Receive()) != null)
{ {
Term.PrintLine(line); Console.WriteLine(line);
} }
SenderThread.Abort(); SenderThread.Abort();
Term.Close();
} }
public void doSend () public void doSend ()
{ {
while (true) { while (true) {
Send.Send(Term.ReadLine()); Send.Send(Console.ReadLine());
} }
} }

View File

@@ -1,101 +0,0 @@
using System;
using System.Threading;
// Wrapper class for the console allowing concurrent read and write operations
public class Terminal
{
private Object TerminalLock = new Object ();
private Object ReadLineLock = new Object ();
private AutoResetEvent ResetEvent = new AutoResetEvent (false);
private String InputBuffer = "";
private bool AcceptInput = false;
private int OutLinePos;
private Thread catchInputThread;
public Terminal ()
{
OutLinePos = Console.CursorTop;
catchInputThread = new Thread (new ThreadStart (this.CatchInput));
catchInputThread.Start ();
}
private void CatchInput ()
{
while (true) {
ConsoleKeyInfo key = Console.ReadKey (false);
lock (TerminalLock) {
if (!AcceptInput) {
Console.CursorLeft--;
Console.Write (" ");
Console.CursorLeft--;
continue;
}
if (key.Key == ConsoleKey.Backspace) {
if (InputBuffer.Length == 0)
continue;
InputBuffer = InputBuffer.Substring (0, InputBuffer.Length - 1);
ClearInputLine ();
Console.Write (InputBuffer);
} else if (key.Key == ConsoleKey.Enter) {
AcceptInput = false;
ResetEvent.Set ();
OutLinePos = Console.CursorTop;
} else {
InputBuffer += key.KeyChar;
ClearInputLine ();
Console.Write (InputBuffer);
}
}
}
}
public void PrintLine (String s)
{
lock (TerminalLock) {
ClearInputLine ();
Console.WriteLine (s);
OutLinePos = Console.CursorTop;
Console.Write (InputBuffer);
}
}
public String ReadLine ()
{
lock (ReadLineLock) {
try {
String result;
AcceptInput = true;
ResetEvent.WaitOne ();
result = InputBuffer;
InputBuffer = "";
return result;
} catch (ThreadAbortException e) {
lock (TerminalLock) {
AcceptInput = false;
InputBuffer = "";
ClearInputLine ();
}
throw e;
}
}
}
public void ClearInputLine ()
{
lock (TerminalLock) {
int top = Console.CursorTop;
int left = Console.CursorLeft;
Console.SetCursorPosition (0, OutLinePos);
while (left != Console.CursorLeft || top != Console.CursorTop) {
Console.Write (" ");
}
Console.SetCursorPosition (0, OutLinePos);
}
}
public void Close ()
{
catchInputThread.Abort();
}
}