Beispiel #1
0
        public void downloading(IAsyncResult res)
        {
            int read = 0;

            try { read = response_stream.EndRead(res); }
            catch (Exception e) { failure("Error receving response from server after saving file", e); return; }
            if (read > 0)
            {
                downloaded += read;
                if (downloaded < buffer.Length)
                {
                    try { response_stream.BeginRead(buffer, downloaded, buffer.Length - downloaded, downloading, this); }
                    catch (Exception e) { failure("Error receving response from server after saving file", e); }
                    return;
                }
            }
            try
            {
                response_stream.Close();
                response_stream = null;
            }
            catch (Exception) { }

            string resp = System.Text.Encoding.UTF8.GetString(buffer, 0, downloaded);

            if (resp.Equals("OK"))
            {
                saved = true;
            }
            else
            {
                failure("Error saving file: " + resp, null);
            }
        }
        private void ReadCallback(IAsyncResult ar)
        {
            try
            {
                var buffer = (byte[])ar.AsyncState;
                var readed = stream.EndRead(ar);

                if (0 < readed)
                {
                    parser.Push(buffer, 0, readed);

                    if (!canceled)
                    {
                        stream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
                    }
                }
                else
                {
                    OnElement(parser, null);
                }
            }
            catch (Exception ex)
            {
                OnError(parser, ex);
            }
        }
Beispiel #3
0
            static void StreamReadCallback(IAsyncResult ar)
            {
                var This = (GetResponse)ar.AsyncState;

                System.IO.Stream stream = null;
                try
                {
                    stream = This.resp.GetResponseStream();
                    var szRead = stream.EndRead(ar);
                    if (szRead > 0)
                    {
                        This.outputStream.Write(This.temp, 0, szRead);
                        stream.BeginRead(This.temp, 0, This.temp.Length, StreamReadCallback, This);
                    }
                    else
                    {
                        This.SetResponded(This.resp.StatusCode, This.resp.StatusDescription, This.resp.Cookies, This.resp.Headers);
                        stream.Close();
                        stream.Dispose();
                        stream = null;
                        This.resp.Close();
                        This.resp = null;
                    }
                }
                catch (System.Exception e)
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                        stream = null;
                    }
                    This.SetRespondedWithClientError(e);
                }
            }
