Beispiel #1
0
        /// <inheritdoc/>
        /// <exception cref="TimeoutException"> Throws if operation takes longer than <see cref="Service.MUnitConfiguration.SendTimeout"/>. </exception>
        /// <exception cref="SocketException"> Re-throws exception thrown by underlying TCP socket. </exception>
        public virtual uint Send(WireMessage data)
        {
            ThrowUtilities.NullArgument(data, nameof(data));
            Debug.Assert(_handler != null, "_handler should not be null." + Environment.NewLine + Environment.StackTrace);

            data.ID = _messageID++;
            byte[] bytes = SerializeHelper.BinarySerialize(data, _eof);

            _handler.Send(bytes, 0, bytes.Length, SocketFlags.None, out SocketError socketError);

            if (socketError != SocketError.Success)
            {
                if (socketError == SocketError.TimedOut)
                {
                    throw new TimeoutException(string.Format(
                                                   CultureInfo.CurrentCulture,
                                                   Errors.UTE_TimeoutFormattableString,
                                                   "Send"));
                }
                else
                {
                    throw new SocketException((int)socketError);
                }
            }

            return(data.ID);
        }
Beispiel #2
0
        /// <inheritdoc/>
        protected override object ProcessActionCommand(WireMessage message)
        {
            ThrowUtilities.NullArgument(message, nameof(message));

            switch (message.Type)
            {
            case WireMessageTypes.Telemetry:
                switch (message.Command)
                {
                case CommandType.RecordTestStart:
                    this.RecordTestStartEvent?.Invoke(message.Entity as TestResult);
                    break;

                case CommandType.RecordTestEnd:
                    this.RecordTestEndEvent?.Invoke(message.Entity as TestResult);
                    break;

                case CommandType.DiscoverTests:
                    _discoverTestsLocks.HandlerLock.WaitOne();
                    this.DiscoverTestsEvent?.Invoke(message.SessionID, message.Entity as ICollection <ITestMethodContext>);
                    break;

                default:
                    _logger.RecordMessage(MessageLevel.Warning, string.Format(
                                              CultureInfo.CurrentCulture,
                                              Errors.UTE_UnsupportedCommandType,
                                              message.Command.ToString()));
                    break;
                }

                break;

            default:
                switch (message.Command)
                {
                case CommandType.TakeResults:
                    this.RecordTestResultEvent?.Invoke(message.Entity as TestResult);
                    break;

                case CommandType.CheckAssemblyHash:
                    this.CheckAssemblyHashEvent?.Invoke(message.SessionID, message.Entity as byte[]);
                    break;

                default:
                    _logger.RecordMessage(MessageLevel.Warning, string.Format(
                                              CultureInfo.CurrentCulture,
                                              Errors.UTE_UnsupportedCommandType,
                                              message.Command.ToString()));
                    break;
                }

                break;
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Receive byte transmission synchronously and deserialize them to <see cref="WireMessage"/>.
        /// </summary>
        /// <param name="handler"> Socket that handles data transmission. </param>
        /// <exception cref="SocketException"> Re-throw from <paramref name="handler"/>. </exception>
        protected void DeserializeMessage(Socket handler)
        {
            if (_stateObject == null)
            {
                _stateObject = new StateObject();
            }

            if (_stateObject.Stream.Length > _eof.Length)
            {
                byte[] buffer = _stateObject.Stream.ToArray();
                if (this.IsEOF(buffer, _stateObject.StartReadPos, buffer.Length, out int index))
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        stream.Write(buffer, 0, index + 1);
                        stream.Position = 0;
                        WireMessage wireMessage = SerializeHelper.BinaryRead <WireMessage>(stream);
                        this.MessageQueue.Enqueue(wireMessage);
                    }

                    _stateObject.ResetStream(buffer, index + _eof.Length + 1);
                    return;
                }
                else
                {
                    _stateObject.StartReadPos = buffer.Length;
                }
            }

            if (!_disposed && handler.Available > 0)
            {
                byte[] buffer   = new byte[TCPConstants.TCPBufferSize];
                int    byteRead = handler.Receive(
                    buffer,
                    0,
                    buffer.Length,
                    SocketFlags.None,
                    out SocketError socketError);
                _stateObject.Stream.Write(buffer, 0, byteRead);

                if (socketError != SocketError.Success)
                {
                    throw new SocketException((int)socketError);
                }
            }

            if (!handler.Connected)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }
        }
