本文整理匯總了C#中System.Threading.Overlapped.Pack方法的典型用法代碼示例。如果您正苦於以下問題:C# Overlapped.Pack方法的具體用法?C# Overlapped.Pack怎麽用?C# Overlapped.Pack使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Overlapped
的用法示例。
在下文中一共展示了Overlapped.Pack方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: HttpResponseStreamAsyncResult
internal unsafe HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback, byte[] buffer, int offset, int size, bool chunked, bool sentHeaders) : base(asyncObject, userState, callback)
{
this.m_SentHeaders = sentHeaders;
Overlapped overlapped = new Overlapped {
AsyncResult = this
};
if (size == 0)
{
this.m_DataChunks = null;
this.m_pOverlapped = overlapped.Pack(s_IOCallback, null);
}
else
{
this.m_DataChunks = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK[chunked ? 3 : 1];
object[] userData = new object[1 + this.m_DataChunks.Length];
userData[this.m_DataChunks.Length] = this.m_DataChunks;
int num = 0;
byte[] arr = null;
if (chunked)
{
arr = ConnectStream.GetChunkHeader(size, out num);
this.m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[0].BufferLength = (uint) (arr.Length - num);
userData[0] = arr;
this.m_DataChunks[1] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[1].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[1].BufferLength = (uint) size;
userData[1] = buffer;
this.m_DataChunks[2] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[2].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[2].BufferLength = (uint) NclConstants.CRLF.Length;
userData[2] = NclConstants.CRLF;
}
else
{
this.m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
this.m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
this.m_DataChunks[0].BufferLength = (uint) size;
userData[0] = buffer;
}
this.m_pOverlapped = overlapped.Pack(s_IOCallback, userData);
if (chunked)
{
this.m_DataChunks[0].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(arr, num);
this.m_DataChunks[1].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
this.m_DataChunks[2].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(NclConstants.CRLF, 0);
}
else
{
this.m_DataChunks[0].pBuffer = (byte*) Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
}
}
}
開發者ID:pritesh-mandowara-sp,項目名稱:DecompliedDotNetLibraries,代碼行數:54,代碼來源:HttpResponseStreamAsyncResult.cs
示例2: AsyncFileStream_AsyncResult
internal AsyncFileStream_AsyncResult( AsyncCallback userCallback, Object stateObject, bool isWrite )
{
m_userCallback = userCallback;
m_userStateObject = stateObject;
m_waitHandle = new ManualResetEvent( false );
m_isWrite = isWrite;
Overlapped overlapped = new Overlapped( 0, 0, IntPtr.Zero, this );
m_overlapped = overlapped.Pack( s_callback, null );
}
示例3: Reset
internal void Reset(uint size) {
if (size == m_Size)
{
return;
}
if (m_Size != 0)
{
Overlapped.Free(m_pOverlapped);
}
m_Size = size;
if (size == 0)
{
m_pOverlapped = null;
m_MemoryBlob = null;
m_BackingBuffer = null;
return;
}
m_BackingBuffer = new byte[checked((int) size)];
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = this;
m_pOverlapped = overlapped.Pack(s_IOCallback, m_BackingBuffer);
m_MemoryBlob = (UnsafeNclNativeMethods.HttpApi.HTTP_SSL_CLIENT_CERT_INFO*) Marshal.UnsafeAddrOfPinnedArrayElement(m_BackingBuffer, 0);
}
示例4: WritePipeOverlapped
public void WritePipeOverlapped(int ifaceIndex, byte pipeID, byte[] buffer,
int offset, int bytesToWrite, WinUSBAsyncResult result)
{
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = result;
unsafe
{
NativeOverlapped* pOverlapped = null;
uint bytesWritten;
pOverlapped = overlapped.Pack(_PipeIOCallback, buffer);
bool success;
// Buffer is pinned already by overlapped.Pack
fixed (byte* pBuffer = buffer)
{
success = NativeInterface.WinUsb_WritePipe(_InterfaceHandle(ifaceIndex), pipeID,
pBuffer + offset, (uint)bytesToWrite, out bytesWritten, pOverlapped);
}
_HandleOverlappedAPI(success, "Failed to asynchronously write pipe on WinUSB device.",
pOverlapped, result, (int)bytesWritten);
}
}
示例5: Send
//--------------------------------------------------------------------------
// Data transmission
//--------------------------------------------------------------------------
public override bool Send(NxtMessage msg)
{
bool fResult = false;
if (this.IsOpen)
{
try {
lock (this.msgReplyTargets)
{
this.msgReplyTargets.Add(msg);
}
byte[] rgbToSend = msg.DataForBluetoothTransmission;
int cbSent = 0;
unsafe
{
EventWaitHandle asyncWriteCompleteEvent = new EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
Overlapped overlappedWrite = new Overlapped(-1, -1, asyncWriteCompleteEvent.SafeWaitHandle.DangerousGetHandle(), null);
NativeOverlapped* pNativeOverlappedWrite = overlappedWrite.Pack(null, rgbToSend);
try
{
bool fSuccess = WriteFile(this.hSerialPort, rgbToSend, rgbToSend.Length, out cbSent, new IntPtr(pNativeOverlappedWrite));
if (!fSuccess)
{
int err = Marshal.GetLastWin32Error();
if (ERROR_IO_PENDING == err)
{
asyncWriteCompleteEvent.WaitOne();
}
else
ThrowWin32Error(err);
}
}
finally
{
System.Threading.Overlapped.Free(pNativeOverlappedWrite);
}
}
msg.NoteSent();
fResult = true;
}
catch (Exception)
{
}
}
return fResult;
}
示例6: ConnectNamedPipe
public unsafe static bool ConnectNamedPipe(IntPtr hNamedPipe, // handle to named pipe
Overlapped lpOverlapped // overlapped structure
)
{
NativeOverlapped* overlappedPack = lpOverlapped.Pack(null, null);
bool fRet = false;
if (overlappedPack != null)
{
fRet = ConnectNamedPipe(hNamedPipe, overlappedPack);
Overlapped.Free(overlappedPack);
}
return fRet;
}
示例7: BeginWriteCore
private unsafe FileStreamAsyncResult BeginWriteCore(byte[] bytes, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
{
NativeOverlapped* overlappedPtr;
FileStreamAsyncResult ar = new FileStreamAsyncResult {
_handle = this._handle,
_userCallback = userCallback,
_userStateObject = stateObject,
_isWrite = true
};
ManualResetEvent event2 = new ManualResetEvent(false);
ar._waitHandle = event2;
Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, ar);
if (userCallback != null)
{
overlappedPtr = overlapped.Pack(IOCallback, bytes);
}
else
{
overlappedPtr = overlapped.UnsafePack(null, bytes);
}
ar._overlapped = overlappedPtr;
if (this.CanSeek)
{
long length = this.Length;
if (this._exposedHandle)
{
this.VerifyOSHandlePosition();
}
if ((this._pos + numBytes) > length)
{
this.SetLengthCore(this._pos + numBytes);
}
overlappedPtr->OffsetLow = (int) this._pos;
overlappedPtr->OffsetHigh = (int) (this._pos >> 0x20);
this.SeekCore((long) numBytes, SeekOrigin.Current);
}
int hr = 0;
if ((this.WriteFileNative(this._handle, bytes, offset, numBytes, overlappedPtr, out hr) == -1) && (numBytes != -1))
{
if (hr == 0xe8)
{
ar.CallUserCallback();
return ar;
}
if (hr == 0x3e5)
{
return ar;
}
if (!this._handle.IsClosed && this.CanSeek)
{
this.SeekCore(0L, SeekOrigin.Current);
}
if (hr == 0x26)
{
__Error.EndOfFile();
return ar;
}
__Error.WinIOError(hr, string.Empty);
}
return ar;
}
示例8: BeginWrite
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
SerialAsyncResult ar = new SerialAsyncResult(this);
ar.Init(state, callback, false);
Overlapped ov = new Overlapped(0, 0, ar.AsyncWaitHandle.Handle.ToInt32(), ar);
unsafe { fixed (byte* data = &buffer[0]) {
int write = 0;
NativeOverlapped* nov = ov.Pack(m_IOCompletionCallback);
WriteFile(m_hFile, data, count, &write, nov);
} }
if (GetLastError() == ERROR_IO_PENDING)
return ar;
else
throw new Exception("Unable to initialize write. Errorcode: " + GetLastError().ToString());
}
示例9: HttpResponseStreamAsyncResult
internal HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback, byte[] buffer, int offset, int size, bool chunked, bool sentHeaders): base(asyncObject, userState, callback){
m_SentHeaders = sentHeaders;
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = this;
if (size == 0) {
m_DataChunks = null;
m_pOverlapped = overlapped.Pack(s_IOCallback, null);
}
else {
m_DataChunks = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK[chunked ? 3 : 1];
GlobalLog.Print("HttpResponseStreamAsyncResult#" + ValidationHelper.HashString(this) + "::.ctor() m_pOverlapped:0x" + ((IntPtr)m_pOverlapped).ToString("x8"));
object[] objectsToPin = new object[1 + m_DataChunks.Length];
objectsToPin[m_DataChunks.Length] = m_DataChunks;
int chunkHeaderOffset = 0;
byte[] chunkHeaderBuffer = null;
if (chunked) {
chunkHeaderBuffer = ConnectStream.GetChunkHeader(size, out chunkHeaderOffset);
m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
m_DataChunks[0].BufferLength = (uint)(chunkHeaderBuffer.Length - chunkHeaderOffset);
objectsToPin[0] = chunkHeaderBuffer;
m_DataChunks[1] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
m_DataChunks[1].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
m_DataChunks[1].BufferLength = (uint)size;
objectsToPin[1] = buffer;
m_DataChunks[2] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
m_DataChunks[2].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
m_DataChunks[2].BufferLength = (uint)NclConstants.CRLF.Length;
objectsToPin[2] = NclConstants.CRLF;
}
else {
m_DataChunks[0] = new UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK();
m_DataChunks[0].DataChunkType = UnsafeNclNativeMethods.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
m_DataChunks[0].BufferLength = (uint)size;
objectsToPin[0] = buffer;
}
// This call will pin needed memory
m_pOverlapped = overlapped.Pack(s_IOCallback, objectsToPin);
if (chunked)
{
m_DataChunks[0].pBuffer = (byte*)(Marshal.UnsafeAddrOfPinnedArrayElement(chunkHeaderBuffer, chunkHeaderOffset));
m_DataChunks[1].pBuffer = (byte*)(Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset));
m_DataChunks[2].pBuffer = (byte*)(Marshal.UnsafeAddrOfPinnedArrayElement(NclConstants.CRLF, 0));
}
else
{
m_DataChunks[0].pBuffer = (byte*)(Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset));
}
}
}
示例10: WriteDirect
public override int WriteDirect(Page start, long pagePosition, int pagesToWrite)
{
if (_fileInfo.Extension == ".voron" && pagePosition > 1)
{
}
var position = pagePosition * PageSize;
var toWrite = pagesToWrite * PageSize;
var overlapped = new Overlapped()
{
OffsetLow = (int)(position & 0xffffffff),
OffsetHigh = (int)(position >> 32),
};
var nativeOverlapped = overlapped.Pack(null, null);
try
{
var startWrite = start.Base;
while (toWrite != 0)
{
int written;
if (NativeFileMethods.WriteFile(_safeFileHandle, startWrite, toWrite, out written, nativeOverlapped) == false)
{
throw new Win32Exception();
}
toWrite -= written;
startWrite += written;
}
return toWrite;
}
finally
{
Overlapped.Unpack(nativeOverlapped);
Overlapped.Free(nativeOverlapped);
}
}
示例11: FileStreamAsyncResult
internal FileStreamAsyncResult(
int numBufferedBytes,
byte[] bytes,
#if USE_OVERLAPPED
SafeFileHandle handle,
#endif
AsyncCallback userCallback,
Object userStateObject,
bool isWrite)
{
_userCallback = userCallback;
_userStateObject = userStateObject;
_isWrite = isWrite;
_numBufferedBytes = numBufferedBytes;
#if USE_OVERLAPPED
_handle = handle;
#endif
// For Synchronous IO, I could go with either a callback and using
// the managed Monitor class, or I could create a handle and wait on it.
ManualResetEvent waitHandle = new ManualResetEvent(false);
_waitHandle = waitHandle;
#if USE_OVERLAPPED
// Create a managed overlapped class
// We will set the file offsets later
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = this;
// Pack the Overlapped class, and store it in the async result
var ioCallback = s_IOCallback; // cached static delegate; delay initialized due to it being SecurityCritical
if (ioCallback == null) s_IOCallback = ioCallback = new IOCompletionCallback(AsyncFSCallback);
_overlapped = overlapped.Pack(ioCallback, bytes);
Debug.Assert(_overlapped != null, "Did Overlapped.Pack or Overlapped.UnsafePack just return a null?");
#endif
}
示例12: Allocate
private UnsafeNclNativeMethods.HttpApi.HTTP_REQUEST* Allocate(uint size)
{
uint newSize = size != 0 ? size : RequestBuffer == null ? 4096 : Size;
if (m_NativeOverlapped != null && newSize != RequestBuffer.Length)
{
NativeOverlapped* nativeOverlapped = m_NativeOverlapped;
m_NativeOverlapped = null;
Overlapped.Free(nativeOverlapped);
}
if (m_NativeOverlapped == null)
{
SetBuffer(checked((int) newSize));
Overlapped overlapped = new Overlapped();
overlapped.AsyncResult = m_Result;
m_NativeOverlapped = overlapped.Pack(ListenerAsyncResult.IOCallback, RequestBuffer);
return (UnsafeNclNativeMethods.HttpApi.HTTP_REQUEST*) Marshal.UnsafeAddrOfPinnedArrayElement(RequestBuffer, 0);
}
return RequestBlob;
}
示例13: WaitForCommEvent
private unsafe int WaitForCommEvent()
{
int eventsOccurred = 0;
Win32API_Serial.SetCommMask(_safeHandle.Handle, Win32API_Serial.ALL_EVENTS);
AsyncSerialStream_AsyncResult asyncResult = new AsyncSerialStream_AsyncResult();
asyncResult._userCallback = null;
asyncResult._userStateObject = null;
asyncResult._isWrite = false;
asyncResult._oldTimeout = -1;
asyncResult._numBufferedBytes = 0;
ManualResetEvent waitHandle = new ManualResetEvent(false);
asyncResult._waitHandle = waitHandle;
Overlapped overlapped = new Overlapped(0, 0, 0, asyncResult);
NativeOverlapped* intOverlapped = overlapped.Pack(IOCallback);
if (Win32API_Serial.WaitCommEvent(_safeHandle.Handle, ref eventsOccurred, intOverlapped) == false)
{
int hr = Marshal.GetLastWin32Error();
if (hr == Win32API_Serial.ERROR_IO_PENDING)
{
int temp = Win32API_Serial.WaitForSingleObject(waitHandle.Handle, -1);
if(temp == 0) // no error
return eventsOccurred;
else
Resources.WinIOError();
}
else
Resources.WinIOError();
}
return eventsOccurred;
}
示例14: BeginWriteCore
unsafe private AsyncSerialStream_AsyncResult BeginWriteCore(byte[] array, int offset, int numBytes, AsyncCallback userCallback, Object stateObject, int timeout)
{
AsyncSerialStream_AsyncResult asyncResult = new AsyncSerialStream_AsyncResult();
asyncResult._userCallback = userCallback;
asyncResult._userStateObject = stateObject;
asyncResult._isWrite = true;
asyncResult._oldTimeout = WriteTimeout;
WriteTimeout = timeout;
ManualResetEvent waitHandle = new ManualResetEvent(false);
asyncResult._waitHandle = waitHandle;
Overlapped overlapped = new Overlapped(0, 0, 0, asyncResult);
NativeOverlapped* intOverlapped = overlapped.Pack(IOCallback);
int hr = 0;
int r = WriteFileNative(_safeHandle, array, offset, numBytes, intOverlapped, out hr);
if (r==-1)
{
if (hr != Win32API_Serial.ERROR_IO_PENDING)
{
if (hr == Win32API_Serial.ERROR_HANDLE_EOF)
Resources.EndOfFile();
else
Resources.WinIOError(hr, String.Empty);
}
}
return asyncResult;
}
示例15: BeginWriteCore
unsafe private SerialStreamAsyncResult BeginWriteCore(byte[] array, int offset, int numBytes, AsyncCallback userCallback, Object stateObject)
{
// Create and store async stream class library specific data in the
// async result
SerialStreamAsyncResult asyncResult = new SerialStreamAsyncResult();
asyncResult._userCallback = userCallback;
asyncResult._userStateObject = stateObject;
asyncResult._isWrite = true;
// For Synchronous IO, I could go with either a callback and using
// the managed Monitor class, or I could create a handle and wait on it.
ManualResetEvent waitHandle = new ManualResetEvent(false);
asyncResult._waitHandle = waitHandle;
// Create a managed overlapped class
// We will set the file offsets later
Overlapped overlapped = new Overlapped(0, 0, IntPtr.Zero, asyncResult);
// Pack the Overlapped class, and store it in the async result
NativeOverlapped* intOverlapped = overlapped.Pack(IOCallback, array);
asyncResult._overlapped = intOverlapped;
int hr = 0;
// queue an async WriteFile operation and pass in a packed overlapped
int r = WriteFileNative(array, offset, numBytes, intOverlapped, out hr);
// WriteFile, the OS version, will return 0 on failure. But
// my WriteFileNative wrapper returns -1. My wrapper will return
// the following:
// On error, r==-1.
// On async requests that are still pending, r==-1 w/ hr==ERROR_IO_PENDING
// On async requests that completed sequentially, r==0
// Note that you will NEVER RELIABLY be able to get the number of bytes
// written back from this call when using overlapped IO! You must
// not pass in a non-null lpNumBytesWritten to WriteFile when using
// overlapped structures!
if (r==-1)
{
if (hr != NativeMethods.ERROR_IO_PENDING)
{
if (hr == NativeMethods.ERROR_HANDLE_EOF)
InternalResources.EndOfFile();
else
InternalResources.WinIOError(hr, String.Empty);
}
}
return asyncResult;
}