Ejemplo n.º 1
0
        public Task <bool> SendBuffer(byte[] data, int offset, int count)
        {
            PLog.LogTrace($"Sending data to {m_remote}, Data Length = {count}");
            TaskCompletionSource <bool> src = new TaskCompletionSource <bool>();

            if (!m_isConnected)
            {
                PLog.LogWarning("Failed to send. Needs to be connected before calling send");
                src.SetResult(false);
                return(src.Task);
            }

            try
            {
                var args = m_factory.GetEmptyArgs(); // No need to Free these, as using own buffer
                args.UserToken      = src;
                args.RemoteEndPoint = m_remote;
                args.SetBuffer(data, offset, count);
                args.Completed += OnSendBufferCompleted;

                if (!m_socket.SendAsync(args))
                {
                    OnSendBufferCompleted(this, args as SocketAsyncEventArgs);
                }
            }
            catch (Exception ex)
            {
                PLog.LogError(ex, $"Exception while sending data to {m_remote}");
                src.SetException(ex);
            }

            return(src.Task);
        }
Ejemplo n.º 2
0
            // Removing this test until test ordering is sorted out
            public void TestWhenLoggerIsNull()
            {
                var testex    = new ArgumentException("ARGEX");
                var testevent = new EventId(123);

                PLog.LogCritical("TEST1");
                PLog.LogCritical("TEST2 {0}", "PARAM2");
                PLog.LogCritical(testex, "TEST3 {0}", "PARAM3");
                PLog.LogCritical(testevent, "TEST4");
                PLog.LogCritical(testevent, testex, "TEST4");

                testex    = new ArgumentException("ARGEX");
                testevent = new EventId(123);

                PLog.LogDebug("TEST1");
                PLog.LogDebug("TEST2 {0}", "PARAM2");
                PLog.LogDebug(testex, "TEST3 {0}", "PARAM3");
                PLog.LogDebug(testevent, "TEST4");
                PLog.LogDebug(testevent, testex, "TEST4");

                testex    = new ArgumentException("ARGEX");
                testevent = new EventId(123);

                PLog.LogError("TEST1");
                PLog.LogError("TEST2 {0}", "PARAM2");
                PLog.LogError(testex, "TEST3 {0}", "PARAM3");
                PLog.LogError(testevent, "TEST4");
                PLog.LogError(testevent, testex, "TEST4");

                testex    = new ArgumentException("ARGEX");
                testevent = new EventId(123);

                PLog.LogInformation("TEST1");
                PLog.LogInformation("TEST2 {0}", "PARAM2");
                PLog.LogInformation(testex, "TEST3 {0}", "PARAM3");
                PLog.LogInformation(testevent, "TEST4");
                PLog.LogInformation(testevent, testex, "TEST4");

                testex    = new ArgumentException("ARGEX");
                testevent = new EventId(123);

                PLog.LogTrace("TEST1");
                PLog.LogTrace("TEST2 {0}", "PARAM2");
                PLog.LogTrace(testex, "TEST3 {0}", "PARAM3");
                PLog.LogTrace(testevent, "TEST4");
                PLog.LogTrace(testevent, testex, "TEST4");

                testex    = new ArgumentException("ARGEX");
                testevent = new EventId(123);

                PLog.LogWarning("TEST1");
                PLog.LogWarning("TEST2 {0}", "PARAM2");
                PLog.LogWarning(testex, "TEST3 {0}", "PARAM3");
                PLog.LogWarning(testevent, "TEST4");
                PLog.LogWarning(testevent, testex, "TEST4");
            }
Ejemplo n.º 3
0
        private void OnSendBufferCompleted(object sender, SocketAsyncEventArgs e)
        {
            var src    = e.UserToken as TaskCompletionSource <bool>;
            var result = (e.SocketError == SocketError.Success) && (e.LastOperation == SocketAsyncOperation.Send);

            try
            {
                if (result)
                {
                    PLog.LogTrace($"Send to {m_remote} completed.");
                    src.SetResult(true);
                }
                else
                {
                    PLog.LogWarning($"Send operation to {m_remote} failed on {e.LastOperation} with {e.SocketError}");
                    src.SetResult(false);
                }
            }
            catch (Exception ex)
            {
                PLog.LogError(ex, $"Exception while completing send to {m_remote}");
                src.SetException(ex);
            }
        }
