RevenantX / LiteNetLib

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

methods #69

Closed G4bit closed 7 years ago

G4bit commented 7 years ago

i dont know what i am doing wrong. but server doesnt get any call for OnNetworkReceive method or any other ones from INetEventListener, is these methods only used in client?

RevenantX commented 7 years ago

INetEventListener, is these methods only used in client?

No. Event listener used on client and server (NetManager) Can you show your code that doesn't works. And more information?

G4bit commented 7 years ago

i think i found problem, i didnt used poolEvents(); but now then i start server it doesnt show form ;[

server code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiteNetLib;
using LiteNetLib.Utils;

namespace MMoServerTest
{
    public partial class frmServer : Form,INetEventListener
    {
        static EventBasedNetListener listener = new EventBasedNetListener();
        static NetManager server = new NetManager(listener, 10,"TestGame");

        public frmServer()
        {
            InitializeComponent();
        }
        private void btnBroadcast_Click(object sender, EventArgs e)
        {
            NetDataWriter writer = new NetDataWriter();
            writer.Put("Ciulpkit bybius");
            server.SendToAll(writer,SendOptions.Unreliable);
        }
        private void frmServer_Load(object sender, EventArgs e)
        {
            StartServer();
        }

        private void StartServer()
        {
            if (server.Start(1350))
            {
                tbLog.AppendText("Server started! \n");
                lbStatus.Text = "Satus: Online";
            }
            else
            {
                tbLog.AppendText("Server failed to start! \n");
                lbStatus.Text = "Satus: Offline";
            }

            while (server.IsRunning)
            {
                server.PollEvents();
            }
        }
        private void tmrLoop_Tick(object sender, EventArgs e)
        {
            if(server.IsRunning)
            {
                lbCu.Text = "Connected: " + server.PeersCount;
            }
        }

        public void OnPeerConnected(NetPeer peer)
        {
            tbLog.AppendText("Wtf krw");
        }

        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            tbLog.AppendText("Wtf krw");
        }

        public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
        {
            tbLog.AppendText("Wtf krw");
        }

        public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
        {
            tbLog.AppendText("Wtf krw");
        }

        public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
        {
            tbLog.AppendText("Wtf krw");
        }

        public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
        {
            tbLog.AppendText("Wtf krw");
        }
    }
}
RevenantX commented 7 years ago
         while (server.IsRunning)
            {
                server.PollEvents();
            }

This is infinite loop.

G4bit commented 7 years ago

i know that i am retarded 🐶 but i still have no clue how to make it work

        static EventBasedNetListener listener = new EventBasedNetListener();
        static NetManager server;
        private Thread thread;
        private void StartServer()
        {
            server = new NetManager(listener, 10, "TestGame");
            server.DiscoveryEnabled = true;
            thread = new Thread(new ThreadStart(GetEvent));
            thread.Start();

            if (server.Start(1350))
            {
                tbLog.AppendText("Server started! \n");
                lbStatus.Text = "Satus: Online";
            }
            else
            {
                tbLog.AppendText("Server failed to start! \n");
                lbStatus.Text = "Satus: Offline";
            }
        }

        private void GetEvent()
        {
            while (server.IsRunning)
            {
                server.PollEvents();
                Thread.Sleep(15);
            }
        }
    it doesnt get any call for
    public void OnPeerConnected(NetPeer peer)
    {
        tbLog.AppendText("OnPeerConnected");
    }
G4bit commented 7 years ago

