From 295e3c10bb6d1c83796554edf8369e5aa87a20a8 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Mon, 9 Oct 2017 08:56:25 +0200 Subject: Initial commit. --- Server/App.cs | 10 +++ Server/ClientConnection.cs | 34 +++++++++ Server/LogSingleton.cs | 40 +++++++++++ Server/MainWindow.cs | 76 +++++++++++++++++++++ Server/Program.cs | 19 ++++++ Server/Server.csproj | 50 ++++++++++++++ Server/TcpChatServer.cs | 50 ++++++++++++++ Server/bin/Debug/Eto.Gtk2.dll | Bin 0 -> 314368 bytes Server/bin/Debug/Eto.Gtk3.dll | Bin 0 -> 323584 bytes Server/bin/Debug/Eto.dll | Bin 0 -> 502784 bytes Server/bin/Debug/Server.exe | Bin 0 -> 10752 bytes Server/bin/Debug/Server.pdb | Bin 0 -> 2668 bytes Server/bin/Debug/Xwt.Gtk.dll | Bin 0 -> 286720 bytes Server/bin/Debug/Xwt.Gtk.dll.config | 22 ++++++ Server/bin/Debug/Xwt.dll | Bin 0 -> 414720 bytes Server/bin/Debug/atk-sharp.dll.config | 8 +++ Server/bin/Debug/gdk-sharp.dll.config | 14 ++++ Server/bin/Debug/gio-sharp.dll.config | 10 +++ Server/bin/Debug/glib-sharp.dll.config | 8 +++ Server/bin/Debug/gtk-dotnet.dll.config | 5 ++ Server/bin/Debug/gtk-sharp.dll.config | 12 ++++ Server/bin/Debug/pango-sharp.dll.config | 12 ++++ ....NETFramework,Version=v4.5.AssemblyAttribute.cs | 2 + Server/obj/x86/Debug/Server.csproj.CopyComplete | 0 .../Debug/Server.csproj.CoreCompileInputs.cache | 1 + .../x86/Debug/Server.csproj.FileListAbsolute.txt | 9 +++ .../Debug/Server.csproj.FilesWrittenAbsolute.txt | 13 ++++ .../Server.csprojResolveAssemblyReference.cache | Bin 0 -> 72279 bytes Server/obj/x86/Debug/Server.exe | Bin 0 -> 10752 bytes Server/obj/x86/Debug/Server.pdb | Bin 0 -> 2668 bytes Server/packages.config | 5 ++ 31 files changed, 400 insertions(+) create mode 100644 Server/App.cs create mode 100644 Server/ClientConnection.cs create mode 100644 Server/LogSingleton.cs create mode 100644 Server/MainWindow.cs create mode 100644 Server/Program.cs create mode 100644 Server/Server.csproj create mode 100644 Server/TcpChatServer.cs create mode 100644 Server/bin/Debug/Eto.Gtk2.dll create mode 100644 Server/bin/Debug/Eto.Gtk3.dll create mode 100644 Server/bin/Debug/Eto.dll create mode 100644 Server/bin/Debug/Server.exe create mode 100644 Server/bin/Debug/Server.pdb create mode 100644 Server/bin/Debug/Xwt.Gtk.dll create mode 100644 Server/bin/Debug/Xwt.Gtk.dll.config create mode 100644 Server/bin/Debug/Xwt.dll create mode 100644 Server/bin/Debug/atk-sharp.dll.config create mode 100644 Server/bin/Debug/gdk-sharp.dll.config create mode 100644 Server/bin/Debug/gio-sharp.dll.config create mode 100644 Server/bin/Debug/glib-sharp.dll.config create mode 100644 Server/bin/Debug/gtk-dotnet.dll.config create mode 100644 Server/bin/Debug/gtk-sharp.dll.config create mode 100644 Server/bin/Debug/pango-sharp.dll.config create mode 100644 Server/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs create mode 100644 Server/obj/x86/Debug/Server.csproj.CopyComplete create mode 100644 Server/obj/x86/Debug/Server.csproj.CoreCompileInputs.cache create mode 100644 Server/obj/x86/Debug/Server.csproj.FileListAbsolute.txt create mode 100644 Server/obj/x86/Debug/Server.csproj.FilesWrittenAbsolute.txt create mode 100644 Server/obj/x86/Debug/Server.csprojResolveAssemblyReference.cache create mode 100644 Server/obj/x86/Debug/Server.exe create mode 100644 Server/obj/x86/Debug/Server.pdb create mode 100644 Server/packages.config (limited to 'Server') diff --git a/Server/App.cs b/Server/App.cs new file mode 100644 index 0000000..8d02d69 --- /dev/null +++ b/Server/App.cs @@ -0,0 +1,10 @@ +sing System; +namespace Server +{ + public class App + { + public App() + { + } + } +} diff --git a/Server/ClientConnection.cs b/Server/ClientConnection.cs new file mode 100644 index 0000000..f2d8976 --- /dev/null +++ b/Server/ClientConnection.cs @@ -0,0 +1,34 @@ +using System; +using System.IO; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Server +{ + public class ClientConnection + { + public StreamReader StreamReader { get; set; } + public StreamWriter StreamWriter { get; set; } + + public ClientConnection(TcpListener listener) + { + TcpClient connectionSocket = listener.AcceptTcpClient(); + NetworkStream ns = connectionSocket.GetStream(); + StreamReader = new StreamReader(ns); + StreamWriter = new StreamWriter(ns); + StreamWriter.AutoFlush = true; + Task.Run(() => EchoService(StreamReader, StreamWriter)); + } + + private async Task EchoService(StreamReader sr, StreamWriter sw) + { + while (true) + { + string message = await sr.ReadLineAsync(); + LogSingleton.Instance.Log += $"{message}\n"; + foreach (ClientConnection client in LogSingleton.Instance.ConnectedClients) client.StreamWriter.WriteLine(message); + message = ""; + } + } + } +} diff --git a/Server/LogSingleton.cs b/Server/LogSingleton.cs new file mode 100644 index 0000000..158440d --- /dev/null +++ b/Server/LogSingleton.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace Server +{ + public class LogSingleton : INotifyPropertyChanged + { + private static LogSingleton _instance; + public static LogSingleton Instance => _instance ?? (_instance = new LogSingleton()); + + private string log; + public string Log + { + get + { + return log; + } + set + { + log = value; + OnPropertyChanged(); + } + } + + public List ConnectedClients { get; set; } + + private LogSingleton() + { + ConnectedClients = new List(); + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/Server/MainWindow.cs b/Server/MainWindow.cs new file mode 100644 index 0000000..e8974d2 --- /dev/null +++ b/Server/MainWindow.cs @@ -0,0 +1,76 @@ +using System; +using Xwt; +using Xwt.Formats; + +namespace Server +{ + public class MainWindow : Window + { + TcpChatServer tcpChatServer; + + public MainWindow() + { + Title = "TCP Chat X Server"; + Width = 600; + Height = 400; + + VBox mainVBox; + HBox connectionHBox, messageHBox; + TextEntry ipTextEntry = new TextEntry + { + PlaceholderText = "IP Address", + Text = "127.0.0.1", + }; + TextEntry portTextEntry = new TextEntry + { + WidthRequest = 100, + PlaceholderText = "Port", + Text = "6789" + }; + Button startButton = new Button + { + Label = "Start" + }; + startButton.Clicked += (object sender, EventArgs e) => + { + tcpChatServer = new TcpChatServer(); + tcpChatServer.Start(ipTextEntry.Text, int.Parse(portTextEntry.Text)); + }; + RichTextView outputTextView = new RichTextView + { + ExpandHorizontal = true, + ExpandVertical = true, + MinHeight = 200 + }; + LogSingleton.Instance.PropertyChanged += (sender, args) => + outputTextView.LoadText(LogSingleton.Instance.Log, TextFormat.Plain); + + TextEntry messageTextEntry = new TextEntry() + { + PlaceholderText = "Message" + }; + Button sendButton = new Button + { + Label = "Send" + }; + s