diff --git a/src/src/TapiLine.cs b/src/src/TapiLine.cs index c15ebec..87ae579 100644 --- a/src/src/TapiLine.cs +++ b/src/src/TapiLine.cs @@ -221,7 +221,7 @@ public int RingModeCount /// public AnswerModes AnswerMode { - get { return (AnswerModes) _ldc.dwAnswerMode; } + get { return (AnswerModes)_ldc.dwAnswerMode; } } /// @@ -419,7 +419,7 @@ public Guid Guid /// public string[] AvailableDeviceClasses { - get { return (string[]) _devClasses.Clone(); } + get { return (string[])_devClasses.Clone(); } } /// @@ -520,7 +520,7 @@ public int OpenCount /// public MediaModes OpenMediaModes { - get { return (MediaModes) _lds.dwOpenMediaModes; } + get { return (MediaModes)_lds.dwOpenMediaModes; } } /// @@ -663,7 +663,7 @@ public bool Locked /// public bool Connected { - get { return (_lds.dwDevStatusFlags & NativeMethods.LINEDEVSTATUSFLAGS_CONNECTED) > 0;} + get { return (_lds.dwDevStatusFlags & NativeMethods.LINEDEVSTATUSFLAGS_CONNECTED) > 0; } set { _lineOwner.SetDeviceState(NativeMethods.LINEDEVSTATUSFLAGS_CONNECTED, value); } } @@ -685,7 +685,7 @@ public byte[] DeviceSpecificData /// public MediaModes AvailableMediaModes { - get { return (MediaModes) _lds.dwAvailableMediaModes; } + get { return (MediaModes)_lds.dwAvailableMediaModes; } } /// @@ -763,12 +763,12 @@ public string ToString(string format, IFormatProvider formatProvider) /// public sealed class TapiLine : IDisposable, ITapiLine { - private const int MinTapiVersion = (int) TapiVersion.V13; - private const int MaxTapiVersion = (int) TapiVersion.V31; + private const int MinTapiVersion = (int)TapiVersion.V13; + private const int MaxTapiVersion = (int)TapiVersion.V31; private readonly TapiManager _mgr; - private readonly int _deviceId; - private readonly int _negotiatedVersion; + private readonly int _deviceId; + private readonly int _negotiatedVersion; private int _negotiatedExtVersion; private readonly int _stringFormat; private string _lineName = string.Empty; @@ -811,13 +811,13 @@ public sealed class TapiLine : IDisposable, ITapiLine /// Owner /// Device ID for this line internal TapiLine(TapiManager mgr, int deviceId) - { + { _mgr = mgr; - _deviceId = deviceId; + _deviceId = deviceId; _lcb = LineCallback; LINEEXTENSIONID extId; - int rc = NativeMethods.lineNegotiateAPIVersion(_mgr.LineHandle, _deviceId, + int rc = NativeMethods.lineNegotiateAPIVersion(_mgr.LineHandle, _deviceId, MinTapiVersion, MaxTapiVersion, out _negotiatedVersion, out extId); if (rc == NativeMethods.LINEERR_OK) { @@ -832,9 +832,9 @@ internal TapiLine(TapiManager mgr, int deviceId) { _addresses[i] = new TapiAddress(this, i); // Forward events from this address - _addresses[i].Changed += delegate(object sender, AddressInfoChangeEventArgs e) { if (AddressChanged != null) AddressChanged(this, e); }; - _addresses[i].CallStateChanged += delegate(object sender, CallStateEventArgs e) { if (CallStateChanged != null) CallStateChanged(this, e); }; - _addresses[i].CallInfoChanged += delegate(object sender, CallInfoChangeEventArgs e) { if (CallInfoChanged != null) CallInfoChanged(this, e); }; + _addresses[i].Changed += delegate (object sender, AddressInfoChangeEventArgs e) { if (AddressChanged != null) AddressChanged(this, e); }; + _addresses[i].CallStateChanged += delegate (object sender, CallStateEventArgs e) { if (CallStateChanged != null) CallStateChanged(this, e); }; + _addresses[i].CallInfoChanged += delegate (object sender, CallInfoChangeEventArgs e) { if (CallInfoChanged != null) CallInfoChanged(this, e); }; } _extensionId = String.Format(CultureInfo.CurrentCulture, "{0}.{1}.{2}.{3}", extId.dwExtensionID0, extId.dwExtensionID1, extId.dwExtensionID2, extId.dwExtensionID3); @@ -845,7 +845,7 @@ internal TapiLine(TapiManager mgr, int deviceId) _addresses = new TapiAddress[0]; _props = new LineCapabilities(new LINEDEVCAPS(), null); } - } + } private LINEDEVCAPS GatherDevCaps() { @@ -870,7 +870,7 @@ private LINEDEVCAPS GatherDevCaps() rawBuffer = new byte[ldc.dwUsedSize]; Marshal.Copy(pLdc, rawBuffer, 0, ldc.dwUsedSize); } - Marshal.FreeHGlobal(pLdc); + Marshal.FreeHGlobal(pLdc); } while (rc == NativeMethods.LINEERR_STRUCTURETOOSMALL); _props = new LineCapabilities(ldc, rawBuffer); @@ -947,9 +947,9 @@ internal int StringFormat /// The numeric device ID representing the line. /// public int Id - { + { get { return _deviceId; } - } + } /// /// The permanent numeric ID representing this line @@ -963,9 +963,9 @@ public int PermanentId /// The that this line negotiated to. /// public TapiVersion NegotiatedVersion - { - get { return (TapiVersion) _negotiatedVersion; } - } + { + get { return (TapiVersion)_negotiatedVersion; } + } /// /// This associates an arbitrary object with the line device @@ -988,7 +988,7 @@ public int NegotiateExtensions(int minVersion, int maxVersion, EventHandler public ITapiAddress[] Addresses { - get { return (TapiAddress[]) _addresses.Clone(); } + get { return (TapiAddress[])_addresses.Clone(); } } /// @@ -1060,7 +1060,7 @@ public ITapiAddress FindAddress(string number) /// public string Name { - get + get { if (_lineName.Length == 0 && _props != null) _lineName = _props.LineName; @@ -1091,9 +1091,9 @@ public LineStatus Status /// /// String public override string ToString() - { - return Name; - } + { + return Name; + } /// /// Returns true/false whether the line is currently open. @@ -1137,7 +1137,7 @@ public void Monitor() /// /// Address ID to open (-1 for all) private void Open(Privilege openMode, MediaModes mediaMode, int addressId) - { + { if (IsOpen) throw new TapiException("Line is already open", NativeMethods.LINEERR_OPERATIONUNAVAIL); @@ -1155,7 +1155,7 @@ private void Open(Privilege openMode, MediaModes mediaMode, int addressId) uint hLine; int rc = NativeMethods.lineOpen(_mgr.LineHandle, _deviceId, out hLine, _negotiatedVersion, _negotiatedExtVersion, - Marshal.GetFunctionPointerForDelegate(_lcb), privilege, (int) mediaMode, ref lcp); + Marshal.GetFunctionPointerForDelegate(_lcb), privilege, (int)mediaMode, ref lcp); if (rc == NativeMethods.LINEERR_OK) { @@ -1177,7 +1177,7 @@ private void Open(Privilege openMode, MediaModes mediaMode, int addressId) } else throw new TapiException("lineOpen failed", rc); - } + } /// /// This closes the line device. @@ -1233,8 +1233,8 @@ public ITapiCall MakeCall(string address) /// object or null. public ITapiCall MakeCall(string address, Country country, MakeCallParams param) { - return (from addr in Addresses - where addr.Status.CanMakeCall + return (from addr in Addresses + where addr.Status.CanMakeCall select addr.MakeCall(address, (country == null) ? 0 : country.CountryCode, param) ).FirstOrDefault(); } @@ -1405,7 +1405,7 @@ public void CancelForward() int rc = NativeMethods.lineForward(Handle, 1, 0, IntPtr.Zero, 0, out hCall, IntPtr.Zero); if (rc < 0) throw new TapiException("lineForward failed", rc); - + // Wait for the LINE_REPLY so we don't need to deal with the value type // issues of IntPtr being filled in async. var req = new PendingTapiRequest(rc, null, null); @@ -1563,7 +1563,7 @@ public byte[] GetDeviceConfig(string deviceClass) Marshal.FreeHGlobal(pVs); } while (rc == NativeMethods.LINEERR_STRUCTURETOOSMALL); - + if (rc != 0) throw new TapiException("lineGetDevConfig failed", rc); return rawBuffer; @@ -1629,7 +1629,7 @@ private void LineCallback(TapiEvent dwMessage, IntPtr dwParam1, IntPtr dwParam2, case TapiEvent.LINE_ADDRESSSTATE: _addresses[dwParam1.ToInt32()].OnAddressStateChange(dwParam2.ToInt32()); break; - + case TapiEvent.LINE_LINEDEVSTATE: HandleDevStateChange(dwParam1.ToInt32(), dwParam2, dwParam3); break; @@ -1675,24 +1675,20 @@ internal void OnDeviceSpecific(TapiCall call, IntPtr dwParam1, IntPtr dwParam2, private void HandleNewCall(TapiCall call, int callPrivileges) { + f if (NewCall != null) { Privilege priv = (callPrivileges == NativeMethods.LINECALLPRIVILEGE_NONE) ? Privilege.None : (callPrivileges == NativeMethods.LINECALLPRIVILEGE_MONITOR) ? Privilege.Monitor : Privilege.Owner; foreach (EventHandler nc in NewCall.GetInvocationList()) { - nc.BeginInvoke(this, new NewCallEventArgs(call, priv), - delegate(IAsyncResult ar) - { - try - { - var nce = (EventHandler)ar.AsyncState; - nce.EndInvoke(ar); - } - catch - { - } - }, nc); + try + { + await Task.Run(() => nc(this, new NewCallEventArgs(call, priv))); + } + catch + { + } } } } diff --git a/src/src/TapiManager.cs b/src/src/TapiManager.cs index 3ab4d58..6ae74ab 100644 --- a/src/src/TapiManager.cs +++ b/src/src/TapiManager.cs @@ -28,14 +28,14 @@ namespace JulMar.Atapi { - /// - /// The TapiManager class is the starting point for the library. It does the initial negotiation with TAPI + /// + /// The TapiManager class is the starting point for the library. It does the initial negotiation with TAPI /// and maintains the list of lines which can be worked with. - /// - public sealed class TapiManager : IDisposable + /// + public sealed class TapiManager : IDisposable { - private const int RequestTimeoutSeconds = 5; - private readonly string _appName; + private const int RequestTimeoutSeconds = 5; + private readonly string _appName; private HTLINEAPP _hTapiLine = new HTLINEAPP(); private HTPHONEAPP _hTapiPhone = new HTPHONEAPP(); private int _lineVersion; @@ -115,11 +115,11 @@ public TapiManager(string appname) public TapiManager(string appName, TapiVersion ver) { _appName = appName; - _lineVersion = (int) ver; + _lineVersion = (int)ver; _phoneVersion = (int)ver; _lineArray = new List(); _phoneArray = new List(); - } + } /// /// This method initializes the TAPI infrastructure. @@ -269,7 +269,7 @@ public void Shutdown() } } - try + try { _hTapiLine.Close(); } @@ -342,9 +342,9 @@ private static TapiProvider ReadTapiProviderInfo(LINEPROVIDERLIST lpl, byte[] ra Marshal.PtrToStructure(pLpe, lpe); Marshal.FreeHGlobal(pLpe); - return new TapiProvider(lpe.dwPermanentProviderID, - NativeMethods.GetString(rawBuffer, - lpe.dwProviderFilenameOffset, lpe.dwProviderFilenameSize, + return new TapiProvider(lpe.dwPermanentProviderID, + NativeMethods.GetString(rawBuffer, + lpe.dwProviderFilenameOffset, lpe.dwProviderFilenameSize, NativeMethods.STRINGFORMAT_UNICODE)); } @@ -412,8 +412,8 @@ public TapiPhone GetPhoneByPermanentId(int permanentLineId) /// /// Processes the TAPI messages /// - private void ProcessTapiMessages() - { + private async void ProcessTapiMessages() + { var arrWait = new WaitHandle[] { _evtStop, _evtReceivedLineEvent, _evtReceivedPhoneEvent }; var msg = new LINEMESSAGE(); ProcessTapiMessageDelegate ptmCb = ProcessTapiMessage; @@ -453,20 +453,17 @@ private void ProcessTapiMessages() // made a blocking call and is waiting on the result from this // thread.. if the WinForms/WPF app then attempts to marshal to // the UI thread from this callback it will deadlock. - ptmCb.BeginInvoke(msg, ar => - { - try - { - ptmCb.EndInvoke(ar); - } - catch (Exception ex) - { - Trace.WriteLine("TAPI message exception: " + ex.Message); - } - }, null); + try + { + await Task.Run(() => ptmCb(msg)); + } + catch (Exception ex) + { + Trace.WriteLine("TAPI message exception: " + ex.Message); + } } } - } + } delegate void ProcessTapiMessageDelegate(LINEMESSAGE msg); @@ -478,13 +475,13 @@ private void ProcessTapiMessage(LINEMESSAGE msg) { TapiCall call = TapiCall.FindCallByHandle(msg.hDevice); if (call != null) - call.OnCallStateChange(msg.dwParam1.ToInt32(), msg.dwParam2, (MediaModes) msg.dwParam3.ToInt32()); + call.OnCallStateChange(msg.dwParam1.ToInt32(), msg.dwParam2, (MediaModes)msg.dwParam3.ToInt32()); else { // Call doesn't exist (yet), wait for a LINE_REPLY to add the call. lock (_pendingCallStateMessages) { - _pendingCallStateMessages.Add(Tuple.Create(DateTime.Now,msg)); + _pendingCallStateMessages.Add(Tuple.Create(DateTime.Now, msg)); } } } @@ -590,7 +587,7 @@ private void ProcessTapiMessage(LINEMESSAGE msg) case TapiEvent.PHONE_REPLY: HandleCompletion(msg.dwParam1.ToInt32(), msg.dwParam2); break; - + case TapiEvent.PHONE_STATE: break; @@ -651,7 +648,7 @@ private void ProcessTapiMessage(LINEMESSAGE msg) newLine.AddressChanged += HandleAddressChanged; newLine.Changed += HandleLineChanged; newLine.Ringing += HandleLineRinging; - + lock (_lineArray) { _lineArray.Add(newLine); @@ -682,7 +679,7 @@ private void ProcessTapiMessage(LINEMESSAGE msg) HandleCompletion(msg.dwParam1.ToInt32(), msg.dwParam2); break; - + default: break; } @@ -691,7 +688,7 @@ private void ProcessTapiMessage(LINEMESSAGE msg) internal void HandleCompletion(int reqId, IntPtr finalResult) { DateTime timeEntered = DateTime.Now; - for (; ;) + for (; ; ) { // Wait up to 5 seconds for a completion event to show up. if ((DateTime.Now - timeEntered).TotalSeconds > RequestTimeoutSeconds) @@ -715,11 +712,11 @@ internal void HandleCompletion(int reqId, IntPtr finalResult) } } - private void HandleNewCall(object sender, NewCallEventArgs e) - { + private void HandleNewCall(object sender, NewCallEventArgs e) + { if (NewCall != null) NewCall(this, e); - } + } private void HandleCallStateChanged(object sender, CallStateEventArgs e) { @@ -779,7 +776,7 @@ internal int GetAsyncRequestid() /// to an async request creating calls. /// internal void ReportQueuedCallStateChanges() - { + { lock (_pendingCallStateMessages) { for (int i = 0; i < _pendingCallStateMessages.Count; i++) @@ -790,7 +787,7 @@ internal void ReportQueuedCallStateChanges() TapiCall call = TapiCall.FindCallByHandle(msg.hDevice); if (call != null) { - call.OnCallStateChange(msg.dwParam1.ToInt32(), msg.dwParam2, (MediaModes) msg.dwParam3.ToInt32()); + call.OnCallStateChange(msg.dwParam1.ToInt32(), msg.dwParam2, (MediaModes)msg.dwParam3.ToInt32()); remove = true; } // Not found - has it been queued up too long? @@ -807,6 +804,6 @@ internal void ReportQueuedCallStateChanges() } } } - } + } } }