private void AfterConnect(Task task)
 {
     if (task.IsCompleted)
     {
         if (_ClientWs.State == WebSocketState.Open)
         {
             Task.Run(() =>
             {
                 _Connected = true;
                 Task.Run(() => DataReceiver(), _Token);
                 ServerConnected?.Invoke(this, EventArgs.Empty);
             }, _Token);
         }
         else
         {
             _Connected = false;
             ServerDisconnected?.Invoke(this, EventArgs.Empty);
         }
     }
     else
     {
         _Connected = false;
         ServerDisconnected?.Invoke(this, EventArgs.Empty);
     }
 }
Ejemplo n.º 2
0
 private void AfterConnect(Task task)
 {
     if (task.IsCompleted)
     {
         if (_ClientWs.State == WebSocketState.Open)
         {
             Task.Run(() =>
             {
                 Task.Run(() => DataReceiver(), _Token);
                 ServerConnected?.Invoke(this, EventArgs.Empty);
             }, _Token);
         }
         else
         {
             if (DisconnectEventConnecting)
             {
                 ServerDisconnected?.Invoke(this, EventArgs.Empty);
                 InternalServerDisconnected?.Invoke(this, EventArgs.Empty);
             }
         }
     }
     else
     {
         if (DisconnectEventConnecting)
         {
             ServerDisconnected?.Invoke(this, EventArgs.Empty);
             InternalServerDisconnected?.Invoke(this, EventArgs.Empty);
         }
     }
 }
Ejemplo n.º 3
0
 private void OnConnect(Server server)
 {
     Log("Connected to " + Host + ":" + Port);
     client.Mutex.WaitOne();
     ServerConnected?.Invoke(server);
     server.SendMessage("{ 'getProperties': { 'version': 512 } }");
     client.Mutex.ReleaseMutex();
 }
Ejemplo n.º 4
0
        private void ConnAcceptedEvent(Socket client)
        {
            SocketClientEventArgs args = new SocketClientEventArgs();

            args.socket = client;
            ServerConnected?.Invoke(this, args);

            ListeningForServerMessage(client);
        }
