Ejemplo n.º 1
0
        // server callbacks ////////////////////////////////////////////////////
        void OnLibuvServerConnected(TcpStream handle, Exception error)
        {
            // setup callbacks for the new connection
            handle.onMessage = OnLibuvServerMessage;
            handle.onError   = OnLibuvServerError;
            handle.onClosed  = OnLibuvServerClosed;

            // close if errors (AFTER setting up onClosed callback!)
            if (error != null)
            {
                Debug.Log($"libuv sv: client connection failed {error}");
                handle.Dispose();
                return;
            }

            // assign a connectionId via UserToken.
            // this is better than using handle.InternalHandle.ToInt32() because
            // the InternalHandle isn't available in OnLibuvClosed anymore.
            handle.UserToken = nextConnectionId++;
            connections[(int)handle.UserToken] = handle;

            Debug.Log("libuv sv: client connected with connectionId=" + (int)handle.UserToken);

            // dotsnet event
            OnServerConnected.Invoke((int)handle.UserToken);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Callback when connection has closed.
        /// </summary>
        /// <param name="handle">The stream handle in which closed connection to.</param>
        private void OnLibuvClientClosed(TcpStream handle)
        {
            Libuv2kNGLogger.Log("libuv client callback: closed connection");

            handle.Dispose();

            // set client to null so we can't send to an old reference anymore
            _client = null;
        }
Ejemplo n.º 3
0
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    ssl?.Dispose();
                    transport.Dispose();
                }

                base.Dispose(disposing);
            }
Ejemplo n.º 4
0
 public override void ServerStop()
 {
     if (server != null)
     {
         server.Dispose();
         server = null;
         connections.Clear();
         Debug.Log("libuv sv: TCP stopped!");
     }
 }
Ejemplo n.º 5
0
 private void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     _timer?.Dispose();
     TcpClient?.Dispose();
     TcpStream?.Dispose();
     Disconnect();
 }
