예제 #1
0
 private bool ServerDisconnected()
 {
     _client.Dispose();
     _client            = null;
     _isServerConnected = false;
     return(true);
 }
예제 #2
0
        private void InternetListen()
        {
            while (true)
            {
                if (!_flagOfThreadsBreak)
                {
                    try
                    {
                        bool status = _client != null && _client.Send(new byte[] { });

                        if (!status)
                        {
                            if (_client != null)
                            {
                                _client.Dispose();
                                _client = null;
                            }
                            _isServerConnected = false;
                        }
                    }
                    catch
                    {
                        if (_client != null)
                        {
                            _client.Dispose();
                            _client = null;
                        }
                        _isServerConnected = false;
                    }
                }

                Thread.Sleep(1000);
            }
        }
예제 #3
0
        private void StartClient()
        {
            try
            {
                _client = new Tcp_Client(ServerIpAddress.ToString(), ServerPort, ServerConnected, ServerDisconnected, MessageReceived, false);

                _isServerConnected = true;

                MessageModel message = new MessageModel();
                message.Type    = Type.ClientLogon;
                message.Message = string.Empty;
                message.PcName  = Environment.MachineName;

                string json = JsonConvert.SerializeObject(message);

                if (_key == null)
                {
                    _client.Send(Encoding.UTF8.GetBytes(json));
                }
                else
                {
                    _client.Send(ExtensionMethods.Encrypt(json, _key));
                }
            }
            catch
            {
                _isServerConnected = false;
                DisconnectedServer?.Invoke("No server connection");
            }
        }
예제 #4
0
        private bool MessageReceived(byte[] data)
        {
            string receiveData = string.Empty;

            if (_key == null)
            {
                receiveData = Encoding.UTF8.GetString(data);
            }
            else
            {
                receiveData = Encoding.UTF8.GetString(ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key));
            }


            var message = JsonConvert.DeserializeObject <MessageModel>(receiveData);

            if (message == null)
            {
                return(true);
            }

            LocalInfo info;

            info.ipAddress     = _client.LocalAddress;
            info.localEndPoint = Convert.ToInt32(_client.LocalPort);
            info.pcName        = Environment.MachineName;

            if (message.Type == Type.AcceptLogon)
            {
                _isServerConnected = true;
                ConnectedServer?.Invoke(info);
            }

            if (message.Type == Type.Disconnect)
            {
                _client.Dispose();
                _client             = null;
                _flagOfThreadsBreak = true;
                _isServerConnected  = false;
                DisconnectedServer?.Invoke("Connection disconnected by Server");
            }

            if (message.Type == Type.Blocked)
            {
                _client.Dispose();
                _client            = null;
                _isServerConnected = false;
                DisconnectedServer?.Invoke("It does not connect to the server because it is a blocked IP.");
            }

            if (message.Type == Type.Message)
            {
                ReceivedMessageFromServer?.Invoke(message.Message);
            }

            return(true);
        }
예제 #5
0
 public void DisconnectServer()
 {
     _flagOfThreadsBreak = true;
     if (_client != null)
     {
         _client.Dispose();
         _client            = null;
         _isServerConnected = false;
         DisconnectedServer?.Invoke("No server connection.");
     }
 }