Пример #1
0
        private void AcceptCallback(IAsyncResult ar)
        {
            if (ForcedTermination)
            {
                return;
            }

            var    server = (Socket)ar.AsyncState;
            Socket client;
            // int Id = ID;
            // bool exit = false;
            string ipadd = "";

            try {
                client = server.EndAccept(ar);
                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                // server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger , true);
                server.BeginAccept(new AsyncCallback(AcceptCallback), server);
                ConnectionSuccessfull?.Invoke(this, new ConnectionSuccessfullArgs(client.RemoteEndPoint.ToString()));
                ConnectingList.Add(client.RemoteEndPoint.ToString(), client);
                ConnectedCount++;
            } catch {
                return;
            }

            ipadd = client.RemoteEndPoint.ToString();
            while (true)
            {
                using (MemoryStream ms = new System.IO.MemoryStream()) {
                    byte[] resBytes = new byte[255];

                    int resSize = 0;
                    try {
                        do
                        {
                            resSize = client.Receive(resBytes);
                            if (resSize == 0)
                            {
                                break;
                            }
                            ms.Write(resBytes, 0, resSize);
                        } while (resBytes[resSize - 1] != '\n');

                        string resMsg = Encoding.UTF8.GetString(ms.ToArray()).Trim('\r', '\n');
                        if (resMsg != "")
                        {
                            MessageReceived?.Invoke(this, new MessageReceivedArgs(ipadd, resMsg));
                        }
                    } catch (Exception e) {
                        Console.WriteLine(e);
                        break;
                    }
                }
            }
        }
Пример #2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            if (ForcedTermination)
            {
                return;
            }

            var    server = (Socket)ar.AsyncState;
            Socket client;
            // int Id = ID;
            // bool exit = false;
            string ipadd = "";

            try {
                client = server.EndAccept(ar);
                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                // server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger , true);
                server.BeginAccept(new AsyncCallback(AcceptCallback), server);
                ConnectionSuccessfull?.Invoke(this, new ConnectionSuccessfullArgs(client.RemoteEndPoint.ToString()));
                ConnectingList.Add(client.RemoteEndPoint.ToString(), client);
                ConnectedCount++;
            } catch {
                return;
            }

            ipadd = client.RemoteEndPoint.ToString();
            var sw = new Stopwatch();

            sw.Start();
            while (sw.ElapsedMilliseconds < Timeout)
            {
                using (MemoryStream ms = new System.IO.MemoryStream()) {
                    byte[] resBytes = new byte[255];
                    int    resSize  = 0;
                    try {
                        do
                        {
                            resSize = client.Receive(resBytes);
                            if (resSize == 0 && ms.Length == 0)
                            {
                                continue;
                            }

                            ms.Write(resBytes, 0, resSize);
                            // Debug.WriteLine(string.Join(" ", resBytes.Select(s => $"{s}")));
                            Debug.Write($"{Encoding.UTF8.GetString(resBytes)} |=-=| ");
                            sw.Restart();
                        } while (resBytes[resSize - 1] != '\n');

                        var resData = resBytes.ToArray();
                        DataReceived?.Invoke(this, new DataReceivedArgs(ipadd, resData));
                        var resMsg = Encoding.UTF8.GetString(ms.ToArray())
                                     .Trim('\r', '\n');
                        Debug.WriteLine($"{Environment.NewLine}------------------------       response fin.      ---------------------------");
                        // Debug.WriteLine($"{resMsg}{Environment.NewLine}------------------------------------------------");
                        MessageReceived?.Invoke(this, new MessageReceivedArgs(ipadd, resMsg));
                    } catch (Exception e) {
                        Console.WriteLine(e);
                        break;
                    }
                }
            }

            sw.Stop();
            Disconnect(ipadd);
        }
Пример #3
0
        private async void ButtonLoginFormEnter_Click(object sender, EventArgs e)
        {
            try
            {
                var isPinCorrect = false;

                if (_isPin)
                {
                    _pin         = textBoxLoginFormPassword.Text;
                    _user        = textBoxLoginFormUser.Text;
                    isPinCorrect = CheckPin();

                    if (isPinCorrect)
                    {
                        var sm = CredentialManager.GetCredentials($"Password_{_user}");
                        if (sm == null)
                        {
                            _msg.Status = Status.Warning;
                            _msg.Head   = "Pin cod was not found";
                            _msg.Text   = "You try to use pin code, but it wasn't found. Try to add it before using.";
                            Report.Notify(_msg);
                            return;
                        }
                        _password = sm.Password;
                    }
                    else
                    {
                        _msg.Status = Status.Warning;
                        _msg.Head   = "Unsuccessful login";
                        _msg.Text   = "Wrong login or pin. In case of you don't remember your pin, you can save a new one.";
                        Report.Notify(_msg);
                        return;
                    }
                }
                _sqlcs.UserID   = _user;
                _sqlcs.Password = _password;

                RegataContext.ConString = _sqlcs.ConnectionString;

                using (var r = new RegataContext())
                {
                    if (await r.Database.CanConnectAsync(new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token))
                    {
                        ConnectionSuccessfull?.Invoke(_sqlcs);
                        Hide();
                    }
                    else
                    {
                        _msg.Status = Status.Warning;
                        _msg.Head   = "Unsuccessful login";
                        _msg.Text   = "Wrong login or pin. In case of you don't remember your pin, you can save a new one.";
                        Report.Notify(_msg);
                        return;
                    }
                }
            }
            catch (TaskCanceledException)
            {
                _msg.Status = Status.Warning;
                _msg.Head   = "Connection timeout";
                _msg.Text   = "Too long time for connection. Seems DB is not available";
                Report.Notify(_msg);
            }
            catch (Exception ex)
            {
                _msg.Status       = Status.Error;
                _msg.Head         = "Unregistred error in Regata login system";
                _msg.Text         = ex.Message;
                _msg.DetailedText = ex.ToString();
                Report.Notify(_msg);
            }
        }