From 23836945c93ad9419d2e6b9e5d1f1128528f6d55 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 21 Nov 2016 20:24:22 +0100 Subject: [PATCH] Some Basic stuff --- SMTPServer/SMTPServer.sln | 2 + .../Exceptions/PortUsedException.cs | 22 +++++++ SMTPServer/SMTPServer/PortListener.cs | 64 +++++++++++++++++++ SMTPServer/SMTPServer/Program.cs | 17 ++++- SMTPServer/SMTPServer/SMTPServer.csproj | 11 ++-- SMTPServer/SMTPServer/StartTcpConnection.cs | 16 +++++ SMTPServer/SMTPServer/TCPListener.cs | 64 +++++++++++++++++++ SMTPServer/TCPListener.cs | 8 +++ 8 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 SMTPServer/SMTPServer/Exceptions/PortUsedException.cs create mode 100644 SMTPServer/SMTPServer/PortListener.cs create mode 100644 SMTPServer/SMTPServer/StartTcpConnection.cs create mode 100644 SMTPServer/SMTPServer/TCPListener.cs create mode 100644 SMTPServer/TCPListener.cs diff --git a/SMTPServer/SMTPServer.sln b/SMTPServer/SMTPServer.sln index a907b9c..0591f69 100644 --- a/SMTPServer/SMTPServer.sln +++ b/SMTPServer/SMTPServer.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.25914.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTPServer", "SMTPServer\SMTPServer.csproj", "{ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D43013F8-933F-4ADC-8943-08E91662A070}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/SMTPServer/SMTPServer/Exceptions/PortUsedException.cs b/SMTPServer/SMTPServer/Exceptions/PortUsedException.cs new file mode 100644 index 0000000..ee405ba --- /dev/null +++ b/SMTPServer/SMTPServer/Exceptions/PortUsedException.cs @@ -0,0 +1,22 @@ +using System; + +namespace SMTPServer.Exceptions +{ + public class PortUsedException : Exception + { + public PortUsedException() : base() + { + + } + + public PortUsedException(string message) : base(message) + { + + } + + public PortUsedException(string message, Exception inner) : base(message, inner) + { + + } + } +} \ No newline at end of file diff --git a/SMTPServer/SMTPServer/PortListener.cs b/SMTPServer/SMTPServer/PortListener.cs new file mode 100644 index 0000000..a85ffa5 --- /dev/null +++ b/SMTPServer/SMTPServer/PortListener.cs @@ -0,0 +1,64 @@ +using SMTPServer.Exceptions; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace SMTPServer +{ + public delegate void ConnectedEventHandler(object source, ConnectedEventArgs e); + + public class ConnectedEventArgs : EventArgs + { + private string EventInfo; + + public Socket Client { get; private set; } + + public ConnectedEventArgs(Socket client) + { + Client = client; + } + } + + public class PortListener + { + static List _Ports = null; + TcpListener _Listener; + + public event ConnectedEventHandler OnConnected; + + public PortListener(int port) + { + if(_Ports == null) + { + _Ports = new List(); + } + + foreach(var i in _Ports) + { + if (i == port) + { + throw new PortUsedException(); + } + } + + _Ports.Add(port); + StartListening(new TcpListener(IPAddress.Any, port)); + } + + private void StartListening(TcpListener listener) + { + listener.Start(); + _Listener = listener; + Thread thread = new Thread(new ThreadStart(ListenerAsync)); + } + + private async void ListenerAsync() + { + var client = await _Listener.AcceptSocketAsync(); + OnConnected(this, new ConnectedEventArgs(client)); + } + } +} diff --git a/SMTPServer/SMTPServer/Program.cs b/SMTPServer/SMTPServer/Program.cs index 104ecf0..85aea45 100644 --- a/SMTPServer/SMTPServer/Program.cs +++ b/SMTPServer/SMTPServer/Program.cs @@ -1,9 +1,20 @@ using System; -class Program +namespace SMTPServer { - static void Main(string[] args) + class Program { - Console.WriteLine("Hello World!"); + static void Main(string[] args) + { + Console.WriteLine("####################"); + + var listener = new PortListener(5122); + listener.OnConnected += Listener_OnConnected; + } + + private static void Listener_OnConnected(object source, ConnectedEventArgs e) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/SMTPServer/SMTPServer/SMTPServer.csproj b/SMTPServer/SMTPServer/SMTPServer.csproj index 35c4d2b..fbb51e6 100644 --- a/SMTPServer/SMTPServer/SMTPServer.csproj +++ b/SMTPServer/SMTPServer/SMTPServer.csproj @@ -1,25 +1,24 @@ - + - Exe netcoreapp1.0 - - + + + - 1.0.1 + 1.1.0 1.0.0-alpha-20161104-2 All - \ No newline at end of file diff --git a/SMTPServer/SMTPServer/StartTcpConnection.cs b/SMTPServer/SMTPServer/StartTcpConnection.cs new file mode 100644 index 0000000..b97b533 --- /dev/null +++ b/SMTPServer/SMTPServer/StartTcpConnection.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; + +namespace SMTPServer +{ + class StartTcpConnection + { + public StartTcpConnection(int port, IPAddress destination) + { + + } + + } +} diff --git a/SMTPServer/SMTPServer/TCPListener.cs b/SMTPServer/SMTPServer/TCPListener.cs new file mode 100644 index 0000000..a1017ea --- /dev/null +++ b/SMTPServer/SMTPServer/TCPListener.cs @@ -0,0 +1,64 @@ +using SMTPServer.Exceptions; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace SMTPServer +{ + public delegate void ConnectedEventHandler(object source, ConnectedEventArgs e); + + public class ConnectedEventArgs : EventArgs + { + private string EventInfo; + + public Socket Client { get; private set; } + + public ConnectedEventArgs(Socket client) + { + Client = client; + } + } + + public class TCPListener + { + static List _Ports = null; + TcpListener _Listener; + + public event ConnectedEventHandler OnConnected; + + public TCPListener(int port) + { + if(_Ports == null) + { + _Ports = new List(); + } + + foreach(var i in _Ports) + { + if (i == port) + { + throw new PortUsedException(); + } + } + + _Ports.Add(port); + StartListening(new TcpListener(IPAddress.Any, port)); + } + + private void StartListening(TcpListener listener) + { + listener.Start(); + _Listener = listener; + Thread thread = new Thread(new ThreadStart(ListenerAsync)); + } + + private async void ListenerAsync() + { + var client = await _Listener.AcceptSocketAsync(); + OnConnected(this, new ConnectedEventArgs(client)); + } + } +} diff --git a/SMTPServer/TCPListener.cs b/SMTPServer/TCPListener.cs new file mode 100644 index 0000000..d555ecb --- /dev/null +++ b/SMTPServer/TCPListener.cs @@ -0,0 +1,8 @@ +using System; + +public class Class1 +{ + public Class1() + { + } +}