All Projects → hiramtan → Hisocket

hiramtan / Hisocket

It is a lightweight client socket solution, you can used it in C# project or Unity3d

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Hisocket

Stick
solution of "sticking packets" for TCP network transmission
Stars: ✭ 261 (-5.09%)
Mutual labels:  bytes, tcp, socket, net
Unitypluginwithwsl
Unity native plugin with WSL (Windows Subsystem for Linux)
Stars: ✭ 39 (-85.82%)
Mutual labels:  unity, unity3d, plugin, plugins
Meshstreaminggrasshopper
Plugin for Grasshopper to stream mesh geometry through web socket.
Stars: ✭ 52 (-81.09%)
Mutual labels:  unity, unity3d, plugin
Klak
Creative coding library for Unity
Stars: ✭ 1,347 (+389.82%)
Mutual labels:  unity, unity3d, plugin
Textureupdateexample
An example showing how to update textures from a native plugin in Unity.
Stars: ✭ 133 (-51.64%)
Mutual labels:  unity, unity3d, plugin
Simpleunitytcp
🖧 Simple Unity Project to show how TCP communication are builded in C# without multi-threading or Unity network (Unet) involved.
Stars: ✭ 22 (-92%)
Mutual labels:  unity, unity3d, tcp
Minimumaudioplugin
Minimum implementation of a native audio plugin for Unity
Stars: ✭ 33 (-88%)
Mutual labels:  unity, unity3d, plugin
Ruffles
Lightweight and fully managed reliable UDP library.
Stars: ✭ 131 (-52.36%)
Mutual labels:  unity, network, net
Klakspout
Spout plugin for Unity
Stars: ✭ 332 (+20.73%)
Mutual labels:  unity, unity3d, plugin
Unitymobileinput
Unity mobile Input plugin for iOS and Android (Unity UI compatible)
Stars: ✭ 170 (-38.18%)
Mutual labels:  unity, unity3d, plugins
Ecs
ECS for Unity with full game state automatic rollbacks
Stars: ✭ 151 (-45.09%)
Mutual labels:  unity, unity3d, network
Nice Lua
基于xlua的MVVM框架,支持Addressables, 统一渲染管线等Unity新特性
Stars: ✭ 207 (-24.73%)
Mutual labels:  unity, unity3d, network
Networker
A simple to use TCP and UDP networking library for .NET. Compatible with Unity.
Stars: ✭ 408 (+48.36%)
Mutual labels:  unity, unity3d, tcp
Klakndi
NewTek NDI™ plugin for Unity
Stars: ✭ 401 (+45.82%)
Mutual labels:  unity, unity3d, plugin
Game Networking Resources
A Curated List of Game Network Programming Resources
Stars: ✭ 4,208 (+1430.18%)
Mutual labels:  unity, unity3d, network
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (-40.36%)
Mutual labels:  bytes, network, tcp
Yasio
A multi-platform support c++11 library with focus on asio (asynchronous socket I/O) for any client application.
Stars: ✭ 483 (+75.64%)
Mutual labels:  unity3d, tcp, socket
Pesocket
A C# Network Library.
Stars: ✭ 134 (-51.27%)
Mutual labels:  unity3d, network, socket
Klaksyphon
Syphon plugin for Unity
Stars: ✭ 149 (-45.82%)
Mutual labels:  unity, unity3d, plugin
Unitysocketprotobuf3demo
主要实现了用Unity对接了Leaf服务器。其次带了些小工具。
Stars: ✭ 244 (-11.27%)
Mutual labels:  unity, tcp, socket

HiSocket

It is a lightweight client socket solution, you can used it in Unity3d or C# project

Packagist Build Status GitHub release


中文说明

How to use

Quick Start:

        //tcp example
        private IPackage package = new PackageExample();
        private TcpConnection tcp;
        void Init()
        {
            tcp = new TcpConnection(package);
            tcp.OnConnected += OnConnected;
            tcp.OnReceive += OnReceive;
            //...
            //...
            tcp.Connect("127.0.0.1",999);
        }
        void OnConnected()
        {
            //connect success
            tcp.Send(new byte[10]);//send message
        }

        void OnReceive(byte[] bytes)
        {
            //get message from server
        }

More example:


General

This project contains:

  • Tcp
    • TcpConnection
    • TcpSocket
    • Plugin
      • Ping
      • Statistical
  • Message
    • Binary Message
    • Protobuf Message
    • Aes encryption
  • BlockBuffer

Features

  • Support Tcp socket
  • Scalable byte Array
  • High-performance byte block buffer
  • Message registration and call back
  • Support byte message
  • Support protobuf message
  • AES encryption

