using Google.Protobuf;
using ServerCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;

public class NetWorkManager
{
    ServerSession _session = new ServerSession();

    public void Send(IMessage packet)
    {
        _session.Send(packet);
    }

    public void Connect()
    {
        string host = Dns.GetHostName();
        IPHostEntry ipHost = Dns.GetHostEntry(host);
        IPAddress ipAddr = ipHost.AddressList[1];
        IPEndPoint endPoint = new IPEndPoint(ipAddr, 7777);

        Connector connector = new Connector();
        connector.Connect(endPoint,
            () => { return _session; }, 1);
    }

    public void Update()
    {
        List<PacketMessage> packets = PacketQueue.Instance.PopAll();

        foreach (PacketMessage pm in packets)
        {
            Action<PacketSession, IMessage> handler = PacketManager.Instance.GetPacketHandler(pm.Id);
            if (handler != null)
                handler.Invoke(_session, pm.Message);
        }
    }
}