Beispiel #4
0
        static int _m_EndRead(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.IO.Stream gen_to_be_invoked = (System.IO.Stream)translator.FastGetCSObj(L, 1);



                {
                    System.IAsyncResult _asyncResult = (System.IAsyncResult)translator.GetObject(L, 2, typeof(System.IAsyncResult));

                    int gen_ret = gen_to_be_invoked.EndRead(
                        _asyncResult);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Beispiel #5
0
        private void CheckRead()
        {
            if (_stream == null)
            {
                return;
            }

            if (_async != null)
            {
                if (_async.IsCompleted)
                {
                    _readCount  = _stream.EndRead(_async);
                    _readOffset = 0;
                    //Debug.Log("EndRead {0}", _readCount);
                    _async = null;

                    if (_readCount > 0)
                    {
                        LastTime = System.DateTime.Now;
                    }
                }
            }
            else if (Available == 0)
            {
                //Debug.Log("BeginRead");
                _async = _stream.BeginRead(_readBuffer, 0, _readBuffer.Length, null, null);
            }
        }
        void TransparentStreamReadRequestMessageReceived(TransparentStreamMessageBase transparentStreamMessageBase)
        {
            TransparentStreamReadRequestMessage request = (TransparentStreamReadRequestMessage)transparentStreamMessageBase;

            byte[] buffer = new byte[request.Count];
            try {
                baseStream.BeginRead(buffer, 0, request.Count, (ar) => {
                    try {
                        int bytesRead = baseStream.EndRead(ar);
                        if (bytesRead != ((byte[])ar.AsyncState).Length)
                        {
                            byte[] newBuffer = new byte[bytesRead];
                            System.Buffer.BlockCopy((byte[])ar.AsyncState, 0, newBuffer, 0, bytesRead);
                            objectBusSession.SendMessage(new TransparentStreamReadResponseMessage(streamID, request.ID, newBuffer, null));
                        }
                        else
                        {
                            objectBusSession.SendMessage(new TransparentStreamReadResponseMessage(streamID, request.ID, (byte[])ar.AsyncState, null));
                        }
                    } catch (Exception ex) {
                        objectBusSession.SendMessage(new TransparentStreamReadResponseMessage(streamID, request.ID, (byte[])ar.AsyncState, ex));
                    }
                }, buffer);
            } catch (Exception ex) {
                objectBusSession.SendMessage(new TransparentStreamReadResponseMessage(streamID, request.ID, null, ex));
            }
        }
Beispiel #7
0
        void Reader()
        {
            try
            {
                readAsyncResult = networkStream.BeginRead(readBuffer, 0, 6, new AsyncCallback((asyncResult) =>
                {
                    if (readAsyncResult != asyncResult)
                    {
                        return;
                    }

                    lock (sentLock)
                    {
                        try
                        {
                            var bytesRead = networkStream.EndRead(asyncResult);

                            if (bytesRead > 0)
                            {
                                //We now expect apple to close the connection on us anyway, so let's try and close things
                                // up here as well to get a head start
                                //Hopefully this way we have less messages written to the stream that we have to requeue
                                try { stream.Close(); } catch { }
                                try { stream.Dispose(); } catch { }

                                try { client.Client.Shutdown(SocketShutdown.Both); } catch { }
                                try { client.Client.Dispose(); } catch { }

                                try { client.Close(); } catch { }

                                client = null;
                                stream = null;

                                //Get the enhanced format response
                                // byte 0 is always '1', byte 1 is the status, bytes 2,3,4,5 are the identifier of the notification
                                var status     = readBuffer[1];
                                var identifier = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(readBuffer, 2));

                                HandleFailedNotification(identifier, status);

                                //Start reading again
                                Reader();
                            }
                            else
                            {
                                connected = false;
                            }
                        }
                        catch
                        {
                            connected = false;
                        }
                    }                     // End Lock
                }), null);
            }
            catch
            {
                connected = false;
            }
        }
Beispiel #8
0
        void Reader()
        {
            try
            {
                networkStream.BeginRead(readBuffer, 0, 6, new AsyncCallback((asyncResult) =>
                {
                    lock (sentLock)
                    {
                        try
                        {
                            var bytesRead = networkStream.EndRead(asyncResult);

                            if (bytesRead > 0)
                            {
                                Log.Info("in Reader bytes read {0}", bytesRead);
                                //We now expect apple to close the connection on us anyway, so let's try and close things
                                // up here as well to get a head start
                                //Hopefully this way we have less messages written to the stream that we have to requeue
                                try { stream.Close(); stream.Dispose(); }
                                catch { }

                                try { client.Close(); stream.Dispose(); }
                                catch { }

                                //Get the enhanced format response
                                // byte 0 is always '1', byte 1 is the status, bytes 2,3,4,5 are the identifier of the notification
                                var status     = readBuffer[1];
                                var identifier = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(readBuffer, 2));

                                HandleFailedNotification(identifier, status);

                                //Start reading again
                                Reader();
                                //connected = false;
                            }
                            else
                            {
                                Log.Warning("Read 0 bytes, perhaps this is the signal to close the stream. Shutting down client and the bunch.");
                                try { stream.Close(); stream.Dispose(); }
                                catch { }

                                try { client.Close(); stream.Dispose(); }
                                catch { }

                                connected = false;
                            }
                        }
                        catch
                        {
                            connected = false;
                        }
                    }                     // End Lock
                }), null);
            }
            catch
            {
                connected = false;
            }
        }
            protected void RecvCallback(IAsyncResult ar)
            {
                ResultHandler rh     = (ResultHandler)ar.AsyncState;
                int           result = mStream.EndRead(ar);

                if (result == 0)
                {
                    result = MoSync.Constants.CONNERR_CLOSED;
                }
                rh(mHandle, MoSync.Constants.CONNOP_READ, result);
            }
        public void StopReceiving()
        {
            try
            {
                stopReceiver = true;
                connectionStream.EndRead(null);

                connectionStream.Flush();
                connectionStream.Close();
            }
            catch (Exception e)
            {
                networkError = e;
                State        = NetworkStates.NetworkError;
            }
        }
        void Reader()
        {
            try
            {
                var result = networkStream.BeginRead(readBuffer, 0, 6, new AsyncCallback((asyncResult) =>
                {
                    lock (sentLock)
                    {
                        try
                        {
                            var bytesRead = networkStream.EndRead(asyncResult);

                            if (bytesRead > 0)
                            {
                                disconnect();

                                //Get the enhanced format response
                                // byte 0 is always '1', byte 1 is the status, bytes 2,3,4,5 are the identifier of the notification
                                var status     = readBuffer[1];
                                var identifier = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(readBuffer, 2));

                                HandleFailedNotification(identifier, status);

                                //Start reading again
                                Reader();
                            }
                            else
                            {
                                connected = false;
                            }
                        }
                        catch
                        {
                            connected = false;
                        }
                    }                     // End Lock
                }), null);
            }
            catch
            {
                connected = false;
            }
        }
