Пример #1
0
        public void StartListening()
        {
            var    ipPoint      = new IPEndPoint(IPAddress.Parse(ipAddress), port);
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket handler      = null;

            try
            {
                listenSocket.Bind(ipPoint);
                listenSocket.Listen(10);
                logger.Log(SocketServiceMessage.socketStartListening);

                while (true)
                {
                    handler = listenSocket.Accept();
                    var    builder = new StringBuilder();
                    int    bytes   = 0;
                    byte[] data    = new byte[256];

                    do
                    {
                        bytes = handler.Receive(data);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (handler.Available > 0);

                    string matrixName = builder.ToString();
                    logger.Log(matrixName);
                    var result = new OperationResult <string>();

                    if (MatrixExistanceChecker.Check(matrixName, matricesData, out string matrixData))
                    {
                        result.Status = OperationStatus.Ok;
                        result.Data   = matrixData;
                    }
                    else
                    {
                        result.Status  = OperationStatus.Error;
                        result.Message = Errors.WrongMatrixNameError;
                    }

                    string json = JsonSerializer.Serialize(result);
                    data = Encoding.Unicode.GetBytes(json);
                    handler.Send(data);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                handler?.Shutdown(SocketShutdown.Both);
                listenSocket.Close();
                logger.Error(SocketServiceMessage.socketReloading);
                StartListening();
            }
        }
        public void matrixDoesNotExists(string matrixName)
        {
            var matricesData = new List <Matrix>()
            {
                new Matrix("Neo", "0101010010100101010"),
                new Matrix("Morphious", "0000000000110")
            };

            bool doesExist = MatrixExistanceChecker.Check(matrixName, matricesData, out string matrixData);

            Assert.IsTrue(!doesExist && matrixData == null);
        }
        public void matrixExists(string matrixName)
        {
            var matricesData = new List <Matrix>()
            {
                new Matrix("maytrix", "[[1]]"),
                new Matrix("matrix", "[[0],[0]]")
            };

            bool doesExist = MatrixExistanceChecker.Check(matrixName, matricesData, out string matrixData);

            Assert.IsTrue(doesExist && matrixData != null);
        }