RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3k stars 489 forks source link

Feature/proposal: Add a possibility to handle `ConnectionRequest.cs` outside the lib #520

Closed Adyvan closed 1 year ago

Adyvan commented 1 year ago

Feature/proposal Add a possibility to handle ConnectionRequest.cs outside the lib

Just can copy that instead method public NetPeer AcceptIfKey(string key)

//add usages
using System;
//...
        public NetPeer AcceptIfKey(Func<string, bool> keyValidator)
        {
            if (!TryActivate())
                return null;
            try
            {
                if (true == keyValidator?.Invoke(Data.GetString()))
                    Result = ConnectionRequestResult.Accept;
            }
            catch
            {
                NetDebug.WriteError("[AC] Invalid incoming data");
            }

            if (Result == ConnectionRequestResult.Accept)
                return _listener.OnConnectionSolved(this, null, 0, 0);

            Result = ConnectionRequestResult.Reject;
            _listener.OnConnectionSolved(this, null, 0, 0);
            return null;
        }

        public NetPeer AcceptIfKey(string key)
        {
            return AcceptIfKey(k => k == key);
        }

Library version: commit id (3f91b3c8ac43bfbc72701eb3e0343dfbd6ea6b80)

RevenantX commented 1 year ago

@Adyvan you can handle ConnectionRequest outside library already. Just use Accept and Reject and request Data

Adyvan commented 1 year ago

@RevenantX your approach works only if the key is const in an app instance.

I propose a version that can use in an app that has multiple keys or one key per user

RevenantX commented 1 year ago

@Adyvan no, you can send any data not only text keys with Connect call. And process this data (not only text) in OnConnectionRequest using https://revenantx.github.io/LiteNetLib/api/LiteNetLib.ConnectionRequest.html#LiteNetLib_ConnectionRequest_Data and method Accept() and Reject() You can do own logic this way with any keys and additional check per user.

Adyvan commented 1 year ago

@RevenantX now I got it thanks. my proposal does not make sense, sorry for losing your time