コード例 #1
0
        protected virtual void ProcessReceive(SocketAsyncEventArgs e)
        {
            // check if the remote host closed the connection
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                _socketMessageHandler.Write(e.Buffer, e.Offset, e.BytesTransferred);
                var count = _socketMessageHandler.TryRead(out var readBytes);

                if (count > 0)
                {
                    _socketMessageEventArgs.Message = readBytes;
                    _socketMessageEventArgs.Count   = count;

                    // inform that the socket just received complete message
                    MessageReceived.RaiseEvent(this, _socketMessageEventArgs);
                }

                // don't attempt another receive if socket is already disposed
                if (_disposed)
                {
                    return;
                }

                var willRaiseEvent = _clientSocket.ReceiveAsync(e);
                if (!willRaiseEvent)
                {
                    ProcessReceive(e);
                }
            }
        }
コード例 #2
0
        private void HttpPostServer_MessageReceived(object sender, RestResponse e)
        {
            try
            {
                if (e == null)
                {
                    return;
                }

                var tetraControlResponse = JsonConvert.DeserializeObject <TetraControlResponse>(e.Content);
                if (tetraControlResponse == null || tetraControlResponse.data == null)
                {
                    return;
                }

                Logger.WriteDebug(
                    $"{_decoderName}: {tetraControlResponse.data.srcSSI} -> {tetraControlResponse.message}");
                switch (tetraControlResponse.data.type)
                {
                case "status":
                    //Raise event
                    MessageReceived.RaiseEvent(this, new MessageEventArgs(tetraControlResponse.ToStatusString()));
                    break;

                case "pos":
                    MessageReceived.RaiseEvent(this, new MessageEventArgs(tetraControlResponse.ToPositionString()));
                    break;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteDebug($"{_decoderName}: {ex} -> {ex.Message}");
            }
        }
コード例 #3
0
        public CommunicatingApplicationHook(System.Diagnostics.Process targetProcess)
            : base(targetProcess)
        {
            remoteProcess = new Native.Process.RemoteProcess(targetProcess);
            IPC.NamedPipes.NamedPipeServer server = new IPC.NamedPipes.NamedPipeServer();

            PipeId = Guid.NewGuid().ToString();

            Thread t = new Thread(() =>
            {
                server.ConnectionRecieved += (s, e) =>
                {
                    connection = e.Value;
                    while (e.Value.IsConnected)
                    {
                        var msg = e.Value.Receive();
                        if (e.Value.IsConnected && msg != null)
                        {
                            MessageReceived.RaiseEvent(this, msg);
                        }
                    }
                    ConnectionClosed.RaiseEvent(this);
                };
                server.Start(new IPC.NamedPipes.NamedPipeInitialisation(PipeId));
            });

            t.Start();
        }
コード例 #4
0
        private async Task <bool> RaiseMessageReceivedAsync(Message envelope)
        {
            var eventArgs = new EnvelopeEventArgs <Message>(envelope);

            MessageReceived?.RaiseEvent(this, eventArgs);
            await eventArgs.WaitForDeferralsAsync().ConfigureAwait(false);

            return(true);
        }
コード例 #5
0
ファイル: MessageHook.cs プロジェクト: mkbiltek2019/StUtil
 protected override bool ProcessEvent(IntPtr wParam, IntPtr lParam)
 {
     NativeStructs.CWPSTRUCT msg = (NativeStructs.CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(NativeStructs.CWPSTRUCT));
     MessageReceived.RaiseEvent(this, new Message
     {
         HWnd   = msg.hwnd,
         LParam = msg.lParam,
         Msg    = msg.message,
         WParam = msg.wParam
     });
     return(false);
 }
コード例 #6
0
ファイル: IPCProcess.cs プロジェクト: mkbiltek2019/StUtil
 public void Start()
 {
     if (IsServer)
     {
         server.ConnectionRecieved += server_ConnectionRecieved;
         server.Start(initArgs);
     }
     else
     {
         conn = client.Connect(initArgs);
         while (conn.IsConnected)
         {
             MessageReceived.RaiseEvent(this, conn.Receive());
         }
         Environment.Exit(1);
     }
 }
コード例 #7
0
        private void ReceiveCallback(IAsyncResult _asyncResult)
        {
            try
            {
                //Stop receive timeout
                receiveTimeout.Stop();

                // Retrieve the state object and the client socket from the asynchronous state object.
                var _state = (StateObject)_asyncResult.AsyncState;
                if (_state == null || _state.Socket == null || decoderStatus == ConnectionStatus.Disconnected)
                {
                    return;
                }

                var _socketError = new SocketError();

                //Complete recive
                var _bytesRead = _state.Socket.EndReceive(_asyncResult, out _socketError);
                if (_bytesRead > 0)
                {
                    //Store the data received so far
                    _state.Data += decoderEncoding.GetString(_state.Buffer, 0, _bytesRead);
                    //Logger.WriteDebug($"{decoderName}: receive -> {_state.Data}");

                    //Split each frame
                    var _dataArray =
                        _state.Data.Split(new[] { decoderEtxString }, StringSplitOptions.RemoveEmptyEntries);
                    if (_dataArray != null && _dataArray.Count() > 0)
                    {
                        //Loop through all frames
                        foreach (var _data in _dataArray)
                        {
                            //Raise event
                            MessageReceived.RaiseEvent(this, new MessageEventArgs(_data));
                            //Clear buffer
                            _state.Data = _state.Data.Replace(_data + decoderEtxString, string.Empty);
                        }
                    }


                    if (_state.Socket != null && _state.Socket.Connected)
                    {
                        //Start receive timeout again
                        receiveTimeout.Start();
                        //Start receive again
                        _state.Socket.BeginReceive(_state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback,
                                                   _state);
                    }
                    else
                    {
                        //Raise event
                        Disconnected.RaiseEvent(this, null);
                        //Try to reconnect again;
                        Reconnect();
                    }
                }
                //EndRecieve returns 0 bytes -> connection have been closed by the remote endpoint.
                else if (_bytesRead == 0 && decoderStatus == ConnectionStatus.Connected)
                {
                    Logger.WriteDebug($"{decoderName}: receive -> connection closed from server");

                    //Raise event
                    Disconnected.RaiseEvent(this, null);
                    //Try to reconnect again;
                    Reconnect();
                }
                else if (_socketError != SocketError.Success)
                {
                    Logger.WriteDebug($"{decoderName}: receive -> SocketError({_socketError})");

                    //Raise event
                    Disconnected.RaiseEvent(this, null);
                    //Try to reconnect again;
                    Reconnect();
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                Logger.WriteDebug($"{decoderName}: {ex} -> {ex.Message}");
            }
        }
コード例 #8
0
        public void ReceiveCallback(IAsyncResult _asyncResult)
        {
            // Retrieve the state object and the handler socket from the asynchronous state object.
            var _state = (StateObject)_asyncResult.AsyncState;

            if (_state == null || decoderSocket == null)
            {
                return;
            }

            var _socketError = new SocketError();
            var _bytesRead   = 0;

            try
            {
                // Read data from the remote device.
                _bytesRead = decoderSocket.EndReceive(_asyncResult, out _socketError);

                //EndRecieve returns x bytes -> connection ok
                if (_bytesRead > 0)
                {
                    //Store the data received so far
                    _state.Data += Encoding.ASCII.GetString(_state.Buffer, 0, _bytesRead);

                    //Split each frame
                    var _dataArray = _state.Data.Split(new[]
                    {
                        decoderEtxString
                    }, StringSplitOptions.RemoveEmptyEntries);
                    if (_dataArray != null && _dataArray.Count() > 0)
                    {
                        //Loop through all frames
                        foreach (var _data in _dataArray)
                        {
                            Logger.WriteDebug($"{decoderName}: Message -> {_data}");

                            //Remove frame + etx from buffer
                            _state.Data = _state.Data.Replace(_data + decoderEtxString, "");
                            //Raise event
                            MessageReceived.RaiseEvent(this, new MessageEventArgs(_data));
                        }
                    }

                    if (decoderSocket != null && decoderSocket.Connected)
                    {
                        //Start receive again
                        decoderSocket.BeginReceive(_state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback,
                                                   _state);
                    }
                }
                //EndRecieve returns 0 bytes -> connection have been closed by the remote endpoint.
                else if (_bytesRead == 0 && decoderStatus == ConnectionStatus.ClientConnected)
                {
                    Logger.WriteDebug($"{decoderName}: Connection closed from client");

                    //Raise event
                    Disconnected.RaiseEvent(this, null);
                    //Try to reconnect again;
                    Restart();
                }
                else if (_socketError != SocketError.Success)
                {
                    Logger.WriteDebug($"{decoderName}: SocketError -> {_socketError}");
                }
            }
            catch (Exception ex)
            {
                Logger.WriteDebug($"{decoderName}: Error on receive -> {ex.Message}");
                //Raise event
                Disconnected.RaiseEvent(this, null);
                //Try to reconnect again;
                Restart();
            }
        }