using System;
using UnityEngine;
using CloudPointRpc;

public class MinimalRpcServer : MonoBehaviour
{
    [SerializeField] private string serverIp   = "127.0.0.1";
    [SerializeField] private int    serverPort = 9095;

    private static CrpcTestApi.RpcStringCallback _pingCallback;

    [AOT.MonoPInvokeCallback(typeof(CrpcTestApi.RpcStringCallback))]
    private static IntPtr OnPing(IntPtr rpcStringPtr)
    {
        var req = new RpcString(rpcStringPtr);
        Debug.Log($"[MinimalRpcServer] ping received: {req.Data}");
        return new RpcString("\"pong\"").Handle;
    }

    void Start()
    {
        _pingCallback = OnPing;
        CrpcTestExtensions.CrpcInitWithAddress(serverIp, serverPort);
        CrpcTestExtensions.CrpcAddMethod(_pingCallback, "ping");
        Debug.Log($"[MinimalRpcServer] TCP server started on {serverIp}:{serverPort}, ping registered");
    }

    void OnDestroy() => CrpcServerApi.crpc_deinit();
}
