Ejemplo n.º 1
0
 private void InitEvents()
 {
     _listener = new EventBasedNetListener();
     _listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => {
         _peer.HandleDataReceived(dataReader.GetRemainingBytes(), 0);
         dataReader.Recycle();
     };
     _listener.PeerConnectedEvent += (peer) => {
         Status = EConnectionStatus.Connected;
         var pe = new PeerLn(peer);
         pe.SetConnectedState(true);
         pe.MessageReceived += HandleMessage;
         _peer = pe;
         Peer  = pe;
         Connected?.Invoke();
     };
     _listener.PeerDisconnectedEvent += (peer, disconnectInfo) => {
         if (Peer != null)
         {
             Peer.MessageReceived -= HandleMessage;
         }
         _peer?.SetConnectedState(false);
         Status = EConnectionStatus.Disconnected;
         Disconnected?.Invoke();
     };
     _netManager = new NetManager(_listener)
     {
         DisconnectTimeout = 300000
     };
     _netManager.Start();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts connecting to another socket
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="timeoutMillis"></param>
        /// <returns></returns>
        public IClientSocket Connect(string ip, int port, int timeoutMillis, string key = "")
        {
            _ip   = ip;
            _port = port;


            _isConnected = false;
            Status       = EConnectionStatus.Connecting;

            if (_peer != null)
            {
                _peer.MessageReceived -= HandleMessage;
                _peer.Dispose();
            }

            _listener = new EventBasedNetListener();
            _listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => {
                _peer.HandleDataReceived(dataReader.GetRemainingBytes(), 0);
                dataReader.Recycle();
            };
            _listener.PeerConnectedEvent += (peer) => {
                _isConnected = false;
                var pe = new PeerLn(peer);
                pe.SetConnectedState(true);
                pe.MessageReceived += HandleMessage;
                _peer = pe;
                Peer  = pe;
                Connected?.Invoke();
            };
            _listener.PeerDisconnectedEvent += (peer, disconnectInfo) => {
                _peer?.SetConnectedState(false);
                _isConnected = false;
                Disconnected?.Invoke();
            };
            _netManager = new NetManager(_listener)
            {
                DisconnectTimeout = 300000
            };
            _netManager.Start();
            _netManager.Connect(_ip, _port, key);
            return(this);
        }