public void SendRawMessage(string message, params object[] format) { try { if (NetworkStream == null) { OnNetworkError(new SocketErrorEventArgs(SocketError.NotConnected)); return; } message = string.Format(message, format); var data = Encoding.GetBytes(message + "\r\n"); //lock (NetworkStream) //{ // if (!IsWriting) // { // IsWriting = true; // Console.WriteLine("Wrote {0}", message); // NetworkStream.BeginWrite(data, 0, data.Length, MessageSent, message); // } // else // { lock (NetworkStream) { Console.WriteLine("Enqueued {0}", message); WriteQueue.Enqueue(message); } // } //} } catch { } }
async ReusableTask WaitForPendingWrites() { var tcs = new ReusableTaskCompletionSource <bool>(); WriteQueue.Enqueue(new BufferedIO(null, -1, null, -1, tcs)); await tcs.Task; }
/// <summary> /// Send a raw IRC message. Behaves like /quote in most IRC clients. /// </summary> public void SendRawMessage(string message, params object[] format) { if (NetworkStream == null) { OnNetworkError(new SocketErrorEventArgs(SocketError.NotConnected)); return; } message = string.Format(message, format); var data = Encoding.GetBytes(message + "\r\n"); if (!IsWriting) { IsWriting = true; try { NetworkStream.BeginWrite(data, 0, data.Length, MessageSent, message); } catch (IOException e) { OnError(new Events.ErrorEventArgs(e)); } } else { WriteQueue.Enqueue(message); } }
/// <summary> /// Send a raw IRC message. Behaves like /quote in most IRC clients. /// </summary> public void SendRawMessage(string message, params object[] format) { if (NetworkStream == null) { OnNetworkError(new SocketErrorEventArgs(SocketError.NotConnected)); return; } message = string.Format(message, format); var data = Encoding.GetBytes(message + "\r\n"); try { if (!IsWriting) { IsWriting = true; NetworkStream.BeginWrite(data, 0, data.Length, MessageSent, message); } else { WriteQueue.Enqueue(message); } } catch (Exception ex) { if (this.SocketConnectionError == null) { throw ex; } this.OnSocketConnectionError(new SocketUnhandledExceptionEventArgs(ex)); } }
public static void Write(CollectObject objIn, string runId) { if (objIn != null && runId != null) { WriteQueue.Enqueue(new WriteObject(objIn, runId)); } }
async Task WaitForPendingWrites() { TaskCompletionSource <bool> flushed = new TaskCompletionSource <bool>(); WriteQueue.Enqueue(new BufferedIO(null, -1, null, -1, flushed)); await flushed.Task; }
/// <summary> /// 添加一个待发送消息 /// </summary> /// <param name="message"></param> public void AddMessage(T message) { lock (syncroot) { WriteQueue.Enqueue(message); } }
public static void Write(object o) { Console.Write(o); SpinUp(); WriteQueue.Enqueue(o ?? "null"); //#error reimpl writing and io error handling, threaded }
protected internal Task EnqueueOperation(Func <Task> writeAsync) { if (IsAlive) { // Only enqueue new writes if the connection is alive return(WriteQueue.Enqueue(writeAsync)); } return(TaskAsyncHelper.Empty); }
protected virtual internal Task EnqueueOperation(Func <object, Task> writeAsync, object state) { if (!IsAlive) { return(TaskAsyncHelper.Empty); } // Only enqueue new writes if the connection is alive return(WriteQueue.Enqueue(writeAsync, state)); }
protected virtual internal Task EnqueueOperation(Func<object, Task> writeAsync, object state) { if (!IsAlive) { return TaskAsyncHelper.Empty; } // Only enqueue new writes if the connection is alive Task writeTask = WriteQueue.Enqueue(writeAsync, state); _lastWriteTask = writeTask; return writeTask; }
internal async ReusableTask WriteAsync(ITorrentData manager, long offset, byte[] buffer, int count) { if (count < 1) { throw new ArgumentOutOfRangeException(nameof(count), $"Count must be greater than zero, but was {count}."); } Interlocked.Add(ref pendingWrites, count); await IOLoop; int pieceIndex = (int)(offset / manager.PieceLength); long pieceStart = (long)pieceIndex * manager.PieceLength; long pieceEnd = pieceStart + manager.PieceLength; if (!IncrementalHashes.TryGetValue(ValueTuple.Create(manager, pieceIndex), out IncrementalHashData incrementalHash) && offset == pieceStart) { incrementalHash = IncrementalHashes[ValueTuple.Create(manager, pieceIndex)] = IncrementalHashCache.Dequeue(); incrementalHash.NextOffsetToHash = (long)manager.PieceLength * pieceIndex; } if (incrementalHash != null) { // Incremental hashing does not perform proper bounds checking to ensure // that pieces are correctly incrementally hashed even if 'count' is greater // than the PieceLength. This should never happen under normal operation, but // unit tests do it for convenience sometimes. Keep things safe by cancelling // incremental hashing if that occurs. if ((incrementalHash.NextOffsetToHash + count) > pieceEnd) { IncrementalHashes.Remove(ValueTuple.Create(manager, pieceIndex)); } else if (incrementalHash.NextOffsetToHash == offset) { incrementalHash.Hasher.TransformBlock(buffer, 0, count, buffer, 0); incrementalHash.NextOffsetToHash += count; } } if (WriteLimiter.TryProcess(count)) { Interlocked.Add(ref pendingWrites, -count); Write(manager, offset, buffer, count); } else { var tcs = new ReusableTaskCompletionSource <bool> (); WriteQueue.Enqueue(new BufferedIO(manager, offset, buffer, count, tcs)); await tcs.Task; } }
/// <summary> /// Queues an error into the backup/retry queue /// </summary> /// <remarks>These will be written to the store when we're able to connect again</remarks> public void QueueError(Error e) { // try and rollup in the queue, to save space foreach (var err in WriteQueue.Where(err => e.ErrorHash == err.ErrorHash)) { err.DuplicateCount++; return; } // only queue if we're under the cap if (WriteQueue.Count < BackupQueueSize) { WriteQueue.Enqueue(e); } // spin up the retry mechanism BeginRetry(); }
public async Task SendRawMessage(string message, params object[] format) { message = string.Format(message, format); lock (WriteQueue) { if (IsWriting) { WriteQueue.Enqueue(message); return; } IsWriting = true; } var data = Encoding.GetBytes(message + "\r\n"); try { await NetworkStream.WriteAsync(data, 0, data.Length); } catch (Exception e) { if (NetworkStream == null) { return; } OnNetworkError(new UnhandledExceptionEventArgs(e, true)); Quit(); return; } OnRawMessageSent(new RawMessageEventArgs(message, true)); lock (WriteQueue) { IsWriting = false; } if (WriteQueue.TryDequeue(out message)) { await SendRawMessage(message); } }
public void WriteCmd(object obj) { Log(Name, string.Format("向消息队列添加一个消息,时间:{0}", DataTime2String(DateTime.Now))); WriteQueue.Enqueue(obj); }
public override void Send(INetPacket p) { WriteQueue.Enqueue(p); }