Netty的深入浅出--71.NIO中Directbuffer与Heapbuffer的疑问(知乎)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_37909508/article/details/93020767

知乎上问题描述

 

 

  

当传入的bytebuffer不是directbuffer的时候就会通过getTemporaryDirectBuffer()方法从操作系统中申请一块缓存。 

 

 

 

展开阅读全文

对于netty中sync()方法的一些疑问~

04-15

nn```n public final class WebSocketServer nn static final boolean SSL = System.getProperty("ssl") != null;n static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));nn public static void main(String[] args) throws Exception n // Configure SSL.n final SslContext sslCtx;n if (SSL) n SelfSignedCertificate ssc = new SelfSignedCertificate();n sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());n else n sslCtx = null;n nn EventLoopGroup bossGroup = new NioEventLoopGroup(1);n EventLoopGroup workerGroup = new NioEventLoopGroup();n try n ServerBootstrap b = new ServerBootstrap();n b.group(bossGroup, workerGroup)n .channel(NioServerSocketChannel.class)n .handler(new LoggingHandler(LogLevel.INFO))n .childHandler(new WebSocketServerInitializer(sslCtx));n //从这里就有点看不懂了,这里绑定了一个端口,这个sync是什么?n //文档上说:等待这个未来直到它完成,如果这个未来失败,重新抛出失败的原因。n //文档上说的感觉就是java中serversocket的acpte接收方法,而且还是同步接收n //但实时指定不是,因为执行完sync这段代码,程序并没有阻塞,而是继续执行下面的代码了n Channel ch = b.bind(PORT).sync().channel();nn System.out.println("Open your web browser and navigate to " +n (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');n //程序执行到这里开始阻塞,等待有计算机连接进来n ch.closeFuture().sync();n finally n bossGroup.shutdownGracefully();n workerGroup.shutdownGracefully();n n nn```nn 问答

深入浅出MFC》 中相关疑问?(消息传递)

12-27

在MFC 中的消息传递‘一般’是从子类流向父类的。rn 其中CWinThread类并不在‘消息传递网’ 中。rn 模拟代码主要如下:rn[code=c]rn#include rn#includern#include rnrnusing namespace std;rnrnrn#define RUNTIME_CLASS_NAME(class_name) \rn (#class_name )rnrnenum IDrnrn CObjectid = 0,rn CCmdTergetid = 1,rn CWinThreadid = 11,CWinAppid = 111,rn CWndid = 12,CViewid = 121,CFrameWndid = 122rn;rnrnstruct AFX_MSGMAP_ENTRYrnrn char* pName;rn ID id;rnrn;rnstruct AFX_MSGMAP rnrn AFX_MSGMAP* pBaseMessageMap;rnrn AFX_MSGMAP_ENTRY* lpEntry;rn;rnrn#define ClassName(class_name) #class_namern#define Classid(class_name) class_name##idrnrn #define DECLARE_MESSAGE_MAP() \rn static AFX_MSGMAP_ENTRY _messageEntries[];\rn static AFX_MSGMAP messageMap;\rn virtual AFX_MSGMAP* GetMessageMap() const;rn rnrn#define BEGIN_MESSAGE_MAP(class_name, base_class_name) \rn AFX_MSGMAP* class_name::GetMessageMap() const\rn return &class_name::messageMap; \rn AFX_MSGMAP class_name::messageMap = \rn \rn &(base_class_name::messageMap), (AFX_MSGMAP_ENTRY*)&(class_name::_messageEntries)\rn ;\rn AFX_MSGMAP_ENTRY class_name::_messageEntries[] = \rn ClassName(class_name), Classid(class_name) \rn ;rnrnclass CObjectrnrnrn;rnrnclass CCmdTerget : public CObjectrnrnrnpublic:rn DECLARE_MESSAGE_MAP()rn;rn rnAFX_MSGMAP CCmdTerget::messageMap= NULL, CCmdTerget::_messageEntries;rnAFX_MSGMAP_ENTRY CCmdTerget::_messageEntries[] = ClassName(CCmdTerget),Classid(CCmdTerget);rnAFX_MSGMAP* CCmdTerget::GetMessageMap()const return NULL; rnrn//-rnclass CWinThread : public CCmdTergetrnrnrnpublic:rn rnrn;rnrnclass CWinApp : public CWinThreadrnrnrnpublic:rn DECLARE_MESSAGE_MAP()rn;rnBEGIN_MESSAGE_MAP(CWinApp, CWinThread)rnrn//-rnclass CWnd : public CCmdTergetrnrnrnpublic:rn DECLARE_MESSAGE_MAP()rnrn;rnBEGIN_MESSAGE_MAP(CWnd, CCmdTerget)rnrn//rnclass CView: public CWndrnrnrnpublic:rn DECLARE_MESSAGE_MAP()rn;rnBEGIN_MESSAGE_MAP(CView,CWnd)rnrn//rnclass CFrameWnd : public CWndrnrnrnpublic:rn DECLARE_MESSAGE_MAP()rn;rnBEGIN_MESSAGE_MAP(CFrameWnd, CWnd)rn//////////////////////////////////////////////////////////////////////////rn rnvoid Print(AFX_MSGMAP_ENTRY* pEntry)rn rn AFX_MSGMAP_ENTRY ame[] =rn rn "CObject", CObjectid ,rn "CCmdTerget" ,CCmdTergetid ,rn "CWinThread" ,CWinThreadid,rn "CWinApp" ,CWinAppid,rn "CWnd" ,CWndid ,rn "CView" ,CViewid,rn "CFrameWnd",CFrameWndidrn ;rnrn for (UINT index = 0; index < sizeof(ame)/sizeof(ame[0]); index++)rn rn if (strcmp(pEntry->pName,ame[index].pName ) ==0)rn rn cout<pBaseMessageMap)rn rn if (pMsg->lpEntry)rn rn Print(pMsg->lpEntry);rn rn rnrn return;rnrnrn rnint main( )rnrn CWinApp ca;rn AFX_MSGMAP* pCa = ca.GetMessageMap();rn MsgPrint(pCa);rnrn return 0;rnrn[/code]rn rnrn//---------------------------------------rn主要的疑问就是,子类是通过 pBaseMessageMap 去访问 父类 中的 messageMaprn(即,这段代码 rn[code=c]rnAFX_MSGMAP class_name::messageMap = \rn \rn &(base_class_name::messageMap), (AFX_MSGMAP_ENTRY*)&(class_name::_messageEntries)\rn ;\rn[/code]rn)rn但是在‘CWinThread’类中 并没有 定义 ‘messageMap’ 这个成员啊?rn那么&(CWinThread::messageMap)【&(base_class_name::messageMap】rn又是怎么编译通过的?rnrn运行后的结果如下:rn[img=http://img.my.csdn.net/uploads/201212/27/1356595872_9356.png][/img] 论坛

