GithubHelp home page GithubHelp logo

alec1o / netly Goto Github PK

View Code? Open in Web Editor NEW
57.0 4.0 8.0 2.72 MB

Cross-Platform and Multi-Protocol C# Socket Library. (Extremely fast and easy) ๐Ÿ‡ณ ๐Ÿ‡ช ๐Ÿ‡น ๐Ÿ‡ฑ ๐Ÿ‡พ

Home Page: https://netly.docs.kezero.com

License: MIT License

C# 100.00%
http tcp websocket udp c-sharp dotnet ssl tls rudp mono

netly's Issues

Hard found netly on nuget by search engines. e.g: ``google`` ``bing`` (v4)

Is your feature request related to a problem? Please describe.
Update Netly tags, because the package is hard found on nuget by topic search.

Describe the solution you'd like
add websokets and others relevant tags to be found easy on nuget.
e.g: netly socket sockets lib alec1o kezero ws websocket websockets websocket-server websocket-client client server ...

Describe alternatives you've considered
N/A

Additional context
Update tags on src/Netly.csproj
Update nuget readme to contain websocket and http examples.

Data framing in TCP at the socket level and not at the application level.

๐Ÿ‘ https://stackoverflow.com/questions/73237486/creating-tcp-connection-and-getting-the-header

Protocols like http detect the framing of your data through the stream, and create the framing at the application level, Netly's function is to provide the socket and optionally contains the framing at the application level since version 3, much more I think netly it should be smart enough to create frames through the headers received by the sender. And that:
Relying on TCP headers to create a "frame" rather than creating a frame based on the data received.

Advantage of application-level framing?

  1. Easy to maintain.
    Disadvantage of application-level framing?
  2. Private protocol (When creating your framing, you use its resources to customize it so that it works perfectly in applications with the same protocol. The problem is when you are trying to communicate with libraries that have not been customized to use your framing protocol )
  3. Private in the bubble/Non-resilient (creating a protocol is easy, but it can be difficult to create clients and servers to keep them working with other clients)
  4. Overhead (you are adding more data than you actually need to receive)

