Coder Social home page Coder Social logo

mossbauerlab / tinytcpserver Goto Github PK

View Code? Open in Web Editor NEW
17.0 7.0 6.0 588 KB

A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.

Home Page: https://mossbauerlab.github.io/TinyTcpServer

License: GNU General Public License v3.0

C# 99.78% Batchfile 0.22%
tcp-server hooks network tcp-ip server multi-client charp-tcp-server tcp tcp-socket tcp-client

tinytcpserver's Introduction

TinyTcpServer

1. OVERVIEW

A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with MULTIPLE clients (works under mono and .net) and BEHAVIOUR CUSTOMIZATION via C# SCRIPT. It was fully tested with NUnit Tests on single and multi client (parallel) exchange.

Also we written 2 simple implementations (protocols) over ITcpServer in separate project: Echo server (RFC 862) Time server (RFC 868)

2. SOLUTION STRUCTURE

/   
   ----/Console
   ----/GUI
   ----TinyTcpServer/

              ----MossbauerLab.TinyTcpServer.Core
              ----MossbauerLab.TinyTcpServer.Core.FunctionalTests
              ----MossbauerLab.SimpleExtensions
              ----MossbauerLab.SimpleExtensions.Tests

/Console is a console project with management console (build for FlexibleTcpServer working with scripts) /GUI is a Windows Forms Tool for management server (build for FlexibleTcpServer working with scripts) /TinyTcpServer is a solution with server interface and it extensions (differnt implementation) including FlexibleTcpServer

3. NUGET PACKAGE

https://www.nuget.org/packages/MossbauerLab.TinyTcpServer.Core/

4. FULL EXAMPLE OF HOW TO USE

`

private const String LocalIpAddress = "127.0.0.1"; 
private const UInt16 ServerPort = 8044;   
private const String Script = @"..\..\TestScripts\SimpleScript.cs";
    
private ITcpServer _server;

public void Init()
{
    CompilerOptions options = new CompilerOptions();
    CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<String, String>()
    {
        {"CompilerVersion", "v4.0"}
    });
    options.Parameters = new CompilerParameters(new string[] {"System.Web"});
    options.Parameters.GenerateExecutable = false;
    options.Parameters.GenerateInMemory = true;
    options.ScriptEntryType = "TestEchoScript.EchoScript";
    
    _server = new FlexibleTcpServer(Script, LocalIpAddress, ServerPort);    
}

`

/*That is all ! all logics is inside you script There are requirement to presence of initial class and entry method. Full examples could be found in

  • MossbauerLab.TinyTcpServer.Core.FunctionalTests/Server/TestFlexibleTcpServer.cs
  • In Console and GUI projects with Utils class for getting CompilerOptions and TcpServerConfig from very simple files (Key=Value) */

`

 public class ServerScript
 {
     public void Init(ref ITcpServer server)
     {
         if(server == null)
             throw new NullReferenceException("server");
         _server = server;
         _connectHandlerId = Guid.NewGuid();
         _dataHandlerId = Guid.NewGuid();
         //Console.WriteLine("Init....");
         _server.AddConnectionHandler(_connectHandlerId, OnClientConnection);
         _server.AddHandler(new TcpClientHandlerInfo(_dataHandlerId), OnClientExchange);
     }
     // ...
 }

 // in this method we set up handlers
 // Handlers on Connect and Exchange looks like:
 public Byte[] OnClientExchange(Byte[] receivedData, TcpClientHandlerInfo info)
 {
     lock (receivedData)
     {
         Byte[] outputData = new Byte[receivedData.Length];
         Array.Copy(receivedData, outputData, receivedData.Length);
         return outputData;
     }
 }
 
 // connect true if client connected and false if disconnected 
 public void OnClientConnection(TcpClientContext context, Boolean connect)  
 
 {
        
 }

`

Full example present (in file SimpleScript inside MossbauerLab.TinyTcpServer.FunctionalTests

5 Expanded setting

There are additional settings for TcpServer -> see class TcpServerSettings.cs (MossbauerLab.TinyTcpServer.Core) In Console project there is a class that could parse config ftle (key=value) with that settings class is TcpServerConfigBuilder

it handles file, examples of settings: `

 # number of clients processing the 'same time'
 ParallelTask = 256
 # buffer on receive for every client (in bytes)
 ClientBufferSize = 65535
 # chunk is a auant of size for read and write operations
 ChunkSize = 4096
 # number of times in a row that calls BeginAccept (in a sepatarate from IO processing thread)
 ClientConnectAttempts = 4
 # time while client stays inactive, after this time is off (in seconds) client will be disconneced by server
 ClientInactivityTime = 120
 # timeout for BeginAccept in milliseconds
 ClientConnectTimeout = 1000
 # number of attempts in a row to get data from client
 ClientReadAttempts = 8
 # timeout in milliseconds on every read attemp 
 ReadTimeout = 200
 # timeout in milliseconds for server to shutdown, close all opened resources
 ServerCloseTimeout = 2000
 # timeout in milliseconds to complete write operation
 WriteTimeout = 1000 

`

6 CONTRIBUTORS

 EvilLord666 aka Ushakov Michael
 KatanaZZZ aka Anonymous

tinytcpserver's People

Contributors

evillord666 avatar katanazzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinytcpserver's Issues

Common server API

API must contains following functions:

  1. Start on pair IP, port. If not defined will be used last values or default

  2. Stop server

  3. Restart server

  4. Add or remove filter (by tcp port, by ip address)

  5. Add or remove handlers on Receive e.t.c

  6. Send data to defined client (ip+tcp port)

[1.2] Script Engine

There is a idea to make server awailable to work with any protocols, over TinyTcpServer itself (as transport layer). Possibly script engine will be Roslyn

1.2.3 More convenient GUI tool

We have to make GUI more convenient to use:

  • display and edit config options (grid like control) with possibility to export
  • logging more cases 4 FlexibleTcpServer (display compiler options)
  • remember directories path

[1.1] More logical API

Surely Start and Restart method must be overloaded with function without parameters for starting and restarting server on the same IP and Port.

[1.3] Multiple platform support

Now TinyTcpServer supports only net40, we should support multiple platforms:

net45 (add additional) async Interface

netstandard2.0

netcoreapp2.0+

Use TcpClient for test

I think parallel to NetworkClient class that works with pure sockets we should add option to run server with TcpClient, and this would be the way to TinyTcpClient project.

Release 1.1

The following task should be included in release

#2
#3
#4
#5
#6

However, I was not able to solve all the problems with multiclient parallel working without any protocol on raw data. Maybe it is impossible to do it without some tricks: protocls, resending e.t.c. Hoever we should think if we could speed up our server (v.1.2) and we shoul measure it.

[1.2] GUI Adjustment

For case when we are running server with script we must adjust GUI for this case

[1.2.1] More powerful scripting

I wrote complicated scripts depending on other assemblies and got compilation errors (possibly due to compiler was unable to load referenced assemblies), so we should properly adjust compiler (via one more parameter to FlexlibleTcpServer).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.