RCAClientLibrary.cpp
上传用户:hyz2004817
上传日期:2022-03-30
资源大小:226k
文件大小:29k
- #include "stdafx.h"
- BOOL
- WINAPI
- DecodeFromJPEGBuffer(
- BYTE * lpJpgBuffer,
- DWORD dwJpgBufferSize,
- BYTE** lppRgbBuffer,
- DWORD* lpdwWidth,
- DWORD* lpdwHeight,
- DWORD* lpdwNumberOfChannels
- )
- {
-
- BOOL bres;
- IJLERR jerr;
- DWORD dwWholeImageSize;
- BYTE* lpTemp = NULL;
- // Allocate the IJL JPEG_CORE_PROPERTIES structure.
- JPEG_CORE_PROPERTIES jcprops;
- bres = TRUE;
- __try
- {
- // Initialize the Intel(R) JPEG Library.
- jerr = ijlInit(&jcprops);
- if(IJL_OK != jerr)
- {
- bres = FALSE;
- __leave;
- }
-
- // Get information on the JPEG image
- // (i.e., width, height, and channels).
- jcprops.JPGFile = NULL;
- jcprops.JPGBytes = lpJpgBuffer;
- jcprops.JPGSizeBytes = dwJpgBufferSize;
- jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
- if(IJL_OK != jerr)
- {
- bres = FALSE;
- __leave;
- }
- // Set the JPG color space ... this will always be
- // somewhat of an educated guess at best because JPEG
- // is "color blind" (i.e., nothing in the bit stream
- // tells you what color space the data was encoded from).
- // However, in this example we assume that we are
- // reading JFIF files which means that 3 channel images
- // are in the YCbCr color space and 1 channel images are
- // in the Y color space.
- switch(jcprops.JPGChannels)
- {
- case 1:
- {
- jcprops.JPGColor = IJL_G;
- jcprops.DIBColor = IJL_RGB;
- jcprops.DIBChannels = 3;
- break;
- }
- case 3:
- {
- jcprops.JPGColor = IJL_YCBCR;
- jcprops.DIBColor = IJL_RGB;
- jcprops.DIBChannels = 3;
- break;
- }
- default:
- {
- // This catches everything else, but no
- // color twist will be performed by the IJL.
- jcprops.JPGColor = IJL_OTHER;
- jcprops.DIBColor = IJL_OTHER;
- jcprops.DIBChannels = jcprops.JPGChannels;
- break;
- }
- }
- // Compute size of desired pixel buffer.
- dwWholeImageSize = jcprops.JPGWidth * jcprops.JPGHeight *
- jcprops.DIBChannels;
- // Allocate memory to hold the decompressed image data.
- lpTemp = new BYTE [dwWholeImageSize];
- if(NULL == lpTemp)
- {
- bres = FALSE;
- __leave;
- }
- // Set up the info on the desired DIB properties.
- jcprops.DIBWidth = jcprops.JPGWidth;
- jcprops.DIBHeight = jcprops.JPGHeight;
- jcprops.DIBPadBytes = 0;
- jcprops.DIBBytes = lpTemp;
- // Now get the actual JPEG image data into the pixel buffer.
- jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
- if(IJL_OK != jerr)
- {
- bres = FALSE;
- __leave;
- }
- } // __try
- __finally
- {
- if(FALSE == bres)
- {
- if(NULL != lpTemp)
- {
- delete [] lpTemp;
- lpTemp = NULL;
- }
- }
- // Clean up the Intel(R) JPEG Library.
- ijlFree(&jcprops);
- *lpdwWidth = jcprops.DIBWidth;
- *lpdwHeight = jcprops.DIBHeight;
- *lpdwNumberOfChannels = jcprops.DIBChannels;
- *lppRgbBuffer = lpTemp;
- } // __finally
- return bres;
- } // DecodeFromJPEGBuffer()
- //----------------------------------------------------------
- // An example using the Intel(R) JPEG Library:
- // -- Encode Windows DIB to JPEG buffer.
- //----------------------------------------------------------
- BOOL WINAPI EncodeToJPEGBuffer(
- BYTE* lpRgbBuffer,
- DWORD dwWidth,
- DWORD dwHeight,
- BYTE** lppJpgBuffer,
- DWORD* lpdwJpgBufferSize,
- int iQuality
- )
- {
- BOOL bres;
- IJLERR jerr;
- DWORD dwRgbBufferSize;
- BYTE* lpTemp;
- // Allocate the IJL JPEG_CORE_PROPERTIES structure.
- JPEG_CORE_PROPERTIES jcprops;
- bres = TRUE;
- __try
- {
- // Initialize the Intel(R) JPEG Library.
- jerr = ijlInit(&jcprops);
- if(IJL_OK != jerr)
- {
- bres = FALSE;
- __leave;
- }
- dwRgbBufferSize = dwWidth * dwHeight * 3;
- lpTemp = new BYTE [dwRgbBufferSize];
- if(NULL == lpTemp)
- {
- bres = FALSE;
- __leave;
- }
- // Set up information to write from the pixel buffer.
- jcprops.DIBWidth = dwWidth;
- jcprops.DIBHeight = dwHeight; // Implies a bottom-up DIB.
- jcprops.DIBBytes = lpRgbBuffer;
- jcprops.DIBPadBytes = 0;
- jcprops.DIBChannels = 3;
- jcprops.DIBColor = IJL_RGB;
- jcprops.JPGWidth = dwWidth;
- jcprops.JPGHeight = dwHeight;
- jcprops.JPGFile = NULL;
- jcprops.JPGBytes = lpTemp;
- jcprops.JPGSizeBytes = dwRgbBufferSize;
- jcprops.JPGChannels = 3;
- jcprops.JPGColor = IJL_YCBCR;
- jcprops.JPGSubsampling = IJL_411; // 4:1:1 subsampling.
- jcprops.jquality = (iQuality>=0)&&(iQuality<=100) ? iQuality : 50; // Select "good" image quality
- // Write the actual JPEG image from the pixel buffer.
- jerr = ijlWrite(&jcprops,IJL_JBUFF_WRITEWHOLEIMAGE);
- if(IJL_OK != jerr)
- {
- bres = FALSE;
- __leave;
- }
- } // __try
- __finally
- {
- if(FALSE == bres)
- {
- if(NULL != lpTemp)
- {
- delete[] lpTemp;
- lpTemp = NULL;
- }
- }
- *lppJpgBuffer = lpTemp;
- *lpdwJpgBufferSize = jcprops.JPGSizeBytes;
- // Clean up the Intel(R) JPEG Library.
- ijlFree(&jcprops);
- }
- return bres;
- } // EncodeToJPEGBuffer()
- ////////////////////////////////////////
- //
- // SOCKET 异步 IO 帮助函数, 异步模型为 "事件选择模型"
- //
- // 参数:
- // SOCKET s:
- // 套接字 s 是一个已成功连接的套接字, 并至少使用 FD_READ 标志作为
- // 参数 lNetworkEvents 调用了 WSAEventSelect 成功地将套接字置为异步事件选择模型
- //
- // HANDLE hEvent:
- // 一个用于网络 IO 事件通知的事件句柄.
- //
- // char * buf:
- // 接收数据缓冲
- //
- // int len:
- // 接收数据缓冲总长度
- //
- // BOOL * bExit:
- // BOOL 变量的指针, 应用程序可以修改这个值. 这个函数调用 WaitForSingleObject 等待
- // dwMilliseconds 毫秒后, 将检测 bExit 的值, 若为 bExit 为 TRUE, 函数将立即返回.
- // 返回时是不管等待是否成功的,或说不论等待结果是 WAIT_TIMEOUT 或者 WAIT_OBJECT_0,
- // 都会返回, 只要 bExit 为 TRUE
- //
- //
- // DWORD dwMilliseconds:
- // 函数将使用这个值作为第二个参数,调用 WaitForSingleObject. 以毫秒为单位
- // INFINITE 为无限等待
- //
- // 返回值:
- // 若第一次调用接收函数时, 连接出现问题, 则返回 0
- //
- // 若第一次调用接收函数成功后,以后无论发生任何错误都返回一个整型值,
- // 描述已接收了多少数据
- //
- // 如果返回值与参数 len 相等, 说明成功发送出了所有数据, 若不等,可能是网络错误造成,
- // 也可能是 bExit 被设为 TRUE 后接收操作被强制中断
- INT
- WINAPI RCARecv_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- char* buf,
- const int len
- )
- {
- if( (buf == NULL) || (len==0) )
- return FALSE;
-
- int ret;
- DWORD dwret;
- int count = len;
- int index = 0;
- WSANETWORKEVENTS ns;
- while( count > 0 )
- {
- ret = recv( s, &(buf[index]), count, 0 );
- if( ret == SOCKET_ERROR )
- {
- // 如果发送错误,并且错误代码不是"被阻塞", 则返回 FALSE ( 发生了网络错误 )
- if( WSAGetLastError() != WSAEWOULDBLOCK )
- return len - count;
- } else if( ret == 0 )
- {
- return len - count ;
- } else
- {
- // 如果成功发送, 则更新缓冲偏移字节数和待发总字节数
- index += ret;
- count -= ret;
- continue;
- }
- dwret = WaitForMultipleObjects(
- 2, hEventArray, FALSE, INFINITE );
- switch ( dwret )
- {
- case WAIT_FAILED:
- return len - count;
-
- case WAIT_OBJECT_0:
- break;
- case WAIT_OBJECT_0 + 1:
- return len - count;
- case WAIT_TIMEOUT:
- break;
- }
- ret = WSAEnumNetworkEvents( s, hEventArray[0], &ns );
- if( ret == SOCKET_ERROR )
- {
- return len - count;
- }
-
- if( ns.lNetworkEvents & FD_READ )
- {
- if( ns.iErrorCode[ FD_READ_BIT] != 0 )
- {
- return len - count;
- }
- else
- {
- continue; // 回到循环开始处,再次接收数据(此时,绝对可以接收数据)
- }
- }
- if( ns.lNetworkEvents & FD_CLOSE )
- {
- return len - count;
- }
- }
-
- return len - count;
- }
- ////////////////////////////////////////
- //
- // SOCKET 异步 IO 帮助函数, 异步模型 事件选择模型
- //
- // 参数:
- // SOCKET s:
- // 套接字 s 是一个已成功连接的套接字, 并至少使用 FD_WRITE 标志作为
- // 参数 lNetworkEvents 调用了 WSAEventSelect 成功地将套接字置为异步事件选择模型
- //
- // HANDLE hEvent:
- // 一个用于网络 IO 事件通知的事件句柄.
- //
- // char * buf:
- // 待发数据缓冲
- //
- // int len:
- // 待发数据缓冲总长度
- //
- // BOOL * bExit:
- // BOOL 变量的指针, 应用程序可以修改这个值. 这个函数调用 WaitForSingleObject 等待
- // dwMilliseconds 毫秒后, 将检测 bExit 的值, 若为 bExit 为 TRUE, 函数将立即返回.
- // 返回时是不管等待是否成功的,或说不论等待结果是 WAIT_TIMEOUT 或者 WAIT_OBJECT_0,
- // 都会返回, 只要 bExit 为 TRUE
- //
- //
- // DWORD dwMilliseconds:
- // 函数将使用这个值作为第二个参数,调用 WaitForSingleObject. 以毫秒为单位
- // INFINITE 为无限等待
- //
- // 返回值:
- // 若第一次调用发送函数时, 连接出现问题, 则返回 0
- //
- // 若第一次调用发送函数成功后,以后无论发生任何错误都返回一个整型值,
- // 描述已发送了多少数据
- //
- // 如果返回值与参数 len 相等, 说明成功发送出了所有数据, 若不等,可能是网络错误造成,
- // 也可能是 bExit 被设为 TRUE 后发送操作被强制中断
- INT
- WINAPI RCASend_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- char* buf,
- const int len
- )
- {
- if( (buf == NULL) || (len==0) )
- return FALSE;
-
- int ret;
- int count = (int)len;
- int index =0;
- DWORD dwret;
- WSANETWORKEVENTS ns;
- while( count > 0 )
- {
-
- ret = send( s, &(buf[index]), count, 0 );
-
- if( ret == SOCKET_ERROR )
- {
- // 如果发送错误,并且错误代码不是"被阻塞", 则返回 FALSE ( 发生了网络错误 )
- if( WSAGetLastError() != WSAEWOULDBLOCK )
- return len - count;
- }
- else
- {
- // 如果成功发送, 则更新缓冲偏移字节数和待发总字节数
- cout << ret << endl;
- index += ret;
- count -= ret;
- continue;
- }
- // 到这里,说明没而待发缓冲区可供使用, 在 AsyncEvent IO 模型中, 可以等待事件通知的到来
- // 现在根据 dwMilliseconds 来待待事件通知
- dwret = WaitForMultipleObjects(
- 2, hEventArray, FALSE, INFINITE );
- switch( dwret )
- {
- case WAIT_FAILED:
- case WAIT_OBJECT_0 + 1:
- return len - count;
- case WAIT_OBJECT_0:
- break;
- case WAIT_TIMEOUT:
- continue;
- }
- ret = WSAEnumNetworkEvents( s, hEventArray[0], &ns );
- if( ns.lNetworkEvents & FD_WRITE )
- {
- if( ns.iErrorCode[ FD_WRITE_BIT] != 0 )
- return (len - count);
- else
- continue; // 回到前边,再次发送数据(此时,绝对可以发送数据)
- }
- if( ret == SOCKET_ERROR )
- return (len - count);
-
- if( ns.lNetworkEvents & FD_CLOSE )
- return (len - count);
- }
- return len - count;
- }
- ///////////////////////////////////////////
- //
- // 获得整个屏幕的象素信息
- //
- // 参数:
- //
- // pbmi[in,out] -- BITMAPINFO 结构指针, 函数成功调用后会填充这个结构,这个结构反映了象素信息
- //
- // x, y, w, h; 矩形的左上角座标, 以及宽高. 如果这些参数不合法,将被修整为合法参数
- //
- // pBits[in,out] -- void 指针, 函数调用成功后,pBits 将被象素值填充. 如果 pBits 为NULL,
- // 函数将修改 dwBufferSize 参数, 指明需要多少缓冲存放象素值
- //
- // dwBufferSize -- [in, out] 指明 pBits 的大小. 如果函数成功调用后, dwBufferSize 将会被修改,
- // 指明实际拷贝的象素字节数
- // 返回值:
- // BOOL 类型, 函数调用成功后, 将返回 TRUE; 失败,将返回 FALSE;
- //
- HBITMAP
- WINAPI GetDCPixel_BMP (
- HDC hdc,
- int x,
- int y,
- int w,
- int h,
- int zw,
- int zh,
- WORD wBitCount,
- VOID ** pBits,
- BITMAPINFOHEADER * pbmih, // 当色深为 8 时, 包含颜色表
- DWORD * pdwBitmapInfoSize
- )
- {
- int width = 0;
- int height;
- // 确定 DC 是而效的, 并获得 DC 的宽高(以像素为单位)
- width = GetDeviceCaps( hdc, HORZRES);
- if( width == 0 )
- return NULL;
- height = GetDeviceCaps( hdc, VERTRES);
-
- // 修整请求的矩形
- x = (x<0)||(x>(width-1)) ? 0 : x;
- x = (y<0)||(y>(height-1)) ? 0 : y;
- w = (w>(width-x)) || (w<1) ? width - x : w;
- h = (h>(height-y)) || (h<1) ? height - y : h;
- zw = zw <= 0 ? w : zw;
- zh = zh <= 0 ? h : zh;
- // 修整请求的色深
- if( wBitCount <= 8 )
- wBitCount = 8;
- else if( wBitCount <=16 )
- wBitCount = 16;
- else if( wBitCount <= 24 )
- wBitCount = 24;
- else if( wBitCount >=32 )
- wBitCount = 32;
- // 计算存储图象所需空间的大小
- //DWORD tmp = (((w * wBitCount) +31) & ~31) / 8 * h;
-
- if( pbmih != NULL )
- {
- if( wBitCount == 8 )
- {
- if( *pdwBitmapInfoSize < (sizeof(RGBQUAD)*256 + sizeof(BITMAPINFOHEADER) ))
- return NULL;
- }
- else
- {
- if( *pdwBitmapInfoSize < sizeof( BITMAPINFOHEADER) )
- return NULL;
- }
- }
- else
- {
- *pdwBitmapInfoSize = (sizeof(RGBQUAD)*256 + sizeof(BITMAPINFOHEADER) );
- return NULL;
- }
- HBITMAP bmp;
- PVOID pTmp = NULL;
- BITMAPINFO bmi;
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = zw;
- bmi.bmiHeader.biHeight = zh;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = wBitCount;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biSizeImage = 0;
- bmi.bmiHeader.biXPelsPerMeter = 0;
- bmi.bmiHeader.biYPelsPerMeter = 0;
- bmi.bmiHeader.biClrUsed = 0;
- bmi.bmiHeader.biClrImportant = 0;
- // create a DIB section bitmap;
- bmp = CreateDIBSection( hdc, &bmi, DIB_RGB_COLORS, &pTmp, NULL, 0 );
- if( bmp == NULL )
- return NULL;
- // set the out param pBits to image buffer pTmp;
- *pBits = pTmp;
- HDC hdcMem = CreateCompatibleDC( hdc );
- if( hdcMem == NULL)
- {
- DeleteObject(bmp);
- return NULL;
- }
-
- SelectObject( hdcMem, bmp );
- StretchBlt( hdcMem, 0, 0, zw, zh, hdc, x, y, w, h, SRCCOPY );
- //BitBlt( hdc, 0, 0,w, h, hdcMem, 0, 0, SRCCOPY );
- DIBSECTION ds;
- GetObject( bmp, sizeof(ds), (PVOID)&ds );
- //cout << ds.dsBmih.biClrUsed << endl;
- if( ds.dsBm.bmBitsPixel == 8 )
- {
-
- int clrGetNum;
- SelectObject( hdcMem, bmp );
- clrGetNum = GetDIBColorTable( hdcMem, 0, ds.dsBmih.biClrUsed,
- (RGBQUAD*)(sizeof(BITMAPINFOHEADER) +(char*)pbmih ) );
- if( 0 == clrGetNum )
- {
- DeleteDC( hdcMem );
- DeleteObject( bmp );
- return NULL;
- }
- }
- CopyMemory( (void*)pbmih, &(ds.dsBmih), sizeof(BITMAPINFOHEADER) );
- // 计算并输出图像所需存储空间的大小
- //cout << (((w * wBitCount) +31) & ~31) / 8 * h << endl;
- DeleteDC( hdcMem );
- return bmp;
- }
- VOID
- WINAPI InitResponseHead(
- PRCARESPONSEHEADER prresh,
- DWORD dwStatusCode,
- DWORD dwSize
- )
- {
- lstrcpy( prresh->rcaID, "RCA" );
- prresh->dwStatusCode = dwStatusCode;
- prresh->dwTotalBytes = dwSize;
- }
- char ___s[256];
- char * WINAPI WSAErrorString( int WSAError )
- {
- switch( WSAError )
- {
- case WSAEACCES: // 10013
- lstrcpy( ___s, "拒绝访问 10013" );
- break;
- case WSAEADDRINUSE: // 10048
- lstrcpy( ___s, "地址已经使用 10048" );
- break;
- case WSAEADDRNOTAVAIL: // 10049
- lstrcpy( ___s, "请求的地址无效 10049" );
- break;
- case WSAEAFNOSUPPORT: // 10047
- lstrcpy( ___s, "地址家族不支持 10047" );
- break;
- case WSAEALREADY: // 10037
- lstrcpy( ___s, "操作已经在进行中 10037" );
- break;
- case WSAECONNABORTED: // 10053
- lstrcpy( ___s, "软件原因连接被中断 10053" );
- break;
- case WSAECONNREFUSED: // 10061
- lstrcpy( ___s, "连接被拒绝 10061" );
- break;
- case WSAECONNRESET: // 10054
- lstrcpy( ___s, "连接被重置 10054" );
- break;
- case WSAEDESTADDRREQ: // 10039
- lstrcpy( ___s, "必须指定目的地址 10039" );
- break;
- case WSAEFAULT:
- lstrcpy( ___s, "地址错误 10014" );
- break;
- case WSAEHOSTDOWN:
- lstrcpy( ___s, "远程主机死机了( 蓝屏?) 10064" );
- break;
- case WSAEHOSTUNREACH:
- lstrcpy( ___s, "无法找到主机 10065" );
- break;
- case WSAEINPROGRESS:
- lstrcpy( ___s, "阻塞操作正在进行 10036" );
- break;
- case WSAEINTR:
- lstrcpy( ___s, "阻塞操作被打断 10004" );
- break;
- case WSAEINVAL:
- lstrcpy( ___s, "参数无效 10022" );
- break;
- case WSAEISCONN:
- lstrcpy( ___s, "套接字已经连接 10056" );
- break;
- case WSAEMFILE:
- lstrcpy( ___s, "打开的套接字太多 10024" );
- break;
- case WSAEMSGSIZE:
- lstrcpy( ___s, "消息太长 10040" );
- break;
- case WSAENETDOWN:
- lstrcpy( ___s, "网络系统, 网络接口, 本地网络有错误 10050" );
- break;
- case WSAENETRESET:
- lstrcpy( ___s, "保持活动的连接在操作中出现错误 10052" );
- break;
- case WSAENETUNREACH:
- lstrcpy( ___s, "网络无法到达 10051" );
- break;
- case WSAENOBUFS:
- lstrcpy( ___s, "没有可用的缓冲区 10055" );
- break;
- case WSAENOPROTOOPT:
- lstrcpy( ___s, "错误的协议选项 10042" );
- break;
- case WSAENOTCONN:
- lstrcpy( ___s, "套接字没有连接 10057" );
- break;
- case WSAENOTSOCK:
- lstrcpy( ___s, "套接字句柄无效 10038" );
- break;
- case WSAEOPNOTSUPP:
- lstrcpy( ___s, "操作不支持 10045" );
- break;
- case WSAEPFNOSUPPORT:
- lstrcpy( ___s, "协议族不支持 10046" );
- break;
- case WSAEPROCLIM:
- lstrcpy( ___s, "使用 WinSock 的进程太多 10067" );
- break;
- case WSAEPROTONOSUPPORT:
- lstrcpy( ___s, "协议不支持 10043" );
- break;
- case WSAEPROTOTYPE:
- lstrcpy( ___s, "协议类型错误 10041" );
- break;
- case WSAESHUTDOWN:
- lstrcpy( ___s, "套接字关闭后无法进行发送操作 10058" );
- break;
- case WSAESOCKTNOSUPPORT:
- lstrcpy( ___s, "套接字类型不支持 10044" );
- break;
- case WSAETIMEDOUT:
- lstrcpy( ___s, "连接超时 10060" );
- break;
- case WSATYPE_NOT_FOUND:
- lstrcpy( ___s, "指定的类没有发现 10109" );
- break;
- case WSAEWOULDBLOCK:
- lstrcpy( ___s, "资源暂时无法获得 10035" );
- break;
- case WSAHOST_NOT_FOUND:
- lstrcpy( ___s, "主机没有发现 11001" );
- break;
- case WSA_INVALID_HANDLE:
- lstrcpy( ___s, "无效句柄 (OS dependent)" );
- break;
- case WSA_INVALID_PARAMETER:
- lstrcpy( ___s, "无效参数 (OS dependent)" );
- break;
- // case WSAINVALIDPROCTABLE:
- // lstrcpy( ___s, "无效的过程表 (OS dependent)" );
- // break;
- // case WSAINVALIDPROVIDER:
- // lstrcpy( ___s, "无效的提供者版本 (OS dependent)" );
- // break;
- case WSA_IO_INCOMPLETE:
- lstrcpy( ___s, "重叠 I/O 事件对象不在通知状态 (OS dependent)" );
- break;
- case WSA_IO_PENDING:
- lstrcpy( ___s, "重叠操作将在过后完成 (OS dependent)" );
- break;
- case WSA_NOT_ENOUGH_MEMORY:
- lstrcpy( ___s, "内存不足 (OS dependent) " );
- break;
- case WSANOTINITIALISED:
- lstrcpy( ___s, "WSAStartup 函数没有成功的调用 10093" );
- break;
- case WSANO_DATA:
- lstrcpy( ___s, "请求的名字有效, 但没有请求的数据记录 11004" );
- break;
- case WSANO_RECOVERY:
- lstrcpy( ___s, "无法回复错误, 可能服务器数据库文件没有发现,也可能是服务器返回了错误 11003" );
- break;
- // case WSAPROVIDERFAILEDINIT:
- // lstrcpy( ___s, "无法初始化服务提供者 (OS dependent)" );
- // break;
- case WSASYSCALLFAILURE:
- lstrcpy( ___s, "系统调用失败 (OS dependent)" );
- break;
- case WSASYSNOTREADY:
- lstrcpy( ___s, "网络子系统错误 10091" );
- break;
- case WSATRY_AGAIN:
- lstrcpy( ___s, "Nonauthoritative host not found. please try again later 11002" );
- break;
- case WSAVERNOTSUPPORTED:
- lstrcpy( ___s, "请求的版本不支持 10092" );
- break;
- case WSAEDISCON:
- lstrcpy( ___s, "从容关闭正在进行中 10101" );
- break;
- case WSA_OPERATION_ABORTED:
- lstrcpy( ___s, "重叠操作被中断 (OS dependent)" );
- break;
- default:
- lstrcpy( ___s, "未知错误" );
- }
- return ___s;
- }
- BOOL WINAPI CreateEventSelectSocket(
- SOCKET * s,
- HANDLE * hEvent,
- LONG lEventFlags
- )
- {
-
- int ret;
- *s = socket( AF_INET, SOCK_STREAM, 0 );
- if( *s == INVALID_SOCKET )
- return FALSE;
-
- *hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
- if( *hEvent == NULL )
- {
- closesocket( *s );
- return FALSE;
- }
- ret = WSAEventSelect( *s, *hEvent, lEventFlags );
- if( ret == SOCKET_ERROR )
- {
- closesocket( *s );
- CloseHandle( hEvent );
- return FALSE;
- }
- return TRUE;
- }
- BOOL
- WINAPI
- RCAConnect_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- sockaddr* addr,
- UINT uExitMsg
- )
- {
- int ret;
- BOOL bSunc = FALSE;
- DWORD dwRet;
- WSANETWORKEVENTS ns;
- while(1)
- {
- ret = connect( s, addr, sizeof(sockaddr) );
- if( ret == SOCKET_ERROR )
- {
- if( WSAGetLastError() != WSAEWOULDBLOCK )
- return FALSE;
- }
- else
- {
- return TRUE;
- }
-
- while(1)
- {
- dwRet = WaitForMultipleObjects( 2, hEventArray, FALSE, INFINITE );
-
- if( dwRet == (WAIT_OBJECT_0 + 1 ) )
- {
- return FALSE;
- }
-
- ret = WSAEnumNetworkEvents( s, hEventArray[0], &ns );
- if( ret == SOCKET_ERROR )
- break;
- if( ns.lNetworkEvents & FD_CONNECT )
- {
- if( ns.iErrorCode[ FD_CONNECT_BIT ] != 0 )
- {
- return FALSE;
- }
- else
- return TRUE;
- }
- }
- }
- // never run to here;
- return TRUE;
- }
- VOID
- WINAPI
- InitRCARequestHead (
- RCAREQUESTHEADER * rreqh,
- WORD wMajorVersion, // 主版本号
- WORD wMinorVersion, // 次版本号
- DWORD dwRequestID,
- DWORD dwRequestBytes
- )
- {
- rreqh->dwRequestID = dwRequestID;
- rreqh->requestBytes = dwRequestBytes;
- rreqh->wMajorVersion = wMajorVersion;
- rreqh->wMinorVersion = wMinorVersion;
- lstrcpy( rreqh->rcaID, "RCA" );
- }
- BOOL
- WINAPI
- RCASendRequest_0X10_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- HWND hwnd,
- PSTR pOperation,
- PSTR pFile,
- PSTR pParameters,
- PSTR pDirectory,
- INT nShowCmd
- )
- {
- DWORD dwCountSize;
- INT ret;
- DWORD nOperation = lstrlen( pOperation ) +1;
- DWORD nFile = lstrlen( pFile ) +1;
- DWORD nParameters = lstrlen( pParameters ) +1;
- DWORD nDirectory = lstrlen( pDirectory ) +1;
- dwCountSize =
- 4 + // hwnd
- 4 + nOperation + // pOperation
- 4 + nFile + // pFile
- 4 + nParameters + // pParameters
- 4 + nDirectory + // pDirectory
- 4; // nShowCmd
-
- RCAREQUESTHEADER rreqh;
- InitRCARequestHead( &rreqh, 1, 0, 0x10, dwCountSize + sizeof(RCAREQUESTHEADER) );
- // 发送请求头
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&rreqh, sizeof(RCAREQUESTHEADER));
- if( ret != sizeof(RCAREQUESTHEADER) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&hwnd, sizeof(HWND));
- if( ret != sizeof(HWND) )
- return FALSE;
-
- if( nOperation == 0 )
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nOperation, sizeof(int));
- else {
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nOperation, sizeof(int));
- if( ret != sizeof(int) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pOperation, nOperation);
- if( ret != nOperation )
- return FALSE;
- }
- if( nFile == 0 )
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nFile, sizeof(int));
- else {
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nFile, sizeof(int));
- if( ret != sizeof(int) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pFile, nFile);
- if( ret != nFile )
- return FALSE;
- }
- if( nParameters == 0 )
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nParameters, sizeof(int));
- else {
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nParameters, sizeof(int));
- if( ret != sizeof(int) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pParameters, nParameters);
- if( ret != nParameters )
- return FALSE;
- }
- if( nDirectory == 0 )
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nDirectory, sizeof(int));
- else {
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&nDirectory, sizeof(int));
- if( ret != sizeof(int) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pDirectory, nDirectory);
- if( ret != nDirectory )
- return FALSE;
- }
- return TRUE;
- }
- BOOL
- WINAPI
- RCASendRequest_0X20_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- RCAGETIMAGEDATA * pgm
- )
- {
- if( pgm->dwImageFormat != IMAGE_FORMAT_BMP )
- if( pgm->dwImageFormat != IMAGE_FORMAT_JPEG )
- return FALSE;
- // DWORD dwRet;
- int ret;
- RCAREQUESTHEADER rreqh;
- rreqh.dwRequestID = 0x20;
- rreqh.requestBytes = sizeof(rreqh) + sizeof(RCAGETIMAGEDATA);
- rreqh.wMajorVersion = 1;
- rreqh.wMinorVersion = 0;
- lstrcpy( rreqh.rcaID, "RCA" );
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&rreqh, sizeof(RCAREQUESTHEADER));
- if( ret != sizeof( RCAREQUESTHEADER ) )
- return FALSE;
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pgm, sizeof(RCAGETIMAGEDATA));
- if( ret != sizeof( RCAGETIMAGEDATA ) )
- return FALSE;
- return TRUE;
- }
- HBITMAP
- WINAPI
- RCARecvImageData(
- SOCKET s,
- HANDLE hEventArray[2],
- DWORD dwImageFormat
- )
- {
- int ret;
- // DWORD dwRet;
- RCARESPONSEHEADER rresh;
- // 接收响应头
- ret = RCARecv_EventSelectIO( s, hEventArray, (char*)&rresh,
- sizeof(RCARESPONSEHEADER));
- if ( ret != sizeof(RCARESPONSEHEADER) )
- return FALSE;
- DWORD dwRecvCount = rresh.dwTotalBytes - sizeof(rresh);
- if ( dwRecvCount <= 0 )
- return FALSE;
- // 分配内存, 接收剩余的像素数据
- char * buf = (char*)new BYTE[dwRecvCount];
- if ( buf == NULL )
- return FALSE;
- // 接收图象数据
- ret = RCARecv_EventSelectIO( s, hEventArray, buf, dwRecvCount);
- if ( ret != dwRecvCount )
- return FALSE;
- if ( dwImageFormat == IMAGE_FORMAT_BMP )
- {
- // 通过 BMP 格式的数据创建 HBITMAP
-
- PBITMAPFILEHEADER pbmfh = (PBITMAPFILEHEADER)buf;
- HDC hdc = CreateDC( "DISPLAY", NULL, NULL, NULL );
- if( hdc == NULL )
- {
- delete[] buf;
- return FALSE;
- }
-
- PBYTE pBits = (PBYTE)buf;
- pBits += pbmfh->bfOffBits;
- PBITMAPINFO pbmih = (PBITMAPINFO) ( pbmfh+1 );
- PVOID p;
- HBITMAP hbm = CreateDIBSection( hdc, pbmih, DIB_RGB_COLORS, &p, NULL, NULL );
- if ( hbm == NULL )
- {
- DeleteDC( hdc );
- delete[] buf;
- return FALSE;
- }
-
- CopyMemory( p, pBits, pbmih->bmiHeader.biSizeImage );
- DeleteDC( hdc );
- delete[] buf;
- return hbm;
- } else if ( dwImageFormat == IMAGE_FORMAT_JPEG )
- {
- // 创建 DC 以备在上面绘制 JPEG 像素
- HDC hdc = CreateDC( "DISPLAY" , NULL, NULL, NULL);
- if ( hdc == NULL )
- {
- delete[] buf;
- return FALSE;
- }
- PBYTE pJPEGBuffer = (PBYTE)buf; // JPEG 缓冲区
- DWORD dwJPEGBufferSize = rresh.dwTotalBytes - 40; // JPEG 缓冲区大小
- BYTE* pRGBBuffer = NULL;
- DWORD width; // 图象宽
- DWORD height; // 图象高
- DWORD channels; // 图象颜色通道数目
-
- if ( FALSE == DecodeFromJPEGBuffer( pJPEGBuffer, dwJPEGBufferSize,
- &pRGBBuffer, &width, &height, &channels ) )
- {
- delete[] buf;
- DeleteDC( hdc );
- return FALSE;
- }
-
- BITMAPINFO bmi;
- bmi.bmiHeader.biSize = sizeof(bmi);
- bmi.bmiHeader.biBitCount = 24;
- bmi.bmiHeader.biClrImportant = 0;
- bmi.bmiHeader.biClrUsed = 0;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biWidth = width;
- bmi.bmiHeader.biHeight = height;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biSizeImage = width * height * 3;
- bmi.bmiHeader.biXPelsPerMeter = 0;
- bmi.bmiHeader.biYPelsPerMeter = 0;
-
- bmi.bmiColors[0].rgbRed = (BYTE)0xff;
- bmi.bmiColors[0].rgbGreen = (BYTE)0xff;
- bmi.bmiColors[0].rgbBlue = (BYTE)0xff;
- bmi.bmiColors[0].rgbReserved = (BYTE)0x0;
-
- PVOID p=NULL;
- HBITMAP hbm = CreateDIBSection( hdc, &bmi, DIB_RGB_COLORS, &p, NULL, NULL );
- if( hbm == NULL )
- {
- DeleteDC( hdc );
- delete[] buf;
- delete[] pRGBBuffer;
- return FALSE;
- }
- CopyMemory( p, pRGBBuffer, bmi.bmiHeader.biSizeImage );
- DeleteDC( hdc );
- delete[] buf;
- delete[] pRGBBuffer;
- return hbm;
- }
- // never run to here
- return NULL;
- }
- BOOL
- WINAPI
- RCASendMouseInput_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- RCAMOUSEINPUT * pMouseInput,
- DWORD dwArraySize
- )
- {
- if ( dwArraySize > 20 )
- return FALSE;
- int ret;
- RCAREQUESTHEADER rreqh;
- InitRCARequestHead( &rreqh, 1, 0, 0x40,
- sizeof(rreqh) + sizeof(RCAMOUSEINPUT) * dwArraySize );
-
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&rreqh, sizeof(rreqh));
- if( ret != sizeof(rreqh) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pMouseInput,
- sizeof(RCAMOUSEINPUT)*dwArraySize );
- if( ret != sizeof(RCAMOUSEINPUT) * dwArraySize )
- return FALSE;
- return TRUE;
- }
- BOOL
- WINAPI
- RCASendKeybdInput_EventSelectIO(
- SOCKET s,
- HANDLE hEventArray[2],
- RCAKEYBDINPUT * pKeybdInput,
- DWORD dwArraySize
- )
- {
- if( dwArraySize > 20 )
- return FALSE;
- int ret;
- DWORD dwSize = sizeof(RCAKEYBDINPUT) * dwArraySize;
- RCAREQUESTHEADER rreqh;
- InitRCARequestHead( &rreqh, 1, 0, 0x41, dwSize + sizeof(rreqh) );
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)&rreqh, sizeof(rreqh));
- if( ret != sizeof(rreqh ) )
- return FALSE;
- ret = RCASend_EventSelectIO( s, hEventArray, (char*)pKeybdInput, dwSize);
- if( ret != dwSize )
- return FALSE;
- return TRUE;
- }
- BOOL
- WINAPI
- CreateMouseInput(
- RCAMOUSEINPUT* pmi,
- UINT msg,
- WPARAM w,
- LPARAM l,
- float fWidthScale,
- float fHeightScale
- )
- {
- pmi->dwExtraInfo = 0;
- pmi->mouseData = 0;
- pmi->time = 0;
- switch( msg )
- {
- case WM_MOUSEMOVE:
- pmi->dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
- pmi->dx = (LONG)(( w & 0x0000ffff ) * fWidthScale);
- pmi->dy = (LONG)(( w >> 16 ) * fHeightScale );
- return TRUE;
- case WM_LBUTTONDOWN:
- pmi->dwFlags = MOUSEEVENTF_LEFTDOWN;
- return TRUE;
- case WM_RBUTTONDOWN:
- pmi->dwFlags = MOUSEEVENTF_RIGHTDOWN;
- return TRUE;
- case WM_LBUTTONUP:
- pmi->dwFlags = MOUSEEVENTF_LEFTUP;
- return TRUE;
- case WM_RBUTTONUP:
- pmi->dwFlags = MOUSEEVENTF_RIGHTUP;
- return TRUE;
- default:
- break;
- }
- return FALSE;
- }
- BOOL
- WINAPI
- CreateKeybdInput(
- RCAKEYBDINPUT* pbi,
- UINT msg,
- WPARAM w,
- LPARAM l
- )
- {
- pbi->dwExtraInfo = 0;
- pbi->time = 0;
- pbi->wScan = 0;
- switch( msg )
- {
- case WM_KEYDOWN:
- pbi->wVk = (WORD)w;
- pbi->dwFlags = 0;
- return TRUE;
- case WM_KEYUP:
- pbi->wVk = (WORD)w;
- pbi->dwFlags = KEYEVENTF_KEYUP;
- return TRUE;
- default:
- break;
- }
- return FALSE;
- }