Advantage of framing at socket level?

  1. Resilient (if you can put framing at the socket level, it means all applications communicate without dependency and overhead)

  2. No overhead (This is by sending both and receiving data the libraries do not need to send extra data in the message body to handle framing at the application level.

What am I trying to say with framing?

TCP has a very low data limit and if you want to send 1 megabyte you cannot send it linearly, that is, you need to divide this data and send it in small pieces and the framing function both at the application level and at the socket level is to detect and accumulate the data until it reaches 1 megabytes and only then release it to the receiving target. in case of download, it can be a file on the disk to write to the buffer each time it receives a fragment of data.
Netly by default will use the memory as a buffer, this works well if the data to be received is small, i.e. less than ~10 megabytes, and in case of receiving Gigabytes, Netly will need to use the gigabytes in RAM, and that's where the danger lies. and problem. Netly currently has framing at the application level and has a limit of 8 megabytes that it allows to receive and this can be customized up to as much GB, TB, etc. as you want. The next update to fix this should be to allow netly to write the buffer to disk in a temporary folder and then pass the file path instead of data in a parameter. This can be customized, for example from 10MB. In any case, this is a danger that resides in the framing of any layer,
But being realistic, most users don't even use 1MB, they use it to browse messages and small data and will rarely send large files. In any case, netly being able to deal with framing at the socket level brings compatibility without overhead and with data resilience.

Host obtain ip and by url using DNS

# current version
Host instance = new Host("127.0.0.1", 8080);
Host instance = new Host(...);
# new features required
Host instance = new Host("www.alec1o.com:8080");
Hacks
  • It will use DNS to found IP from url, and for prevent block program only will start find IP when Connect (Open) thread or call function like new Host("www.alec1o.com:7777").RunDNS()

Sample WebSocketClient & SampleWebSocketServer

I was looking for a library that supports both tcp and websocket in Unity.
I found it today and wrote code to test websocket.
It works well.
I hope this code is useful to someone who is new to netly.
Screenshot 2024-04-23 at 3 29 45โ€ฏPM

I'll have to test it in Unity tomorrow.
thank you very much your effort.
Sample WebSocketClient & Sample WebSocketServer

NOTE: all received data are bytes, when bufferType is HTTP.Text it means the bytes are text converted to bytes (can be converted to text again), otherwise it means the bytes are Binary or HTTP.Binary and data cannot or should not be converted to text format

SampleWebSocketClient

using System.Text;
using Netly;

namespace SampleWebSocketClient;

class Program
{
    static void Main(string[] args)
    {
        var client = new HTTP.WebSocket();

        client.On.Open(() =>
        {
            Console.WriteLine("client.On.Open");
            
            // send message to server
            byte[] send = NE.GetBytes("hello world!", Encoding.UTF8);
            client.To.Data(send, HTTP.Binary);
        });

        client.On.Close(() =>
        {
            Console.WriteLine("client.On.Close");
        });

        client.On.Error((exception) =>
        {
            Console.WriteLine("client.On.Error " + exception.Message);
        });

        client.On.Data((bytes, bufferType) =>
        {
            string received = NE.GetString(bytes, Encoding.UTF8);
            Console.WriteLine(received);
        });

        client.On.Event((name, bytes, bufferType) =>
        {
            // websocket receives Netly event (Only Netly)
            // EXAMPLE:
            if (name == "client quit")
            {
                // send event to server
                client.To.Event("goodbye", "Some data here", bufferType);
                // close connection.
                client.To.Close();
            }
        });

        client.On.Modify((ws) =>
        {
            // modify socket
        });

        while (true)
        {
            if (!client.IsOpened)
            {
                client.To.Open(new Uri("ws://somedomain.com:8888/echo"));
                Console.WriteLine("client.To.Open");
                
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
            }
            else
            {
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
                    
            }
        }
    }
}

SampleWebSocketServer

using Netly;

namespace SampleWebSocketServer;

class Program
{
    static void Main(string[] args)
    {
        HTTP.Server server = new HTTP.Server();

        server.Map.WebSocket("/echo", (request, websocket) => {

            websocket.On.Data((bytes, bufferType) =>
            {
                // echo data.
                websocket.To.Data(bytes, bufferType);
            });

            websocket.On.Event((name, bytes, bufferType) =>
            {
                // echo event.
                websocket.To.Event(name, bytes, bufferType);
            });
        });


        server.On.Open(() =>
        {
            Console.WriteLine("server.On.Open");
        });

        server.On.Close(() =>
        {
            Console.WriteLine("server.On.Close");
        });

        server.On.Error((exception) =>
        {
            Console.WriteLine("server.On.Error" + exception.Message);
        });

        while (true)
        {
            if (!server.IsOpened)
            {
                server.To.Open(new Uri("http://somedomain.com:8888"));
            }
            else
            {
                // Just Hold the Console
                Console.WriteLine("Message: ");
                string message = Console.ReadLine();
            }
        }
    }
}

Thank you for your project bro

I used your project in a multiplayer game, and I am very grateful to you. Your project is excellent and very beginner-friendly.Looking forward to your game.
--็ˆฑๆฅ่‡ช**

include API documentation and Guide (v4).

Is your feature request related to a problem? Please describe.

the docs of project is manual and maybe can have errors on api. and is hard mantain manual wrote, for fix that should consider include tools to generate api from sources using XML comments.

Describe the solution you'd like

A clear and concise description of what you want to happen.

Describe alternatives you've considered

  1. Found tools to generate docs from XML comments.
    1. DocFx is most easy way to do this! (Most better choice)

Additional context

If the choice is DocFx

    1. the guide must be rewrite. (current version don't have guide)

Sockets problem on Android build

After many test i find it.

Runnig on Android 11 (same error) Real Device
Runnig on Android 9 (Android Emulator) MeMu Play
Runnig on Android 9 (Android Emulator) BlueStacks 5


The origin of error is internal and unknow

It's definitely a DOTNET issue on android. Netly uses System.Net.Sockets as the basis for socket creation. This library is having internal errors in the Android device.

My suspicions:

  1. Android Manifest needs some kind of permissions.
  2. The compilation of System.Net.Sockets internally is not working due to some internal type of conversion (C# to C++/Java or object).

Doubts:

  1. The problem is with the TCP server or TcpClient. What about the server that is not working (listening directly?) or the TcpClient that cannot connect to the socket? (Needs to be tested)

  2. In case the problem is the TcpServer that cannot listen to the localhost on Android or the TcpClient that cannot connect to the localhost. How should you solve it if the problem is internal? (Do nothing?)

  3. How to move forward? Continue writing socket using System.Net.*? or start the base in C++ and the interface in C#?

Originally posted by @alec1o in #33 (comment)

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.