Terminal Klasse entfernt
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private Terminal Term;
|
||||
private Receiver Rec;
|
||||
private Sender Send;
|
||||
private Thread SenderThread;
|
||||
@@ -17,7 +17,6 @@ public class Program
|
||||
|
||||
public Program ()
|
||||
{
|
||||
Term = new Terminal ();
|
||||
TcpClient client = new TcpClient ("localhost", 9999);
|
||||
Rec = new Receiver (client);
|
||||
Send = new Sender (client);
|
||||
@@ -36,16 +35,15 @@ public class Program
|
||||
string line;
|
||||
while ((line = Rec.Receive()) != null)
|
||||
{
|
||||
Term.PrintLine(line);
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
SenderThread.Abort();
|
||||
Term.Close();
|
||||
}
|
||||
|
||||
public void doSend ()
|
||||
{
|
||||
while (true) {
|
||||
Send.Send(Term.ReadLine());
|
||||
Send.Send(Console.ReadLine());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
101
Terminal.cs
101
Terminal.cs
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user