by Martin Plante [plantem at xceedsoft dot com] posted on 2005/01/06 |
|
I found a problem with SecureNetworkStream.BeginWrite. If the callback passed to "Socket.BeginSend" is called quickly, it may end-up calling SecureNetworkStream.EndWrite BEFORE the following line in BeginWrite gets called. This results in WriteResult being set back to null BEFORE "WriteResult.AsyncResult" is returned, which results in a NullReferenceException. The fix is to work with a local copy of WriteResult within BeginWrite.
Before:
WriteResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);
Array.Copy(buffer, offset, WriteResult.Buffer, 0, size);
try {
Socket.BeginSend(WriteResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
return WriteResult.AsyncResult;
} catch {
throw new IOException();
}
After:
TransferItem localResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);
WriteResult = localResult;
Array.Copy(buffer, offset, localResult.Buffer, 0, size);
try {
Socket.BeginSend(localResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
return localResult.AsyncResult;
} catch {
throw new IOException();
}
Makes sense?
|