Beispiel #4
0
        /// <inheritdoc/>
        /// <exception cref="TimeoutException"> Throws if operation takes longer than <see cref="Service.MUnitConfiguration.SendTimeout"/>. </exception>
        /// <exception cref="SocketException"> Re-throws exception thrown by underlying TCP socket. </exception>
        public void SendAsync(WireMessage data)
        {
            ThrowUtilities.NullArgument(data, nameof(data));
            Debug.Assert(_handler != null, "_handler should not be null." + Environment.NewLine + Environment.StackTrace);

            if (!_handler.Connected)
            {
                _handler.Connect(_remoteEndPoint ?? _handler.RemoteEndPoint);
            }

            if (data.Type == WireMessageTypes.Request)
            {
                data.ID = _messageID++;
            }

            byte[] bytes = SerializeHelper.BinarySerialize(data, _eof);

            _handler.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, SendAsyncCallback, _handler);
        }
Beispiel #5
0
        private void DiscoverTests(WireMessage message)
        {
            ITestCycleGraph tests = _testEngine.DiscoverTests(message.Entity as IEnumerable <string>);

            this.Send(new WireMessage(WireMessageTypes.Telemetry, CommandType.DiscoverTests, tests.TestContextLookup.Values.ToList()), message.SessionID);
        }
Beispiel #6
0
        protected override object ProcessActionCommand(WireMessage message)
        {
            ThrowUtilities.NullArgument(message, nameof(message));

            try
            {
                switch (message.Command)
                {
                case CommandType.CallFunction:
                    if (message.CallingType != null)
                    {
                        object instance = message.CtorParams == null
                            ? Activator.CreateInstance(message.CallingType)
                            : Activator.CreateInstance(message.CallingType, message.CtorParams);

                        ((MethodInfo)message.Entity).Invoke(instance, message.Parameters);
                    }
                    else
                    {
                        ((MethodInfo)message.Entity).Invoke(null, message.Parameters);
                    }

                    break;

                case CommandType.DiscoverTests:
                    this.DiscoverTests(message);
                    break;

                case CommandType.RunTests:
                    switch (message.Entity)
                    {
                    case IEnumerable <string> sources:
                        _testEngine.RunTests(sources, message.SessionID, _logger);
                        break;

                    case IEnumerable <Guid> guids:
                        _testEngine.RunTests(guids, message.SessionID, _logger);
                        break;

                    default:
                        throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Errors.UTE_UnSupportedTestSources,
                                      message.Entity.GetType()));
                    }

                    break;

                case CommandType.Cancel:
                    _testEngine.Cancel();
                    break;

                case CommandType.CheckAssemblyHash:
                    this.Send(
                        new WireMessage(
                            WireMessageTypes.Reply,
                            CommandType.CheckAssemblyHash,
                            new Hash(Assembly.Load((string)message.Entity)).SHA1),
                        message.SessionID);
                    break;

                default:
                    _logger.RecordMessage(MessageLevel.Warning, string.Format(
                                              CultureInfo.CurrentCulture,
                                              Errors.UTE_UnsupportedCommandType,
                                              message.Command.ToString()));
                    break;
                }

                return(null);
            }
            catch (SocketException)
            {
                throw;
            }
            catch (Exception e)
            {
                _logger.RecordMessage(MessageLevel.Error, e.ToString());
                return(null);
            }
        }