mfc深入浅出第一个例子疑问

12-01

真是很奇怪的问题:rn这时mfc上的例子rn全部代码如下:rnrn//----------------------------------------------------------------rn// 文件名:generic.hrn//----------------------------------------------------------------rnrnBOOL InitApplication(HANDLE);rnBOOL InitInstance(HANDLE,int);rnLRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);rnLRESULT CALLBACK About(HWND,UINT,WPARAM,LPARAM);rnrnrn//-----------------------------------rn//文件名:resource.hrn//-----------------------------------rn#ifndef __RESOURCE_H__rnrn#define __RESOURCE_H__rnrn#define IDM_NEW 102rn#define IDM_SAVE 103rn#define IDM_EXIT 104rn#define IDM_ABOUT 105rnrn#endifrnrn//----------------------------------------rn//文件名:Generic.rcrn//-----------------------------------------rn#include "windows.h"rn#include "resource.h"rnrnjjhouricon ICON "jjhour.ico";rnrnGenericMenu MENUrnBEGINrn POPUP "&File"rn BEGINrn MENUITEM "&New",IDM_NEW,GRAYEDrn MENUITEM "Save",IDM_SAVE,GRAYEDrn MENUITEM "E&xit",IDM_EXITrn ENDrn POPUP "&Help"rn BEGINrn MENUITEM "ABout...",IDM_ABOUTrn ENDrnENDrnrnAboutBox DIALOG 22,17,144,75rnSTYLE DS_MODALFRAME|WS_CAPTION|WS_SYSMENUrnCAPTION "About generic"rnBEGINrn CTEXT "Window2000",-1,0,5,144,8rn CTEXT "Generic Application",-1,0,14,144,8rn CTEXT "Version 1.0",-1,0,34,144,8rn DEFPUSHBUTTON "OK",IDOK,53,59,32,14,WS_GROUPrnENDrnrn//----------------------------------------------------------------rn// GENERIC--WIN32程序的基础写法rn//文件名:GENERIC.CPPrn//rn//----------------------------------------------------------------rnrn#include rn#include "resource.h"rn#include "generic.h"rnrnHINSTANCE _hInst; rnHWND _hWnd; rnchar _szAppName[]="Generic"; rnchar _szTitle[]="Generic Sample Application"; rn//----------------------------------------------------------------rn// WinMain---程序进入点rn//rn//----------------------------------------------------------------rnrnint CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)rnrn MSG msg; rn if (!hInstance) rn if (!InitApplication(hInstance))rn return FALSE;rn rnrn //判断初始化是否成功rn if (!InitInstance(hInstance,nCmdShow))rn return FALSE;rnrnrn while(GetMessage(&msg,NULL,0,0))rn rn TranslateMessage(&msg);rn DispatchMessage(&msg);rn rn return (msg.wParam); rnrnrn//----------------------------------------------------------------rn//InitInstance--注册窗口类别 rn//rn//----------------------------------------------------------------rnrnBOOL InitApplication(HINSTANCE hInstance)rnrn WNDCLASS wc;rn wc.style =CS_HREDRAW|CS_VREDRAW; rn wc.lpfnWndProc =(WNDPROC)WndProc; rn wc.cbClsExtra =0; rn wc.cbWndExtra =0; rn wc.hInstance =hInstance; rn wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO); rn wc.hCursor =LoadCursor(NULL,IDC_ARROW); rn wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); rn wc.lpszMenuName =NULL; rn wc.lpszClassName =_szAppName; rn rn //注册创建窗体的资源rn return RegisterClass(&wc);rnrn//----------------------------------------------------------------rn// InitInstance---产生窗体 rn//rn//rn//----------------------------------------------------------------rnrnBOOL InitInstance(HINSTANCE hInstance,int nCmdShow)rnrn _hInst=hInstance; rn _hWnd=CreateWindow(_szAppName,_szTitle, WS_OVERLAPPEDWINDOW, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn NULL, rn NULL, rn hInstance, rn NULL rn );rn if (!_hWnd)rn return FALSE;rnrn ShowWindow(_hWnd,nCmdShow); rn UpdateWindow(_hWnd); rn return TRUE;rnrnrn//----------------------------------------------------------------rn// WndProc--窗体函数 rn//rn//rn//----------------------------------------------------------------rnLRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)rnrn int wmId,wmEvent;rnrn switch(message)rn rn case WM_COMMAND:rn wmId =LOWORD(wParam); rn wmEvent =HIWORD(wParam); rn switch(wmId)rn rn case IDM_ABOUT:rn DialogBox(_hInst,rn "AboutBox", rn hWnd,rn (DLGPROC)Aboutrn );rn break;rn case IDM_EXIT:rn DestroyWindow(hWnd);rn break;rn default:rn return DefWindowProc(hWnd,message,wParam,lParam);rn rn break;rnrn case WM_DESTROY:rn PostQuitMessage(0);rn break;rn default:rn return DefWindowProc(hWnd,message,wParam,lParam);rn rn return 0;rnrn//----------------------------------------------------------------rn// About--弹出框函数 rn//rn//rn//----------------------------------------------------------------rnLRESULT CALLBACK About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)rnrn switch(message)rn rn case WM_INITDIALOG:rn return TRUE;rn case WM_COMMAND:rn if (LOWORD(wParam)==IDOK || LOWORD(wParam)==IDCANCEL)rn rn EndDialog(hDlg,TRUE);rn return TRUE;rn rn break;rn rn return FALSE;rnrnrn以上代码不能通过编译rn报rnnew_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitInstance(void *,int)" (?InitInstance@@YAHPAXH@Z) ,该符号在函数 _WinMain@16 中被引用rnnew_1 error LNK2019: 无法解析的外部符号 "int __cdecl InitApplication(void *)" (?InitApplication@@YAHPAX@Z) ,该符号在函数 _WinMain@16 中被引用rnnew_1 fatal error LNK1120: 2 个无法解析的外部命令rnrn而如果把InitApplication和InitInstance这两个基本点函数的内容直接写到winmainrn中,就可以正常通过编译并执行.rn如:rnrnint CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)rnrn MSG msg; rn /*if (!hInstance) rn if (!InitApplication(hInstance))rn return FALSE;*/rn rn WNDCLASS wc;rn wc.style =CS_HREDRAW|CS_VREDRAW; rn wc.lpfnWndProc =(WNDPROC)WndProc; rn wc.cbClsExtra =0; rn wc.cbWndExtra =0; rn wc.hInstance =hInstance; rn wc.hIcon =LoadIcon(hInstance,IDI_WINLOGO); rn wc.hCursor =LoadCursor(NULL,IDC_ARROW); rn wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); rn wc.lpszMenuName ="GenericMenu"; rn wc.lpszClassName =_szAppName; rn rn rn RegisterClass(&wc);rnrnrn _hInst=hInstance; rnrn _hWnd=CreateWindow(_szAppName, rn _szTitle, rn WS_OVERLAPPEDWINDOW, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn CW_USEDEFAULT, rn NULL, rn NULL, rn hInstance, rn NULL rn );rn //if (!_hWnd)rn //return FALSE;rnrn ShowWindow(_hWnd,nCmdShow); rn UpdateWindow(_hWnd); rn //判断初始化是否成功rn /*if (!InitInstance(hInstance,nCmdShow))rn return FALSE;*/rnrnrn while(GetMessage(&msg,NULL,0,0))rn rn TranslateMessage(&msg);rn DispatchMessage(&msg);rn rn return (msg.wParam); rnrnrn这是为什么呢?如果写到函数里的话应该怎么写才正确rn我的环境是vs2003rn请各位帮忙。~~~~~~~~~ 论坛

没有更多推荐了,返回首页