public void SendPiece(IO.Stream ostream, int pieceId, int begin, int length, IO.Stream istream, PeerFinishedPieceTransfer callback, object state) { SendMessageHeader(ostream, PeerMessage.Piece, 8 + length); WriteInt(ostream, pieceId); WriteInt(ostream, begin); byte[] writeData = new byte[length]; istream.Read(writeData, 0, length); object[] objs = new object[4]; objs[0] = ostream; objs[1] = callback; objs[2] = state; objs[3] = length; ostream.BeginWrite(writeData, 0, writeData.Length, new System.AsyncCallback(OnWriteFinished), (object)objs); }
private void OnWriteFinished(System.IAsyncResult ar) { object[] objs = (object[])ar.AsyncState; IO.Stream ostream = (IO.Stream)objs[0]; PeerFinishedPieceTransfer callback = (PeerFinishedPieceTransfer)objs[1]; object state = objs[2]; try { ostream.EndWrite(ar); this.upthrottle.AddData((int)objs[3]); Config.LogDebugMessage("Piece sent: " + (int)objs[3]); callback(state, true); } catch (System.Exception) { callback(state, false); } }