Ejemplo n.º 5
0
 private void Connect()
 {
     if (IntegratedSecurity || !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
     {
         try
         {
             ServerConnected?.Invoke(this, GetDatabaseList(false));
         }
         catch
         {
             MessageBox.Show("Invalid login.");
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Asynchronouses the listener.
        /// </summary>
        /// <param name="ar">The ar.</param>
        /// <exception cref="NetProxyException">There was a problem connecting to the server.</exception>
        private void AsyncListener(IAsyncResult ar)
        {
            try
            {
                listen = (TcpListener)ar.AsyncState;
                client = listen.EndAcceptTcpClient(ar);

                client.GetStream().BeginRead(clientBuffer, 0, clientBuffer.Length, new AsyncCallback(GetClientData), client);
                ClientConnected?.Invoke();
                //  listen.Stop();
                LightBringer.GetStream().BeginRead(serverBuffer, 0, serverBuffer.Length, new AsyncCallback(GetServerData), LightBringer);
                ServerConnected?.Invoke();
            }
            catch (Exception e)
            {
                SendError(e, this);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///    Connects the specified ip.
        /// </summary>
        /// <param name="ip">The ip.</param>
        /// <param name="port">The port.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public virtual bool Connect(string ip = null, int port = 0)
        {
            Port      = port;
            IpAddress = ip;

            if (string.IsNullOrEmpty(ip))
            {
                IpAddress = Globals.IP;
            }

            if (port == 0)
            {
                Port = Globals.Port;
            }

            var ok = CreateSender(IpAddress, Port);

            if (!ok)
            {
                if (Globals.Debug)
                {
                    Console.WriteLine(Resources.Failed_to_connect_to + " " + IpAddress + " , " +
                                      Resources.port_number + " = " + Port);
                }

                return(false);
            }

            fListenThread = new Thread(ListenForData);
            fListenThread.Start();

            if (Globals.Debug)
            {
                Console.WriteLine(Resources.Connected_to + " " + IpAddress + " , " +
                                  Resources.port_number + " = " + Port);
            }

            if (ServerConnected != null)
            {
                ServerConnected.Invoke();
            }

            return(true);
        }
Ejemplo n.º 8
0
 protected override void ProcessMessage(IpcMessage message)
 {
     if (message is NetIpcLogMessage logMsg)
     {
         HandleLogMessage(logMsg);
     }
     else if (message.MessageType == IpcMessageType.NetIpcClientDisconnected)
     {
         ServerDisconnected?.Invoke(this, null);
     }
     else if (message.MessageType == IpcMessageType.NetIpcClientConnected)
     {
         ServerConnected?.Invoke(this, null);
     }
     else if (message is NetIpcAutoReconnectResponseMessage autoMsg)
     {
         HandleAutoReconnectStateMessage(autoMsg);
     }
 }
Ejemplo n.º 9
0
        public void Host(int port = 5000)
        {
            Console.WriteLine("Server Ready!");
            NetworkedMultiplayerENet peer = new();
            Error error = peer.CreateServer(port);

            if (error != Error.Ok)
            {
                GD.PushError(error.ToString());
            }

            NetworkPeer = peer;
            Console.WriteLine(NetworkPeer.GetConnectionStatus());
            if (NetworkPeer.GetConnectionStatus() == NetworkedMultiplayerPeer.ConnectionStatus.Connected)
            {
                Console.WriteLine("Server Is Live");
                ServerConnected?.Invoke(this, new ServerConnected());
            }
        }
Ejemplo n.º 10
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                // Complete the connection.
                client.EndConnect(ar);

                //Signal Connection complete
                ServerConnected?.Invoke();

                // Begin receiving the data from the remote device.
                client.BeginReceive(player.buffer, 0, ClientPlayer.BufferSize, 0, new AsyncCallback(ReadCallback), player);
            }
            catch (Exception e)
            {
                Logger.Debug(e.ToString());

                ServerFailedToConnect?.Invoke();
            }
        }
Ejemplo n.º 11
0
 private void MeshClientServerConnected(object sender, EventArgs args)
 {
     Logger?.Invoke("[MeshClient] Server " + PeerNode.IpPort + " connected");
     ServerConnected?.Invoke(this, new ServerConnectionEventArgs(PeerNode));
 }
Ejemplo n.º 12
0
 protected virtual void OnServerConnected()
 {
     ServerConnected?.Invoke(this, EventArgs.Empty);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Raises the event <see cref="ServerConnected"/>.
        /// </summary>
        protected internal void RaiseServerConnected()
        {
            _log.Debug(nameof(RaiseServerConnected));

            ServerConnected?.Invoke(this, EventArgs.Empty);
        }
Ejemplo n.º 14
0
 internal void HandleServerConnected(object sender, ConnectionEventArgs args)
 {
     WrappedEventHandler(() => ServerConnected?.Invoke(sender, args), "ServerConnected", sender);
 }
Ejemplo n.º 15
0
 internal void HandleServerConnected(object sender, EventArgs args)
 {
     ServerConnected?.Invoke(sender, args);
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Start the client and establish a connection to the server.
        /// </summary>
        /// <returns></returns>
        public Task StartAsync()
        {
            _Client = new TcpClient();
            _Stats  = new Statistics();
            IAsyncResult asyncResult    = null;
            WaitHandle   waitHandle     = null;
            bool         connectSuccess = false;

            if (_Mode == Mode.Tcp)
            {
                #region TCP

                Logger?.Invoke("[WatsonTcpClient] Connecting to " + _ServerIp + ":" + _ServerPort);

                _Client.LingerState = new LingerOption(true, 0);
                asyncResult         = _Client.BeginConnect(_ServerIp, _ServerPort, null, null);
                waitHandle          = asyncResult.AsyncWaitHandle;

                try
                {
                    connectSuccess = waitHandle.WaitOne(TimeSpan.FromSeconds(_ConnectTimeoutSeconds), false);
                    if (!connectSuccess)
                    {
                        _Client.Close();
                        throw new TimeoutException("Timeout connecting to " + _ServerIp + ":" + _ServerPort);
                    }

                    _Client.EndConnect(asyncResult);

                    _SourceIp   = ((IPEndPoint)_Client.Client.LocalEndPoint).Address.ToString();
                    _SourcePort = ((IPEndPoint)_Client.Client.LocalEndPoint).Port;
                    _TcpStream  = _Client.GetStream();
                    _SslStream  = null;

                    Connected = true;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    waitHandle.Close();
                }

                #endregion TCP
            }
            else if (_Mode == Mode.Ssl)
            {
                #region SSL

                Logger?.Invoke("[WatsonTcpClient] Connecting with SSL to " + _ServerIp + ":" + _ServerPort);

                _Client.LingerState = new LingerOption(true, 0);
                asyncResult         = _Client.BeginConnect(_ServerIp, _ServerPort, null, null);
                waitHandle          = asyncResult.AsyncWaitHandle;

                try
                {
                    connectSuccess = waitHandle.WaitOne(TimeSpan.FromSeconds(_ConnectTimeoutSeconds), false);
                    if (!connectSuccess)
                    {
                        _Client.Close();
                        throw new TimeoutException("Timeout connecting to " + _ServerIp + ":" + _ServerPort);
                    }

                    _Client.EndConnect(asyncResult);

                    _SourceIp   = ((IPEndPoint)_Client.Client.LocalEndPoint).Address.ToString();
                    _SourcePort = ((IPEndPoint)_Client.Client.LocalEndPoint).Port;

                    if (AcceptInvalidCertificates)
                    {
                        // accept invalid certs
                        _SslStream = new SslStream(_Client.GetStream(), false, new RemoteCertificateValidationCallback(AcceptCertificate));
                    }
                    else
                    {
                        // do not accept invalid SSL certificates
                        _SslStream = new SslStream(_Client.GetStream(), false);
                    }

                    _SslStream.AuthenticateAsClient(_ServerIp, _SslCertificateCollection, SslProtocols.Tls12, !AcceptInvalidCertificates);

                    if (!_SslStream.IsEncrypted)
                    {
                        throw new AuthenticationException("Stream is not encrypted");
                    }

                    if (!_SslStream.IsAuthenticated)
                    {
                        throw new AuthenticationException("Stream is not authenticated");
                    }

                    if (MutuallyAuthenticate && !_SslStream.IsMutuallyAuthenticated)
                    {
                        throw new AuthenticationException("Mutual authentication failed");
                    }

                    Connected = true;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    waitHandle.Close();
                }

                #endregion SSL
            }
            else
            {
                throw new ArgumentException("Unknown mode: " + _Mode.ToString());
            }

            ServerConnected?.Invoke(this, EventArgs.Empty);
            return(DataReceiver());
        }
Ejemplo n.º 17
0
 private void RaiseServerConnected()
 {
     ServerConnected?.Invoke(this, new TcpServerConnectedEventArgs(RemoteEndPoint, LocalEndPoint));
 }
 public void OnServerConnected()
 {
     IsConnected = true;
     ServerConnected?.Invoke();
     StartHeartBeat();
 }
Ejemplo n.º 19
0
 /// <summary>
 /// 连接成功触发方法
 /// </summary>
 /// <param name="ipAddresses"></param>
 /// <param name="port"></param>
 private void RaiseServerConnected(IPAddress[] ipAddresses, int port)
 {
     ServerConnected?.Invoke(this, new TcpServerConnectedEventArgs(ipAddresses, port));
 }
Ejemplo n.º 20
0
 internal void HandleServerConnected(object sender, EventArgs args)
 {
     CatchAndReport(() => ServerConnected?.Invoke(sender, args), "ServerConnected", sender);
 }
Ejemplo n.º 21
0
 internal void HandleAuthenticationSucceeded(object sender, EventArgs args)
 {
     WrappedEventHandler(() => ServerConnected?.Invoke(sender, args), "ServerConnected", sender);
 }
Ejemplo n.º 22
0
 internal virtual void OnServerConnected(EventArgs e) => ServerConnected?.Invoke(this, e);
Ejemplo n.º 23
0
 private void OnSererConnected()
 {
     ServerConnected?.Invoke();
 }
Ejemplo n.º 24
0
 protected virtual void OnServerConnected()
 {
     ServerConnected?.Invoke(this, new EventArgs());
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Trigger ServerConnected Event
 /// </summary>
 /// <param name="ipAddress"></param>
 /// <param name="port"></param>
 protected virtual void OnServerConnected(IPAddress[] ipAddress, int port)
 {
     ServerConnected?.Invoke(this, new TCPServerConnectedEventArgs(ipAddress[0], port));
 }