Beispiel #12
0
            void ReadCallback(IAsyncResult asyncResult)
            {
                try
                {
                    var size = Stream.EndRead(asyncResult);
                    if (size <= 0)
                    {
                        var ip = System.Text.Encoding.ASCII.GetString(Buffer);
                        IPAddress = IPAddress.Parse(ip);
                        return;
                    }

                    BufferOffset += size;
                    Stream.BeginRead(Buffer, BufferOffset, Buffer.Length - BufferOffset, ReadCallback, null);
                }
                catch (System.Exception e)
                {
                    Exception = e;
                }
            }
Beispiel #13
0
        public void downloading(IAsyncResult res)
        {
            int read;

            try { read = stream.EndRead(res); }
            catch (Exception e) { failure("Error " + action, e); return; }
            if (read > 0)
            {
                downloaded += read;
                onprogress(buffer, read, total, downloaded);
                try { stream.BeginRead(buffer, 0, 65536, downloading, this); }
                catch (Exception e) { failure("Error " + action, e); }
                return;
            }
            try
            {
                stream.Close(); stream = null;
            }
            catch (Exception) { }
            onsuccess();
        }
Beispiel #14
0
        public void EndRead(System.IO.Stream stream, IAsyncResult asyncResult)
        {
            int read = stream.EndRead(asyncResult);

            _available += read;
        }
Beispiel #15
0
 /// <summary>
 /// Transfers an entire source stream to a target
 /// </summary>
 /// <param name="source">
 /// The stream to read
 /// </param>
 /// <param name="target">
 /// The stream to write
 /// </param>
 /// <returns>
 /// The total number of bytes transferred
 /// </returns>
 public Int32 Copy(Stream source, Stream target)
 {
     var copied = 0;
      var bufferIdx = 0;
      // start an initial dummy write to avoid
      // a null test within the copy loop
      var writer = target.BeginWrite(this.buffers[1], 0, 0, null, null);
      for (; ; )
      {
     // read into the current buffer
     var buffer = this.buffers[bufferIdx];
     var reader = source.BeginRead(buffer, 0, buffer.Length, null, null);
     // complete the previous write and the current read
     target.EndWrite(writer);
     var read = source.EndRead(reader);
     if (read == 0)
        break;
     copied += read;
     // start the next write for the completed read
     writer = target.BeginWrite(buffer, 0, read, null, null);
     // swap the buffer index for the next read
     bufferIdx = (bufferIdx + 1) % 2;
      }
      return copied;
 }
Beispiel #16
0
 public ReadTransaction(System.IO.Stream s, byte[] dest, int off, int len)
     : base(true, new System.AsyncCallback((iar) => s.BeginRead(dest, off, len, new System.AsyncCallback((ia) => s.EndRead(ia)), null)), null, null)
 {
 }
