// Fake implementation of the Services interface methods public IAsyncResult BeginGetDeviceAddress(int devId, AsyncCallback callback, object state) { GenericAsyncResult <String> ar = new GenericAsyncResult <String>(callback, state, false); new Timer((_) => ar.SetResult("device42"), null, 1000, Timeout.Infinite); return(ar); }
//APM implementation of BeginCheckDeviceVersion public IAsyncResult BeginCheckDeviceVersion(Services svc, int devId, AsyncCallback cb, object state) { AsyncCallback onGetDeviceAddress = null, onGetVersionFromDevice = null, onGetStoredVersion = null; var gar = new GenericAsyncResult <bool>(cb, state, false); int stateCount = 2; //to control completion int devVer = -1, stoVer = -1; onGetDeviceAddress = (_ar) => { string devAddr = svc.EndGetDeviceAddress(_ar); svc.BeginGetVersionFromDevice(devAddr, onGetVersionFromDevice, null); }; onGetVersionFromDevice = (_ar) => { devVer = svc.EndGetVersionFromDevice(_ar); if (Interlocked.Decrement(ref stateCount) == 0) { gar.SetResult(devVer == stoVer); } }; onGetStoredVersion = (_ar) => { stoVer = svc.EndGetStoredVersion(_ar); if (Interlocked.Decrement(ref stateCount) == 0) { gar.SetResult(devVer == stoVer); } }; svc.BeginGetDeviceAddress(devId, onGetDeviceAddress, null); svc.BeginGetStoredVersion(devId, onGetStoredVersion, null); return(gar); }
private static AsyncCallback GenerateBeginReadCallback(String commandBuffer, byte [] buffer, Stream input, Stream output, Logger log, GenericAsyncResult <Object> asyncResult, string key, int count) { return((result) => { try { int bytesRead = input.EndRead(result); if (result.CompletedSynchronously) { count++; } else { count = 0; } commandBuffer += System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); if (commandBuffer.Contains(EMPTY_LINE)) { MESSAGE_HANDLERS [key](commandBuffer, buffer, input, output, log, asyncResult); } else if (count < RECURSION_LIMIT) { input.BeginRead(buffer, 0, buffer.Length, GenerateBeginReadCallback(commandBuffer, buffer, input, output, log, asyncResult, key, count), null); } } catch (IOException e) { asyncResult.OnComplete(null, e); log.LogMessage(String.Format("Handler - Connection closed by client {0}", e)); } }); }
public IAsyncResult BeginGetVersionFromDevice(String addr, AsyncCallback callback, object state) { GenericAsyncResult <int> ar = new GenericAsyncResult <int>(callback, state, false); new Timer((_) => ar.SetResult(42), null, 100, Timeout.Infinite); return(ar); }
public IAsyncResult BeginGetStoredVersion(int devId, AsyncCallback callback, object state) { GenericAsyncResult <int> ar = new GenericAsyncResult <int>(callback, state, false); new Timer((_) => ar.SetResult(42), null, 200, Timeout.Infinite); return(ar); }
private static void ProcessRun(String commandBuffer, byte [] buffer, Stream input, Stream output, Logger log, GenericAsyncResult <Object> asyncResult) { int commandDataLength = commandBuffer.IndexOf(EMPTY_LINE); int commandHeaderLength; String message = commandBuffer.Substring(0, commandDataLength); commandHeaderLength = message.IndexOf('\n'); if (commandHeaderLength < 0) { commandHeaderLength = message.Length; } string command = message.Substring(0, commandHeaderLength).Trim(); message = message.Substring(commandHeaderLength).TrimStart(); commandBuffer = commandBuffer.Substring(commandDataLength + EMPTY_LINE.Length); log.LogMessage("Handler - " + command); if (!MESSAGE_HANDLERS.ContainsKey(command)) { log.LogMessage("Handler - Unknown message type. Servicing ending."); asyncResult.OnComplete(null, null); return; } MESSAGE_HANDLERS [command](message, buffer, input, output, log, asyncResult); }
// begin take activating a timeout public IAsyncResult BeginTake(int timeout, CancellationToken ctk, AsyncCallback ucb, object ustate) { GenericAsyncResult <T> gar = new GenericAsyncResult <T>(ucb, ustate, false); filledSlots.BeginWaitEx(timeout, ctk, (ar) => { try { if (!filledSlots.EndWaitEx(ar)) { // complete request due to timeout gar.OnComplete(null, null); return; } // complete request with success T item; lock (room) { item = room[takeIdx++ % queueSize]; } freeSlots.Release(); gar.OnComplete(item, null); } catch (Exception ex) { gar.OnComplete(null, ex); } }, null); return(gar); }
//--- // asynchronous APM interface //--- // begin put activating a timeout public IAsyncResult BeginPut(T item, int timeout, CancellationToken ctk, AsyncCallback ucb, object ustate) { GenericAsyncResult <bool> gar = new GenericAsyncResult <bool>(ucb, ustate, false); freeSlots.BeginWaitEx(timeout, ctk, (ar) => { try { if (!freeSlots.EndWaitEx(ar)) { // complete put request with timeout gar.OnComplete(false, null); return; } // wait succeeded lock (room) { room[putIdx++ % queueSize] = item; } filledSlots.Release(); // complete put request with success gar.OnComplete(true, null); } catch (Exception ex) { // complete put request with exception gar.OnComplete(false, ex); } }, null); return(gar); }
public IAsyncResult BeginComputeHash(Stream s, AsyncCallback callback, object state) { var h = SHA1.Create(); var buffer = new byte[4 * 1024 * 1024]; var result = new GenericAsyncResult <byte[]>(callback, state, synchCompletion: false); AsyncCallback cb = null; cb = ar => { Console.Write('.'); try { var readBytes = s.EndRead(ar); if (readBytes != 0) { h.TransformBlock(buffer, 0, readBytes, buffer, 0); s.BeginRead(buffer, 0, buffer.Length, cb, null); } else { h.TransformFinalBlock(buffer, 0, 0); result.OnComplete(h.Hash, null); h.Dispose(); } } catch (Exception e) { result.OnComplete(null, e); h.Dispose(); return; } }; s.BeginRead(buffer, 0, buffer.Length, cb, null); return(result); }
// // File copy using APM asynchronous read and synchronous write operations. // public static IAsyncResult BeginCopyAsync(Stream src, Stream dst, AsyncCallback cb, object state) { GenericAsyncResult <long> gar = new GenericAsyncResult <long>(cb, state, false); byte[] rdBuffer = new byte[BUFFER_SIZE], wrBuffer = new byte[BUFFER_SIZE]; long fileSize = 0; AsyncCallback onReadCompleted = null; onReadCompleted = delegate(IAsyncResult ar) { int bytesRead = 0; try { bytesRead = src.EndRead(ar); } catch (IOException ioex) { src.Close(); gar.OnComplete(0, ioex); return; } if (bytesRead > 0) { // // Switch the buffers. // // The lock ensures that we can't process the completion of the // new read (writing the underlying buffer buffer) before write // the current read buffer. // byte[] tmp = rdBuffer; rdBuffer = wrBuffer; wrBuffer = tmp; lock (dst) { src.BeginRead(rdBuffer, 0, rdBuffer.Length, onReadCompleted, null); dst.Write(wrBuffer, 0, bytesRead); fileSize += bytesRead; } } else { // // We reach the EOF of the source stream. // We must ensure that the write of the last block is done, // before close the destination stream and complete the // underlying task. // src.Close(); lock (dst) { gar.OnComplete(fileSize, null); } dst.Close(); return; } }; // // Start the copy process starting the first asynchronous read. // src.BeginRead(rdBuffer, 0, rdBuffer.Length, onReadCompleted, null); return(gar); }
//--------------------------------------------------------------------- // APM //-- public IAsyncResult BeginWaitEx(int timeout, CancellationToken ctk, AsyncCallback ucb, object ustate) { bool result = true; lock (_lock) { if (currentPermits > 0) { currentPermits -= 1; } else if (timeout == 0 || ctk.IsCancellationRequested) { result = false; } else { var waiter = new ApmWaiter <Data>(new Data(), ucb, ustate, ctk, timeout, AsyncUnlink); DListNode.AddLast(waiters, waiter); waiter.Enable(); return(waiter.AsyncResult); } } // FromResult will invoke the async callbacks, so do it outside of the lock if (result) { return(GenericAsyncResult <bool> .FromResult(ucb, ustate, true, null, true)); } // synchronous completion due to timeout or cancellation return(ctk.IsCancellationRequested ? GenericAsyncResult <bool> .FromResult(ucb, ustate, false, new OperationCanceledException(ctk), true) : GenericAsyncResult <bool> .FromResult(ucb, ustate, false, null, true)); }
private IAsyncResult BeginListen(TcpListener server, Logger logger, CancellationToken cToken, AsyncCallback acb, object state) { GenericAsyncResult <bool> gar = new GenericAsyncResult <bool>(acb, state, false); int activeConnections = 0; TcpClient tcpClient = null; server.BeginAcceptTcpClient(onAcceptClient, null); void onAcceptClient(IAsyncResult ar) { tcpClient = server.EndAcceptTcpClient(ar); if (!cToken.IsCancellationRequested && Interlocked.Increment(ref activeConnections) < MAX_ACTIVE_CONNECTIONS) { server.BeginAcceptTcpClient(onAcceptClient, null); } BeginHandlerRunner(tcpClient, logger, onHandlerRunner, null); } void onHandlerRunner(IAsyncResult ar) { if (!cToken.IsCancellationRequested && Interlocked.Decrement(ref activeConnections) == MAX_ACTIVE_CONNECTIONS - 1) { server.BeginAcceptTcpClient(onAcceptClient, null); } else if (cToken.IsCancellationRequested && Interlocked.Decrement(ref activeConnections) == 0) { tcpClient.Close(); gar.SetResult(true); } } return(gar); }
// // Returns a completed instance of GenericAsyncResult<R> with the specified result. // public static IAsyncResult FromResult(AsyncCallback ucallback, object ustate, R result, Exception error, bool synchCompletion) { GenericAsyncResult <R> gar = new GenericAsyncResult <R>(ucallback, ustate, synchCompletion); gar.OnComplete(result, error); return(gar); }
public void EndRun(IAsyncResult res) { GenericAsyncResult <object> ar = (GenericAsyncResult <object>)res; Object obj = ar.Result; input.Close(); output.Close(); }
/// <summary> /// Performs request servicing. /// </summary> public IAsyncResult BeginRun(String commandBuffer, AsyncCallback callback, Object state) { GenericAsyncResult <Object> asyncResult = new GenericAsyncResult <Object>(callback, state); byte [] buffer = new byte [32]; Stream inputBase = input.BaseStream; inputBase.BeginRead(buffer, 0, buffer.Length, GenerateBeginReadCallback(commandBuffer, buffer, inputBase, output.BaseStream, log, asyncResult, "RUN", 0), null); return(asyncResult); }
IAsyncResult Service.BeginFindId(String name, String bdate, AsyncCallback cb, Object st) { GenericAsyncResult <int> ar = new GenericAsyncResult <int>(cb, st, false); new Timer( (_) => ar.SetResult(42), null, new Random(Environment.TickCount).Next(1000), Timeout.Infinite ); return(ar); }
IAsyncResult Service.BeginObtainAvatarUri(int userId, AsyncCallback cb, Object st) { GenericAsyncResult <Uri> ar = new GenericAsyncResult <Uri>(cb, st, false); new Timer( (_) => ar.SetResult(new Uri("http://google.com")), null, new Random(Environment.TickCount).Next(1000), Timeout.Infinite ); return(ar); }
public IAsyncResult BeginCreateRequest(string server, AsyncCallback callback, object state) { GenericAsyncResult<HttpWebRequest> asyncResult = new GenericAsyncResult<HttpWebRequest>(state, callback); var request = HttpWebRequest.Create(server + url) as HttpWebRequest; request.Method = method; asyncResult.Data = request; Task<Stream> task = Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, TaskCreationOptions.AttachedToParent); task.ContinueWith(k=> { Serialize(k.Result); asyncResult.Finished(); }); return asyncResult; }
public static IAsyncResult BeginGetUserAvatar(Service svc, string name, string bdate, AsyncCallback cb, object state) { GenericAsyncResult <Uri> gar = new GenericAsyncResult <Uri>(cb, state, false); AsyncCallback onObtainAvatarUri = (_ant) => { var uri = svc.EndObtainAvatarUri(_ant); gar.SetResult(uri); }; AsyncCallback onFindId = (_ant) => { int id = svc.EndFindId(_ant); svc.BeginObtainAvatarUri(id, onObtainAvatarUri, null); }; svc.BeginFindId(name, bdate, onFindId, null); return(gar); }
// // File copy using synchronous read and write operations. // public static IAsyncResult BeginCopySync(Stream src, Stream dst, AsyncCallback cb, object state) { byte[] buffer = new byte[BUFFER_SIZE]; long fileSize = 0; try { int bytesRead; while ((bytesRead = src.Read(buffer, 0, buffer.Length)) != 0) { dst.Write(buffer, 0, bytesRead); fileSize += bytesRead; } } finally { src.Close(); dst.Close(); } return(GenericAsyncResult <long> .FromResult(cb, state, fileSize, null, true)); }
public IAsyncResult BeginExecOnNearServer <S, R>(IAPMServices <S, R> s, Uri[] servers, S service, AsyncCallback ac, object state) { GenericAsyncResult <R> gar = new GenericAsyncResult <R>(ac, state, false); int got = 0, failures = 0; for (int i = 0; i < servers.Length; i++) { s.BeginPingServer(servers[i], onPingServer, null); } void onPingServer(IAsyncResult ar) { try { Uri uri = s.EndPingServer(ar); if (Interlocked.Exchange(ref got, 1) == 0) { s.BeginExecService(uri, service, onExecService, null); } } catch (Exception ex) { if (Interlocked.Increment(ref failures) == servers.Length) { gar.SetException(ex); } } } void onExecService(IAsyncResult ar) { try { gar.SetResult(s.EndExecService(ar)); } catch (Exception ex) { gar.SetException(ex); } } return(gar); }
protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state) { //first time thru (_reply channel is not initialized) so call the callback. if (_ReplyChannel == null) { _AcceptChannelAsyncResult = new GenericAsyncResult(true, state, timeout, callback, true) { AsyncState = state, Timeout = timeout }; callback?.Invoke(_AcceptChannelAsyncResult); } else { _AcceptChannelAsyncResult = new GenericAsyncResult(true, state, timeout, callback, false) { AsyncState = state, Timeout = timeout }; } return(_AcceptChannelAsyncResult); }
public IAsyncResult BeginWaitForRequest(TimeSpan timeout, AsyncCallback callback, object state) { ThrowIfDisposedOrNotOpen(); var asyncResult = new GenericAsyncResult(false, state, timeout, callback, false); var consumer = new EventingBasicConsumer(Model); consumer.Received += (a, b) => { asyncResult.Complete(); _CurrentRabbitMqMessage = b.AsRabbitMqMessage(); callback?.Invoke(asyncResult); }; Model.BasicConsume(ServiceConfiguration.ServiceQueue, false, consumer); return(asyncResult); }
public ApmWaiter(TData data, AsyncCallback ucb, object ustate, CancellationToken ctk, int timeout, Action <Waiter <TData> > unlink) : base(data, ctk, timeout, unlink) { AsyncResult = new GenericAsyncResult <bool>(ucb, ustate, synchCompletion: false); }
public bool EndWaitEx(IAsyncResult asyncResult) { GenericAsyncResult <bool> gbar = (GenericAsyncResult <bool>)asyncResult; return(gbar.Result); }
// end put public bool EndPut(IAsyncResult asyncResult) { GenericAsyncResult <bool> gar = (GenericAsyncResult <bool>)asyncResult; return(gar.Result); }
// end take public T EndTake(IAsyncResult asyncResult) { GenericAsyncResult <T> gar = (GenericAsyncResult <T>)asyncResult; return(gar.Result); }
/// <summary> /// Handles LIST_LOCATIONS messages. /// </summary> private static void ProcessListLocationsMessage(string line, byte [] buffer, Stream input, Stream output, Logger log, GenericAsyncResult <object> ar) { log.LogMessage(string.Format("::: LIST_LOCATIONS START IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); IPEndPoint [] fileLocations = Store.Instance.GetFileLocations(line); List <IPEndPoint> values = new List <IPEndPoint>(fileLocations); string aggregatedValues = ""; if (fileLocations.Length > 0) { aggregatedValues = values.Select((endpoint) => string.Format("{0}:{1}\n", endpoint.Address, endpoint.Port)) .Aggregate((a, b) => a + b); } aggregatedValues += EMPTY_LINE; byte [] message = System.Text.Encoding.ASCII.GetBytes(aggregatedValues); // Send response message. // The message is composed of multiple lines and is terminated by an empty one. // Each line has the following format // <ipAddress>:<portNumber> output.BeginWrite(message, 0, message.Length, (res) => { output.EndWrite(res); // End response and flush it. output.Flush(); ar.OnComplete(null, null); log.LogMessage(string.Format("::: LIST_LOCATIONS END IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); }, null); }
// // File copy using APM performs asynchronous reads and asynchronous write operations. // public static IAsyncResult BeginCopyAsync2(Stream src, Stream dst, AsyncCallback cb, object state) { GenericAsyncResult <long> gar = new GenericAsyncResult <long>(cb, state, false); long fileSize = 0; int pendingWrites = 1; // Account for the last read that reads 0 bytes. AsyncCallback onReadCompleted = null, onWriteCompleted = null; onReadCompleted = delegate(IAsyncResult ar) { int bytesRead = 0; try { bytesRead = src.EndRead(ar); } catch (IOException ioex) { src.Close(); gar.OnComplete(0, ioex); return; } byte[] readBuf = (byte[])ar.AsyncState; if (bytesRead > 0) { // // Start an asynchronous write, and then the next asynchronous read. // lock (dst) { pendingWrites++; dst.BeginWrite(readBuf, 0, bytesRead, onWriteCompleted, bytesRead); } // // Allocate a new buffer and start a new asynchronous read. // byte[] nextBuf = new byte[BUFFER_SIZE]; src.BeginRead(nextBuf, 0, nextBuf.Length, onReadCompleted, nextBuf); } else { // End of source file. src.Close(); lock (dst) { if (--pendingWrites == 0) { // // Last read is a zero-length read no writes are in progress. // The copy is completed. // dst.Close(); gar.OnComplete(fileSize, null); } } } }; onWriteCompleted = delegate(IAsyncResult ar) { try { dst.EndWrite(ar); } catch (IOException ioex) { dst.Close(); gar.OnComplete(0, ioex); return; } lock (dst) { fileSize += (int)ar.AsyncState; if (--pendingWrites == 0) { dst.Close(); gar.OnComplete(fileSize, null); } } }; byte[] firstBuf = new byte[BUFFER_SIZE]; src.BeginRead(firstBuf, 0, firstBuf.Length, onReadCompleted, firstBuf); return(gar); }
/// <summary> /// Handles LIST_FILES messages. /// </summary> private static void ProcessListFilesMessage(string commandBuffer, byte [] buffer, Stream input, Stream output, Logger log, GenericAsyncResult <object> ar) { // Request message does not have a payload. // Read end message mark (empty line) log.LogMessage(string.Format("::: LIST_FILES START IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); string [] trackedFiles = Store.Instance.GetTrackedFiles(); List <String> values = new List <string>(trackedFiles); string aggregatedValues = ""; if (trackedFiles.Length > 0) { aggregatedValues = values.Aggregate((a, b) => a + "\n" + b); } aggregatedValues += EMPTY_LINE; byte [] message = System.Text.Encoding.ASCII.GetBytes(aggregatedValues); // Send response message. // The message is composed of multiple lines and is terminated by an empty one. // Each line contains a name of a tracked file. output.BeginWrite(message, 0, message.Length, (res) => { output.EndWrite(res); // End response and flush it. output.Flush(); ar.OnComplete(null, null); log.LogMessage(string.Format("::: LIST_FILES END IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); }, null); }
/// <summary> /// Handles UNREGISTER messages. /// </summary> private static void ProcessUnregisterMessage(string unregister, byte [] buffer, Stream input, Stream output, Logger log, GenericAsyncResult <object> ar) { // Read message payload, terminated by an empty line. // Each payload line has the following format // <filename>:<ipAddress>:<portNumber> log.LogMessage(string.Format("::: UNREGISTER START IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); foreach (String line in unregister.Split('\n')) { string [] triple = line.Split(':'); if (triple.Length != 3) { log.LogMessage("Handler - Invalid UNREGISTER message:" + line); ar.OnComplete(null, null); return; } IPAddress ipAddress = IPAddress.Parse(triple [1]); ushort port; if (!ushort.TryParse(triple [2], out port)) { log.LogMessage("Handler - Invalid UNREGISTER message:" + line); ar.OnComplete(null, null); return; } Store.Instance.Unregister(triple [0], new IPEndPoint(ipAddress, port)); } ar.OnComplete(null, null); log.LogMessage(string.Format("::: UNREGISTER END IN THREAD {0}", Thread.CurrentThread.ManagedThreadId)); }