|
1 | 1 | # TinyTcpServer |
2 | | -A small tcp server working under Mono or .NET and provides hooks for handling data exchange with clients (works under mono and .net) |
3 | | -It was fully tested with NUnit Tests on single and multi client (parallel) exchange. |
4 | 2 |
|
5 | | -Also we written 2 simple protocols over this server in separate project: |
| 3 | +# 1. OVERVIEW |
| 4 | +A small tcp server working under Mono or .NET and provides hooks for handling data exchange with MULTIPLE clients (works under mono and .net) and BEHAVIOUR CUSTOMIZATION via C# SCRIPT. |
| 5 | +It was fully tested with NUnit Tests on single and multi client (parallel) exchange. |
6 | 6 |
|
| 7 | +Also we written 2 simple implementations (protocols) over ITcpServer in separate project: |
7 | 8 | Echo server (RFC 862) |
8 | 9 | Time server (RFC 868) |
9 | 10 |
|
10 | | -There is a GUI to run these types of servers and any other types in the near future. |
| 11 | +# 2. SOLUTION STRUCTURE |
| 12 | +/ |
| 13 | +----/Console |
| 14 | +----/GUI |
| 15 | +----TinyTcpServer/ |
| 16 | + ----MossbauerLab.TinyTcpServer.Core |
| 17 | + ----MossbauerLab.TinyTcpServer.Core.FunctionalTests |
| 18 | + ----MossbauerLab.SimpleExtensions |
| 19 | + ----MossbauerLab.SimpleExtensions.Tests |
| 20 | + |
| 21 | +/Console is a console project with management console (build for FlexibleTcpServer working with scripts) |
| 22 | +/GUI is a Windows Forms Tool for management server (build for FlexibleTcpServer working with scripts) |
| 23 | +/TinyTcpServer is a solution with server interface and it extensions (differnt implementation) including FlexibleTcpServer |
| 24 | + |
| 25 | +# 3. NUGET PACKAGE |
| 26 | +https://www.nuget.org/packages/MossbauerLab.TinyTcpServer.Core/ |
| 27 | + |
| 28 | +# 4. FULL EXAMPLE OF HOW TO USE |
| 29 | + |
| 30 | +private const String LocalIpAddress = "127.0.0.1"; |
| 31 | +private const UInt16 ServerPort = 8044; |
| 32 | +private const String Script = @"..\..\TestScripts\SimpleScript.cs"; |
| 33 | + |
| 34 | +private ITcpServer _server; |
| 35 | + |
| 36 | +public void Init() |
| 37 | +{ |
| 38 | + _server = new FlexibleTcpServer(Script, LocalIpAddress, ServerPort); |
| 39 | +} |
11 | 40 |
|
12 | | -We are planning (in near future) to build any protocol over this tcp server using script Engine. In this case server will server for transport purposes and all protocol logic will be in script (C# lamguage). |
| 41 | +That is all ! all logics is inside you script |
| 42 | +There are requirement to presence of initial class and entry method |
13 | 43 |
|
14 | | -We are having nuget package: https://www.nuget.org/packages/MossbauerLab.TinyTcpServer.Core/ |
| 44 | + public class ServerScript |
| 45 | + { |
| 46 | + public void Init(ref ITcpServer server) |
| 47 | + { |
| 48 | + if(server == null) |
| 49 | + throw new NullReferenceException("server"); |
| 50 | + _server = server; |
| 51 | + _connectHandlerId = Guid.NewGuid(); |
| 52 | + _dataHandlerId = Guid.NewGuid(); |
| 53 | + //Console.WriteLine("Init...."); |
| 54 | + _server.AddConnectionHandler(_connectHandlerId, OnClientConnection); |
| 55 | + _server.AddHandler(new TcpClientHandlerInfo(_dataHandlerId), OnClientExchange); |
| 56 | + } |
| 57 | + // ... |
| 58 | + } |
| 59 | + |
| 60 | + // in this method we set up handlers |
| 61 | + Handlers on Connect and Exchange looks like: |
| 62 | + public Byte[] OnClientExchange(Byte[] receivedData, TcpClientHandlerInfo info) |
| 63 | + { |
| 64 | + lock (receivedData) |
| 65 | + { |
| 66 | + Byte[] outputData = new Byte[receivedData.Length]; |
| 67 | + Array.Copy(receivedData, outputData, receivedData.Length); |
| 68 | + return outputData; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void OnClientConnection(TcpClientContext context, Boolean connect) // connect true if client connected and false if disconnected |
| 73 | + { |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + Full example present (in file SimpleScript inside MossbauerLab.TinyTcpServer.FunctionalTests |
| 78 | + |
| 79 | + # 5 Expanded setting |
| 80 | + There are additional settings for TcpServer -> see class TcpServerSettings.cs (MossbauerLab.TinyTcpServer.Core) |
| 81 | + In Console project there is a class that could parse config ftle (key=value) with that settings class is TcpServerConfigBuilder |
| 82 | + |
| 83 | + it handles file, examples of settings: |
| 84 | + # This is a example of settings file all settings are represented as pair key=value, lines started from # are commentary (ignores) |
| 85 | + # number of clients processing the 'same time' |
| 86 | + ParallelTask = 256 |
| 87 | + # buffer on receive for every client (in bytes) |
| 88 | + ClientBufferSize = 65535 |
| 89 | + # chunk is a auant of size for read and write operations |
| 90 | + ChunkSize = 4096 |
| 91 | + # number of times in a row that calls BeginAccept (in a sepatarate from IO processing thread) |
| 92 | + ClientConnectAttempts = 4 |
| 93 | + # time while client stays inactive, after this time is off (in seconds) client will be disconneced by server |
| 94 | + ClientInactivityTime = 120 |
| 95 | + # timeout for BeginAccept in milliseconds |
| 96 | + ClientConnectTimeout = 1000 |
| 97 | + # number of attempts in a row to get data from client |
| 98 | + ClientReadAttempts = 8 |
| 99 | + # timeout in milliseconds on every read attemp |
| 100 | + ReadTimeout = 200 |
| 101 | + # timeout in milliseconds for server to shutdown, close all opened resources |
| 102 | + ServerCloseTimeout = 2000 |
| 103 | + # timeout in milliseconds to complete write operation |
| 104 | + WriteTimeout = 1000 |
15 | 105 |
|
16 | | -Contributors |
17 | | -EvilLord666 aka Ushakov Michael |
| 106 | + # 6 CONTRIBUTORS |
| 107 | + EvilLord666 aka Ushakov Michael |
| 108 | + KatanaZZZ aka Anonymous |
0 commit comments