EventFirer.cs :  » Business-Application » 32feet.NET » InTheHand » Net » Tests » Widcomm » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » 32feet.NET 
32feet.NET » InTheHand » Net » Tests » Widcomm » EventFirer.cs
using System;
using System.Collections.Generic;
using System.Text;
using InTheHand.Net.Bluetooth.Widcomm;
using System.Threading;

namespace InTheHand.Net.Tests.Widcomm{
    abstract class EventFirer
    {
        internal delegate void Foo(TestRfcommPort port, PORT_EV eventId);
        //
        protected TestRfcommPort port;

        public EventFirer(TestRfcommPort port)
        {
            this.port = port;
        }

        //public abstract void Run();
        internal abstract void Complete();

        #region Delegate End-/dlgt_BeginInvoke
        protected static IAsyncResult dlgt_BeginInvoke(Foo dlgt, TestRfcommPort port, PORT_EV eventId,
            AsyncCallback callback, object state)
        {
#if true || PocketPC
            FooParams args = new FooParams(dlgt, port, eventId);
            AsyncNoResult<FooParams> ar = new AsyncNoResult<FooParams>(callback, state, args);
            ThreadPool.QueueUserWorkItem(dlgt_Runner, ar);
            return ar;
#else
            arDlgt = dlgt.BeginInvoke(port, eventId, null, null); // NETCF-ok
#endif
        }

        protected static void dlgt_EndInvoke(Foo dlgt, IAsyncResult ar)
        {
#if true || PocketPC
            AsyncNoResult<FooParams> ar2 = (AsyncNoResult<FooParams>)ar;
            ar2.EndInvoke();
#else
            dlgt.EndInvoke(arDlgt); // NETCF-ok
#endif
        }

        sealed class FooParams
        {
            public readonly Foo dlgt;
            public readonly TestRfcommPort port;
            public readonly PORT_EV eventId;

            public FooParams(Foo dlgt, TestRfcommPort port, PORT_EV eventId)
            {
                this.dlgt = dlgt;
                this.port = port;
                this.eventId = eventId;
            }

        }

        static void dlgt_Runner(object state)
        {
            AsyncNoResult<FooParams> ar = (AsyncNoResult<FooParams>)state;
            try {
                ar.BeginParameters.dlgt(ar.BeginParameters.port, ar.BeginParameters.eventId);
                ar.SetAsCompleted(null, false);
            } catch (Exception ex) {
                ar.SetAsCompleted(ex, false);
                /* TODO TargetInvocationException tex
                        = new System.Reflection.TargetInvocationException(ex);
                    ar.SetAsCompleted(tex, false);
                 */
            }
        }
        #endregion
    }

    class OneEventFirer : EventFirer
    {
        const int NoTimeout = -1; // ?or int.MinValue
        protected int preEventDelayMilliseconds = NoTimeout;
        IAsyncResult arDlgt;

        Foo dlgt;

        public OneEventFirer(TestRfcommPort port)
            : base(port)
        {
        }

        public OneEventFirer(TestRfcommPort port, int preEventDelayMilliseconds)
            : base(port)
        {
            this.preEventDelayMilliseconds = preEventDelayMilliseconds;
        }

        public void Run(PORT_EV eventId)
        {
            dlgt = delegate(TestRfcommPort port2, PORT_EV eventId_) {
                if (preEventDelayMilliseconds != -1)
                    Thread.Sleep(preEventDelayMilliseconds);
                port2.NewEvent(eventId_);
            };
            arDlgt = dlgt_BeginInvoke(dlgt, port, eventId, null, null);
        }

        internal override void Complete()
        {
            dlgt_EndInvoke(dlgt, arDlgt); // any exceptions?
        }
    }

    class OneDataEventFirer : EventFirer
    {
        const int NoTimeout = -1; // ??int.MinValue
        protected int preEventDelayMilliseconds = NoTimeout;
        IAsyncResult arDlgt;

        Foo dlgt;

        public OneDataEventFirer(TestRfcommPort port)
            : base(port)
        {
        }

        public void Run(byte[] data)
        {
            dlgt = delegate(TestRfcommPort port2, PORT_EV eventId_) {
                if (preEventDelayMilliseconds != NoTimeout)
                    Thread.Sleep(preEventDelayMilliseconds);
                port2.NewReceive(data);
            };
            arDlgt = dlgt_BeginInvoke(dlgt, port, (PORT_EV)0, null, null);
        }

        internal override void Complete()
        {
            dlgt_EndInvoke(dlgt, arDlgt); // any exceptions?
        }
    }

    class OneEvent100msFirer : OneEventFirer
    {
        public OneEvent100msFirer(TestRfcommPort port)
            : base(port)
        {
            preEventDelayMilliseconds = 100;
        }
    }

    class FireOpenReceiveCloseEvents : EventFirer
    {
        IAsyncResult arDlgt, arDlgt2, arDlgt3;

        Foo dlgt = delegate(TestRfcommPort port2, PORT_EV eventId) {
            port2.NewEvent(eventId);
        };
        Foo dlgtDelay2 = delegate(TestRfcommPort port2, PORT_EV eventId) {
            Thread.Sleep(50);
            port2.NewEvent(eventId);
        };
        Foo dlgtDelay3 = delegate(TestRfcommPort port2, PORT_EV eventId) {
            Thread.Sleep(100);
            port2.NewReceive(new byte[] { (byte)'c' });
        };

        public FireOpenReceiveCloseEvents(TestRfcommPort port)
            : base(port)
        {
        }

        public void Run()
        {
            arDlgt = dlgt_BeginInvoke(dlgt, port, PORT_EV.CONNECTED, null, null);
            arDlgt2 = dlgt_BeginInvoke(dlgtDelay2, port, PORT_EV.CONNECT_ERR, null, null);
            arDlgt3 = dlgt_BeginInvoke(dlgtDelay3, port, PORT_EV.RING, null, null);
        }

        internal override void Complete()
        {
            dlgt_EndInvoke(dlgt, arDlgt); // any exceptions?
            dlgt_EndInvoke(dlgtDelay2, arDlgt2); // any exceptions?
            dlgt_EndInvoke(dlgtDelay3, arDlgt3); // any exceptions?
        }
    }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.