keijiro / OscJack

Lightweight C# implementation of OSC server/client
The Unlicense
465 stars 64 forks source link

Extensibility for custom OscClient.Send() method signatures ? #37

Open zacksettel opened 1 year ago

zacksettel commented 1 year ago

It would be great to be able to extend the class OscClient in order to create additional OscClient.Send() method signatures. For example, I often need to send the following type of OSC message:

public void Send(string address, string str, float data1, float data2, float data3)

For some time now, I have used a forked version of OscJack, but it seems that if there was a way for me to extend the OscClient class (currently sealed), that would be the best (I rely several different method signatures for OscClient.Send()

Or maybe there's another approach beside the fork?

keijiro commented 1 year ago

It's not extensible at the moment. At least, I should remove sealed from OscClient/OscServer.

aaronvark commented 1 year ago

Is there some reason the signature isn't handled dynamically? Such as:

public void Send(string address, params object[] data)
{
    if (data == null)
    {
        Send(address);
        return;
    }

    _encoder.Clear();
    _encoder.Append(address);

    string format = ",";
    foreach (object o in data)
    {
        if (o is string) format += "s";
        else if (o is int) format += "i";
        else if (o is float) format += "f";
    }

    _encoder.Append(format);

    foreach (object o in data)
    {
        if (o is string) _encoder.Append((string)o);
        else if (o is int) _encoder.Append((int)o);
        else if (o is float) _encoder.Append((float)o);
    }

    _socket.Send(_encoder.Buffer, _encoder.Length, SocketFlags.None);
}
zacksettel commented 1 year ago

Excellent idea. That would make for one less dependency on my forked version (I wrote the same method in my forked version).