Beispiel #17
0
        void Reader()
        {
            try
            {
                var result = networkStream.BeginRead(readBuffer, 0, 6, new AsyncCallback((asyncResult) =>
                {
                    lock (sentLock)
                    {
                        try
                        {
                            var bytesRead = networkStream.EndRead(asyncResult);

                            if (bytesRead > 0)
                            {
                                //We now expect apple to close the connection on us anyway, so let's try and close things
                                // up here as well to get a head start
                                //Hopefully this way we have less messages written to the stream that we have to requeue
                                try { stream.Close(); stream.Dispose(); }
                                catch { }

                                try { client.Close(); stream.Dispose(); }
                                catch { }

                                //Get the enhanced format response
                                // byte 0 is always '1', byte 1 is the status, bytes 2,3,4,5 are the identifier of the notification
                                var status     = readBuffer[1];
                                var identifier = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(readBuffer, 2));

                                int failedNotificationIndex         = -1;
                                SentNotification failedNotification = null;

                                //Try and find the failed notification in our sent list
                                for (int i = 0; i < sentNotifications.Count; i++)
                                {
                                    var n = sentNotifications[i];

                                    if (n.Identifier.Equals(identifier))
                                    {
                                        failedNotificationIndex = i;
                                        failedNotification      = n;
                                        break;
                                    }
                                }

                                //Don't bother doing anything unless we know what failed
                                if (failedNotification != null && failedNotificationIndex > -1)
                                {
                                    //Anything before the failed message must have sent OK
                                    // so let's expedite the success status Success for all those before the failed one
                                    if (failedNotificationIndex > 0)
                                    {
                                        for (int i = 0; i < failedNotificationIndex; i++)
                                        {
                                            this.Events.RaiseNotificationSent(sentNotifications[i].Notification);
                                        }
                                    }

                                    //The notification that failed needs to have a failure event raised
                                    // we don't requeue it because apple told us it failed for real
                                    this.Events.RaiseNotificationSendFailure(failedNotification.Notification,
                                                                             new NotificationFailureException(status, failedNotification.Notification));

                                    // finally, raise failure for anything after the index of this failed one
                                    // in the sent list, since we may have sent them but apple will have disregarded
                                    // anything after the failed one and not told us about it
                                    if (failedNotificationIndex < sentNotifications.Count - 1)
                                    {
                                        //Requeue the failed notification since we're not sure it's a bad
                                        // notification, just that it was sent after a bad one was
                                        for (int i = failedNotificationIndex + 1; i <= sentNotifications.Count - 1; i++)
                                        {
                                            this.QueueNotification(sentNotifications[i].Notification, false);
                                        }
                                    }

                                    //Now clear out the sent list since we processed them all manually above
                                    sentNotifications.Clear();
                                }

                                //Start reading again
                                Reader();
                            }
                            else
                            {
                                connected = false;
                            }
                        }
                        catch
                        {
                            connected = false;
                        }
                    }                     // End Lock
                }), null);
            }
            catch
            {
                connected = false;
            }
        }
Beispiel #18
0
 /// <inheritdoc/>
 public override int EndRead(IAsyncResult asyncResult)
 {
     return(_underlyingStream.EndRead(asyncResult));
 }
Beispiel #19
0
 static void ProcessReceiveResults(IAsyncResult ar)
 {
     ns.EndRead(ar);
     this.Invoke(setRcvText, BitConverter.ToString(buffer));
 }
Beispiel #20
0
        void HandleReadDone(IAsyncResult ar)
        {
            if (is_disposed)
            {
                return;
            }

            int byte_read = image_stream.EndRead(ar);

            lock (sync_handle) {
                if (byte_read == 0)
                {
                    image_stream.Close();
                    Close();
                    Loading          = false;
                    notify_completed = true;
                }
                else
                {
                    try {
                        if (!is_disposed && Write(buffer, (ulong)byte_read))
                        {
                            image_stream.BeginRead(buffer, 0, count, HandleReadDone, null);
                        }
                    } catch (ObjectDisposedException) {
                    } catch (GLib.GException) {
                    }
                }
            }

            GLib.Idle.Add(delegate {
                //Send the AreaPrepared event
                if (notify_prepared)
                {
                    notify_prepared = false;
                    if (thumb != null)
                    {
                        thumb.Dispose();
                        thumb = null;
                    }

                    AreaPrepared?.Invoke(this, new AreaPreparedEventArgs(false));
                }

                //Send the AreaUpdated events
                if (damage != Rectangle.Zero)
                {
                    AreaUpdated?.Invoke(this, new AreaUpdatedEventArgs(damage));
                    damage = Rectangle.Zero;
                }

                //Send the Completed event
                if (notify_completed)
                {
                    notify_completed = false;
                    OnCompleted();
                }

                return(false);
            });
        }
Beispiel #21
0
 /// <summary>
 /// Finaliza a leitura da assincrona.
 /// </summary>
 /// <param name="asyncResult"></param>
 /// <returns></returns>
 public override int EndRead(IAsyncResult asyncResult)
 {
     return(_stream.EndRead(asyncResult));
 }