What a shame had to remade it on console to make it work propertly ;[

RevenantX commented 7 years ago

@G4bit it must work in UWP and Windows Forms. Can you send to me sources of your application that doesn't work? And client code too.

G4bit commented 7 years ago

Server files: ServerTest.zip

Client code single script for connecting: dont mind that createClients it was for testing purposes.

using UnityEngine;
using LiteNetLib;
using LiteNetLib.Utils;
using System.Collections.Generic;
public class Network : MonoBehaviour, INetEventListener
{
    private NetManager client;
    List<NetManager> clients = new List<NetManager>();

    void Start()
    {
        //createClients(100);
        client = new NetManager(this, "TestGame");
        client.Start();
        StartConnection();
    }

    private void createClients(int ct)
    {
        for (int i = 0; i < ct; i++)
        {
            clients.Add(new NetManager(this, "TestGame"));
            clients[i].Start();
            clients[i].Connect("localhost", 1350);
        }
    }

    public void closeClients()
    {
        for(int i = 0; i < clients.Count; i++)
        {
            clients[i].Stop();
        }
    }

    void StartConnection()
    {
        client.Connect("localhost",1350);
    }

    void Update()
    {
        client.PollEvents();
    }

    void OnDestroy()
    {
        if (client != null)
            client.Stop();
    }

    public void OnNetworkError(NetEndPoint endPoint, int socketErrorCode)
    {
        Debug.Log("OnNetworkError");
    }

    public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
    {
        Debug.Log("OnNetworkLatencyUpdate");
    }

    public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
    {
        Debug.Log("Received: "+reader.GetString());
    }

    public void OnNetworkReceiveUnconnected(NetEndPoint remoteEndPoint, NetDataReader reader, UnconnectedMessageType messageType)
    {
        Debug.Log("OnNetworkReceiveUnconnected");
    }

    public void OnPeerConnected(NetPeer peer)
    {
        Debug.Log("OnPeerConnected");
    }

    public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
    {
        Debug.Log("OnPeerDisconnected");
    }
}
RevenantX commented 7 years ago

@G4bit your thread closed immediately because "server.IsRunning" - will be true only after "server.Start". Start your thread after server start.

G4bit commented 7 years ago

i dont have much experience with visual studio but that thread is working, it can print in my log. but i have no idea how that server.PollEvents() work.

ps. i think it would be easier to just get from someone working simple server project solution . or stay on console

RevenantX commented 7 years ago

@G4bit "server.PollEvents" call all INetEventListener methods with data that arrived in another thread.

RevenantX commented 7 years ago

@G4bit just checked your code. Thread closed. Because server.IsRunning is false. Add log to while loop.

G4bit commented 7 years ago

i enabled cross thread. CheckForIllegalCrossThreadCalls = false;

        private void GetEvent()
        {
            while (true)
            {
                server.PollEvents();
                Thread.Sleep(15);
                tbLog.AppendText("THread working!!!");
            }
        }

server log: Server starter! ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!! ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!! ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!ThreadWorking!!!

RevenantX commented 7 years ago

@G4bit ah. i see. You using empty listener on server. Implement INetEventListener listener interface in your "Form" class and change this line: server = new NetManager(listener, 10, "TestGame"); to server = new NetManager(this, 10, "TestGame"); Or use EventBasedEventListener and hook up to delegates as in example at main page:

listener.NetworkReceiveEvent += (fromPeer, dataReader) =>
{
    Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */));
};
G4bit commented 7 years ago

That was embarrassing, such stupid mistake :D Works now!! thank you.

timematcher commented 7 years ago

@G4bit Btw, You should really not use CheckForIllegalCrossThreadCalls = false; in desktop apps. The correct way would be to declare a UI synchronization context object as a global variable in winforms app like this

public partial myclass(){
         public SynchronizationContext _context;
public myClass(){
          initializecomponents();
          _context= SynchronizationContext.Current;
       }
public void MyMethodThatIsMakingCrossThreaCallUsingTimerOrThread(){
{
        _context.Post(new SendOrPostCallback((o) => {
           //**Update Status in Console here**
           //OR
            //Update any field or status of Winforms UI control WPF UI control here.
            // and the app will not complain of Cross Thread Violation.
        }), null);
}
}

Another way is to use InvokeRequired. but i find the above method the easiest.