using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace OliviaAddIn
{
    unsafe public class Cstr_socket
    {
        //variables------------------------
        private IntPtr sc;

        //----------------------------------
        public Cstr_socket()
        {
            sc = IntPtr.Zero;
        }
        ~Cstr_socket()
        {
            termina();
        }
        //funciones-----------------------
        //devuelve -1 si no se ha podido crear socket
        //0 si no se puede conectar
        //1 si conectado
       public bool conecta(string ip, int puerto, string ip_local="")
       {
           termina();
           try
           {
               sc = str_socket_crea();
           }
           catch
           {
               sc = IntPtr.Zero;
           }
           if (sc == IntPtr.Zero)
            return false;
           try
           {
               return str_socket_conecta(sc, ip, puerto, ip_local) != 0;
           }
           catch
           {
               termina();
               return false;
           }
       }

       public bool envia(string txt)
       {
           if (sc == IntPtr.Zero)
               return false;
           try
           {
               return 0 != str_socket_envia(sc, txt);
           }
           catch
           {
               termina();
               return false;
           }
       }
       public string recibe()
       {
          
           int nb = -2;
           if (sc == IntPtr.Zero)
               return "";
           try
           {
               nb = str_socket_recive(sc);
               if(nb<=0)//error
                   return "";
               StringBuilder data = new StringBuilder("", nb);
               str_socket_dame_buf(sc, data);
               return data.ToString();

           }
           catch
           {
               termina();
           }
           return "";
       }
       public string dame_error()
       {
           if (sc == IntPtr.Zero)
               return "Socket nulo";
           try
           {
                StringBuilder data = new StringBuilder("Sin errores", 1024);
               str_socket_dame_error(sc,data);
               return data.ToString();
           }
           catch
           {
               termina();
           }
           return "Error en el socket de la dll utiles.dll";
       }
       public void termina()
       {
           if (sc != IntPtr.Zero)
           {
               try
               {
                   str_socket_borra(sc);
               }
               catch
               {
                   sc = IntPtr.Zero;
               }
               sc = IntPtr.Zero;
           }
       }
        //funciones auxiliares------------------------------
       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern IntPtr str_socket_crea();

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern int str_socket_conecta(IntPtr soc, String ip, int puerto, String ip_local=null);

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern int str_socket_envia(IntPtr soc, String txt);//solo este

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern int str_socket_recive(IntPtr soc);

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern void str_socket_dame_buf(IntPtr soc, StringBuilder buf);

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern void str_socket_dame_error(IntPtr soc, StringBuilder error);//llamar con 1024 de espacio

       [DllImport("utiles.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
       unsafe public static extern void str_socket_borra(IntPtr soc);  
    }
}