Ejemplo n.º 4
0
        private void OnConnectCompleted(object sender, SocketAsyncEventArgs e)
        {
            var src    = e.UserToken as TaskCompletionSource <bool>;
            var result = e.SocketError;

            try
            {
                if (src == null)
                {
                    PLog.LogError("SockConn requires UserToken to be of type TaskCompletionSource<bool>");
                    throw new ArgumentException("SockConn requires UserToken to be of type TaskCompletionSource<bool>");
                }

                if (result == SocketError.Success)
                {
                    PLog.LogTrace($"Connected to {m_remote}");
                    m_isConnected = true;
                    src.SetResult(true);
                }
                else
                {
                    PLog.LogWarning($"Failed to connect to {m_remote} with error {result}");
                    src.SetResult(false);
                    m_isConnecting = false;
                    return;
                }

                //
                // Setup our receive loop
                //
                var args = m_factory.AllocateArgs();
                args.RemoteEndPoint = m_remote;
                args.Completed     += (s, a) =>
                {
                    try
                    {
                        PLog.LogTrace($"Received Data from {m_remote}, OnCompleted {a.LastOperation} with {a.SocketError}");
                        if (a.SocketError == SocketError.Success)
                        {
                            if (a.LastOperation == SocketAsyncOperation.Receive)
                            {
                                if (
                                    (m_receiveCallback != null) &&
                                    (!m_receiveCallback.Invoke(a as ISocketAsyncEventArgs))
                                    )
                                {
                                    //
                                    // Handler returned false, so we need new Args for next call
                                    // as the buffer is still being used
                                    //
                                    args = m_factory.AllocateArgs();
                                    args.RemoteEndPoint = m_remote;
                                }
                            }
                            m_socket.ReceiveAsync(args);
                        }
                        else
                        {
                            PLog.LogWarning($"Receive operation failed from {m_remote}, stopping Receive loop");
                            m_isConnected     = false;
                            m_receiveCallback = null;
                            m_factory.FreeArgs(args);
                        }
                    }
                    catch (Exception ex)
                    {
                        PLog.LogError(ex, $"Execption while receiving data from {m_remote}");
                        m_isConnected     = false;
                        m_receiveCallback = null;
                        m_factory.FreeArgs(args);
                        Console.WriteLine(ex.ToString());
                    }
                };
                m_socket.ReceiveAsync(args);
            }
            catch (Exception ex)
            {
                PLog.LogError(ex, $"Execption while connecting to {m_remote}");
                m_isConnected     = false;
                m_isConnecting    = false;
                m_receiveCallback = null;
                src.SetException(ex);
            }
        }
Ejemplo n.º 5
0
        public void TestPLogLogging()
        {
            var testlevel = "Critical";
            var testex    = new ArgumentException("ARGEX");
            var testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogCritical("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogCritical("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogCritical(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogCritical(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogCritical(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");

            testlevel = "Debug";
            testex    = new ArgumentException("ARGEX");
            testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogDebug("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogDebug("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogDebug(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogDebug(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogDebug(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");

            testlevel = "Error";
            testex    = new ArgumentException("ARGEX");
            testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogError("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogError("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogError(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogError(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogError(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");

            testlevel = "Information";
            testex    = new ArgumentException("ARGEX");
            testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogInformation("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogInformation("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogInformation(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogInformation(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogInformation(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");

            testlevel = "Trace";
            testex    = new ArgumentException("ARGEX");
            testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogTrace("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogTrace("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogTrace(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogTrace(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogTrace(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");

            testlevel = "Warning";
            testex    = new ArgumentException("ARGEX");
            testevent = new EventId(123);

            TestLog.Clear();
            PLog.LogWarning("TEST1");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST1");

            TestLog.Clear();
            PLog.LogWarning("TEST2 {0}", "PARAM2");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST2", "PARAM2");

            TestLog.Clear();
            PLog.LogWarning(testex, "TEST3 {0}", "PARAM3");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST3", "PARAM3", "ARGEX");

            TestLog.Clear();
            PLog.LogWarning(testevent, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]");

            TestLog.Clear();
            PLog.LogWarning(testevent, testex, "TEST4");
            TestLog.AssertLogMatch("PBase", testlevel, "TEST4", "[123]", "ARGEX");
        }