Safe handle has been closed - Moon APNS

862 Views Asked by At

I am trying to develop my own Apple Push Notification Server application to send push notification to iOS devices. I download the source code for Moon-APNS from GitHub and modified it to fit with my Web API application.

The following line of code is causing IIS to throw an unhandled win32 exception:

apnsStream.BeginRead(response, 0, 6, new AsyncCallback(ReadResponse), new MyAsyncInfo(response, apnsStream));

Went I look in my computer applications windows log in event viewer, the unhandled exception comes from the ReadResponse method. I have the code with the ReadResponse method and apnsStream.BeginRead call contained within a try-catch statements and it still throws an unhandled exception.

Message: Safe handle has been closed

Stack Trace: at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult) at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.ContextAwareResult.Complete(IntPtr userToken) at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

Here is the complete section of source when I open the TcpClient to the Apple Push Notification Server:

using(TcpClient apnsClient = new TcpClient())
{
    //open connection to the Apple Push Notification service a TCP client connection
    apnsClient.Connect("gateway.sandbox.push.apple.com", 2195);

    //open an SSL Stream
    using(SslStream apnsStream = new SslStream(apnsClient.GetStream(), false, ValidateServerCertificate, SelectLocalCertificate))
    {
        apnsStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", certificates, System.Security.Authentication.SslProtocols.Default, false);

        _CanReadStream = apnsStream.CanRead;
        _IsConnected = true;

        var response = new byte[6];

        apnsStream.BeginRead(response, 0, 6, new AsyncCallback(ReadResponse), new MyAsyncInfo(response, apnsStream));

        //put code to generate payload here           
    }
}

Source code from ReadResponse method

private void ReadResponse(IAsyncResult ar)
{
    if (_IsConnected) return;

    int payLoadIndex = 0;
    string payLoadId = string.Empty;

    try
    {
        var info = ar.AsyncState as MyAsyncInfo;
        info.Stream.ReadTimeout = 100;

        if (_CanReadStream)
        {
            var command = Convert.ToInt16(info.ByteArray[0]);
            var status = Convert.ToInt16(info.ByteArray[1]);
            var id = new byte[4];
            Array.Copy(info.ByteArray, 2, id, 0, 4);

            payLoadId = Encoding.Default.GetString(id);
            payLoadIndex = int.Parse(payLoadId) - 1000;

            _RejectedDevices.Add(_NotificationPayload[payLoadIndex].DeviceToken);

             _IsConnected = false;
         }
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

What can do to resolve this issue?

0

There are 0 best solutions below