Details

  • Use async connection in main thread(avoid thread blocking).
  • Using Circular_buffer to avoid memory allocation every time, and reduce garbage collection.
  • You can get current connect state and message by adding listener of event.
  • If you use Tcp socket, you should implement IPackage interface to pack or unpack message.
  • Ping: there is a ping plugin you can used, but if you are used in unity3d because of the bug of mono, it will throw an error on .net2.0(.net 4.6 will be fine, also you can use unity's api to get ping time)

Advanced

  • If you are clear about socket, you also can use TcpSocket to achieve your logic, anyway the recommend is TcpConnection.
  • You can use API get socket and do extra logic, for example modify socket's out time
  • You can use API get send and receive buffer, for example when disconnect, how to handle buffer's data? just clear or resend to server.
  • OnSocketReceive and OnReceive are diffrent, for example OnSocketReceive size is 100 byte, if user do nothing when uppack OnReceive size is 100. but when user do some zip/unzip(encription.etc) OnReceive size is not 100 anymore.
  • You can add many different plugins based on TcpConnection to achieve different functions.
  • There are a message register base class help user to quick register id and callback(based on reflection)
  • The encryption is use AES, if you want to use encryption you can use the API to encrypte your bytes.
  • .etc

Instructions

Tcp provides reliable, ordered, and error-checked delivery of a stream of bytes. you have to split bytes by yourself, in this framework you can implement IPackage interface to achieve this.

Because Tcp is a a stream of bytes protocol, user should split the bytes to get correct message package. when create a tcp socket channel there must be a package instance to pack and unpack message.

Pack and Unpack message: In the beginning we define a packager to split bytes, when send message we add length in the head of every message and when receive message we use this length to get how long our message is.

Udp provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram. that means you don't know current connect state, but package is integrated.

If use Udp connection shold define send and receive's buffer size.

  • Ping :    Because there is a bug with mono on .net 2.0 and subset in unity3d, you can use logic as below.
    public int PingTime;
    private Ping p;
    private float timeOut = 1;
    private float lastTime;
    void Start()
    {
        StartCoroutine(Ping());
    }
    IEnumerator Ping()
    {
        p = new Ping("127.0.0.1");
        lastTime = Time.realtimeSinceStartup;
        while (!p.isDone && Time.realtimeSinceStartup - lastTime < 1)
        {
            yield return null;
        }
        PingTime = p.time;
        p.DestroyPing();
        yield return new WaitForSeconds(1);
        StartCoroutine(Ping());
    }
    

Example

There are many example in HiSocketExample project or in HiSocket.unitypackage, here is some of them:

Package example:

public class PackageExample:PackageBase
    {
        protected override void Pack(BlockBuffer<byte> bytes, Action<byte[]> onPacked)
        {
            //Use int as header
            int length = bytes.WritePosition;
            var header = BitConverter.GetBytes(length);
            var newBytes = new BlockBuffer<byte>(length + header.Length);
            //Write header and body to buffer
            newBytes.Write(header);
            newBytes.Write(bytes.Buffer);
            //Notice pack funished
            onPacked(newBytes.Buffer);
        }

        protected override void Unpack(BlockBuffer<byte> bytes, Action<byte[]> onUnpacked)
        {
            //Because header is int and cost 4 byte
            while (bytes.WritePosition > 4)
            {
                int length = BitConverter.ToInt32(bytes.Buffer, 0);
                //If receive body
                if (bytes.WritePosition >= 4 + length)
                {
                    bytes.MoveReadPostion(4);
                    var data = bytes.Read(length);
                    //Notice unpack finished
                    onUnpacked(data);
                    bytes.ResetIndex();
                }
            }
        }
    }
TcpConnection tcp;
        void Connect()
        {
            tcp = new TcpConnection(new PackageExample());
            tcp.OnDisconnected += OnDisconnect;
            tcp.Connect("127.0.0.1", 999);
            tcp.Socket.NoDelay = true;
            tcp.Socket.SendTimeout = 100;
            tcp.Socket.ReceiveTimeout = 200;
            //...


            // you can add plugin sub from IPlugins
            tcp.AddPlugin(new StatisticalPlugin("Statistical"));//this plugin calculate how many send
        }

        void OnDisconnect()
        {
            var length = tcp.SendBuffer.WritePosition;
            Console.WriteLine("Still have {0} not send to server when abnormal shutdown");
            var data = tcp.SendBuffer.Read(length);
            tcp.SendBuffer.ResetIndex();

            //use can handle these data, for example maybe can send next time when connect again
            //tcp.Send(data);
        }
 /// <summary>
    /// The recommend is use TcpConnection 
    /// </summary>
    class Example3
    {
        TcpSocket tcp; //The recommend is use TcpConnection 
        void Connect()
        {
            tcp = new TcpSocket(1024);//set buffer size
            tcp.OnReceiveBytes += OnReceive;
            tcp.Connect("127.0.0.1", 999);
        }

        void OnReceive(byte[] bytes)
        {
            //split bytes here
        }
    }

support: [email protected]


MIT License

Copyright (c) [2017] [Hiram]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].