Ejemplo n.º 6
0
        // OnClosed is called after we closed the stream
        void OnLibuvClientClosed(TcpStream handle)
        {
            Debug.Log("libuv cl: closed connection");
            handle.Dispose();

            // set client to null so we can't send to an old reference anymore
            client = null;

            // Mirror event
            OnClientDisconnected.Invoke();
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Shut down the server.
        /// </summary>
        public void Shutdown()
        {
            if (_server != null)
            {
                _cancellationToken.Cancel();
                _server?.Dispose();
                _server = null;

                Libuv2kNGLogger.Log("libuv server: TCP stopped!");

                _serverLoop?.Dispose();
            }
        }
Ejemplo n.º 8
0
        void OnLibuvServerClosed(TcpStream handle)
        {
            Debug.Log($"libuv sv: closed client {handle}");

            // important: remove the connection BEFORE calling the DOTSNET event
            // otherwise DOTSNET OnDisconnected Unspawn might try to send to a
            // disposed connection which we didn't remove yet. do it first.
            connections.Remove((int)handle.UserToken);
            handle.Dispose();

            // Mirror event
            OnServerDisconnected.Invoke((int)handle.UserToken);
        }
Ejemplo n.º 9
0
        // client callbacks ////////////////////////////////////////////////////
        void OnLibuvClientConnected(TcpStream handle, Exception exception)
        {
            // close if errors (AFTER setting up onClosed callback!)
            if (exception != null)
            {
                Debug.Log($"libuv cl: client error {exception}");
                handle.Dispose();
                return;
            }

            // Mirror event
            OnClientConnected.Invoke();

            Debug.Log($"libuv cl: client connected.");
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Queue up connections that are trying to connect.
        /// </summary>
        /// <param name="handle">The stream which is trying to connect to server.</param>
        /// <param name="error"></param>
        private void OnLibuvServerConnected(TcpStream handle, Exception error)
        {
            Libuv2kNGLogger.Log("libuv server: client connected =" + handle.GetPeerEndPoint().Address);

            // close if errors (AFTER setting up onClosed callback!)
            if (error != null)
            {
                Libuv2kNGLogger.Log($"libuv server: client connection failed {error}");

                handle.Dispose();

                return;
            }

            var newClient = new Libuv2kConnection(true, handle);

            _transport.Connected.Invoke(newClient);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Receive call back when we finally connect to a server.
        /// </summary>
        /// <param name="handle">The stream handle we used to connect with server with.</param>
        /// <param name="error">If there were any errors during connection.</param>
        private void ConnectedAction(TcpStream handle, Exception error)
        {
            handle.onMessage = OnLibuvClientMessage;
            handle.onError   = OnLibuvClientError;
            handle.onClosed  = OnLibuvClientClosed;

            // close if errors (AFTER setting up onClosed callback!)
            if (error != null)
            {
                Libuv2kNGLogger.Log($"libuv client callback: client error {error}", LogType.Error);

                handle.Dispose();

                return;
            }

            _connectedComplete.TrySetResult();
        }
Ejemplo n.º 12
0
        /// <summary>
        ///     Disconnect this connection
        /// </summary>
        public void Disconnect()
        {
            Libuv2kNGLogger.Log("libuv client: closed connection");

            _cancellationToken.Cancel();
            _connectedComplete?.TrySetCanceled();
            _client?.Dispose();

            while (_queuedIncomingData.TryDequeue(out _))
            {
                // do nothing
            }

            while (_queueOutgoingData.TryDequeue(out _))
            {
                // do nothing
            }

            _clientLoop?.Dispose();
        }
Ejemplo n.º 13
0
        public void OnTcpStream(object sender, TcpStream stream, bool isNew, ConcurrentQueue <object> queue)
        {
            if (isNew)
            {
                hay++;
            }
            if (stream == null || stream.Count == 0)
            {
                return;
            }

            if (stream.Variables == null)
            {
                stream.Variables       = new ExpandoObject();
                stream.Variables.Valid = new bool[] { true, true, true };
            }

            // Check
            bool some = false;

            try
            {
                for (int x = stream.Variables.Valid.Length - 1; x >= 0; x--)
                {
                    if (!stream.Variables.Valid[x])
                    {
                        continue;
                    }

                    object[] cred;
                    switch (_Checks[x].GetObjects(stream, out cred))
                    {
                    case EExtractorReturn.DontRetry:
                    {
                        stream.Variables.Valid[x] = false;
                        break;
                    }

                    case EExtractorReturn.Retry: some = true; break;

                    case EExtractorReturn.True:
                    {
                        stream.Dispose();

                        foreach (object c in cred)
                        {
                            queue.Enqueue(c);
                        }

                        return;
                    }
                    }
                }
            }
            catch (Exception e)
            {
                WriteError(e.ToString());
            }

            if (!some)
            {
                stream.Dispose();
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Callback for any errors that occur on the connection.
        /// </summary>
        /// <param name="handle">The stream handle we used to connect with server with.</param>
        /// <param name="error">The error that occurred on the connection.</param>
        private void OnLibuvClientError(TcpStream handle, Exception error)
        {
            Libuv2kNGLogger.Log($"libuv client callback: read error {error}", LogType.Error);

            handle.Dispose();
        }
Ejemplo n.º 15
0
 public override void ClientDisconnect()
 {
     client?.Dispose();
     client = null;
 }
Ejemplo n.º 16
0
        public void OnTcpStream(object sender, TcpStream stream, bool isNew, ConcurrentQueue <object> queue)
        {
            if (stream == null || stream.Count == 0)
            {
                return;
            }

            if (stream.Variables == null)
            {
                stream.Variables       = new ExpandoObject();
                stream.Variables.Valid = new bool[] { true, true, true };
            }

            // Check
            bool some = false;

            try
            {
                for (int x = stream.Variables.Valid.Length - 1; x >= 0; x--)
                {
                    if (!stream.Variables.Valid[x])
                    {
                        continue;
                    }

                    Credential[] cred;
                    switch (_Checks[x].GetCredentials(stream, out cred))
                    {
                    case EExtractorReturn.DontRetry:
                    {
                        stream.Variables.Valid[x] = false;
                        break;
                    }

                    case EExtractorReturn.Retry: some = true; break;

                    case EExtractorReturn.True:
                    {
                        stream.Dispose();

                        foreach (Credential c in cred)
                        {
                            // Prevent reiteration
                            string json = c.ToString(), last;
                            if (_LastCred.TryGetValue(c.Type, out last) && last == json)
                            {
                                continue;
                            }

                            _LastCred[c.Type] = json;
                            queue.Enqueue(c);
                        }

                        return;
                    }
                    }
                }
            }
            catch (Exception e)
            {
                WriteError(e.ToString());
            }

            if (!some)
            {
                stream.Dispose();
            }
        }