Giter VIP home page Giter VIP logo

upacketdivision's Introduction

uPacketDivision

This is a native plug-in that divides a given data (System.IntPtr or array) into specified sizes and restores them regardless of the input order. It is intended for use cases such as sending large data via UDP.

Install

  • Unity Package
  • Git URL (UPM)
    • Add https://github.com/hecomi/uPacketDivision.git#upm to Package Manager.
  • Scoped Registry (UPM)
    • Add a scoped registry to your project.
      • URL: https://registry.npmjs.com
      • Scope: com.hecomi
    • Install uPacketDivision in Package Manager.

Platforms

Currently, it is only built for Windows.

How to use

Divide

Create a Divider and call Divide<T>(T[]) with the array as input (or Divide(System.IntPtr, int size)). This will divide the packet internally.

Divider divider = new Divider();

void Divide()
{
    Texture2D image;
    var pixels = image.GetPixels32();
    divider.maxPacketSize = packetSize;
    divider.Divide(pixels);
}

Then, send the split data to the remote in some way. The following functions are available.

  • GetChunkCount().
    • the number of chunks.
  • GetChunk()
    • The byte[] array of the split data
  • GetChunkSize(int index).
    • The size of the split data.
  • GetChunkData(int index).
    • The pointer of the split data Here is an example of using uOSC.
uOSC.uOscClient client;

void Send(int width, int height)
{
    client.Send("/Size", width, height);

    for (uint i = 0; i < divider.GetChunkCount(); ++i)
    {
        client.Send("/Data", divider.GetChunk(i));
    }
}

If you want to use the pointer and size directly, please use GetChunkSize() and GetChunkData() instead.

Assemble

Use Assembler to assemble the data sent to you. Here is an example of the receiving part using uOSC.

Assembler assembler = new Assembler();
Texture2D texture;

public void OnDataReceived(uOSC.Message message)
{
    if (message.address == "/Size")
    {
        var w = (int)message.values[0];
        var h = (int)message.values[1];
        OnSize(w, h);
    }
    else if (message.address == "/Data")
    {
        var data = (byte[])message.values[0];
        OnData(data);
        CheckEvent();
    }
}

void OnSize(int w, int h)
{
    texture = new Texture2D(w, h);
}

void OnData(byte[] data)
{
    assembler.timeout = timeout;
    assembler.Add(data);
}

Each time you add data, check for the completion or loss as follows.

void CheckEvent()
{
    switch (assembler.GetEventType())
    {
        case EventType.FrameCompleted:
        {
            OnDataAssembled(assembler.GetAssembledData<Color32>());
            break;
        }
        case EventType.PacketLoss:
        {
            var type = assembler.GetLossType();
            Debug.LogWarning("Loss: " + type);
            break;
        }
        default:
        {
            break;
        }
    }
}

If you want to get a pointer and its size instead of an array, the following APIs are available.

var index = assembler.GetAssembledFrameIndex();
var data = assembler.GetFrameData(index);
var size = assembler.GetFrameSize(index);
OnDataAssembled(data, (int)size);
assembler.RemoveFrame(index);

Then, the data reconstruction will be completed as follows.

void OnDataAssembled(Color32[] pixels)
{
    texture.SetPixels32(pixels);
    texture.Apply();
    GetComponent<Renderer>().material.mainTexture = texture;
}

upacketdivision's People

Contributors

hecomi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

upacketdivision's Issues

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.