zoukankan      html  css  js  c++  java
  • C++ 回调函数 实现 的测试代码

    最近项目里使用了异步Socket,使用的是完成端口做的e; Accept,receive,send 等完全的异步实现(多线程)

    然后 又要多个端口使用, 后来想到包装下完成端口Socket,然后当有事件是触发回调函数,就不用手动搞N多个线程什么的

    如是,测试例子如下:

    #include "stdafx.h"
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    //回调函数 实现 的测试
    #define __CallBackTestFun__
    
    /*回调函数
    **回调函数实现体必须是静态方法,并且应该使用"__stdcall"声明;
      应该接受参数转换成本类指针,然后才能操作本类的成员
    **即:static void __stdcall Fun(void* pthis,...)
    */
    #ifdef __CallBackTestFun__
    typedef void(__stdcall*POnData)(void*,unsigned int,void*) ;
    typedef void(__stdcall*POnReccive)(void*,unsigned int) ;
    
    class TSocketObj
    {
    private:
    	POnData pOnData,pp;
    	POnReccive pOnReccive;
    	void* tag;
    protected:
    	void OnData(){pOnData(tag,123,"1234");}
    public:
    	int No;//
    	unsigned char type; //0:TCP  1:UDP
    	unsigned int sock;
    	void* pdata;
    	char name[21];
    	TSocketObj *next,*prior;
    
    	TSocketObj(){memset(this,0,sizeof(TSocketObj));}//初始化 也很重要
    	TSocketObj(void* Obj){
    		memset(this,0,sizeof(TSocketObj));
    		tag=Obj;
    		printf("
    Obj:%x
    ",tag);
    	}
    	void SetCallBack(void* Obj,POnData onData,POnReccive onReccive){
    		tag=Obj;pOnData=onData;pOnReccive=onReccive;
    	}
    	void SetP(POnData p){pp=p;}
    	void Go(){
    		if(pOnData)pOnData(tag,sock,name);
    		if(pOnReccive)pOnReccive(tag,No);
    		if(pp)pp(tag,sock,name);
    	}
    };
    
    class TSocket
    {
    	TSocketObj socks;
    public:
    	int m;
    	TSocket(){}
    	TSocket(int n){
    		Set(n);
    	}
    	static void __stdcall OnData(void* pThis,unsigned int no,void* d)
    	{
    		printf("
    This is:%d   Sock:%d	name:%s",((TSocket*)pThis)->m,no,d);
    	}
    	friend void __stdcall OnReccive(void* pThis,unsigned int d)
    	{
    		printf("
    	No:%d
    ",d);
    	}
    	//这个不能做回调函数
    	void __stdcall OnGo(void* pThis,unsigned int no,void* d)
    	{
    		printf("
    	OnGo m:%d
    ",m);
    	}
    	void Set(int d){
    			m=1000+d;
    			socks.No=d;
    			socks.sock=100*d+78;
    			sprintf(socks.name,"Sock_%d",d);
    			socks.SetCallBack(this, OnData, OnReccive);
    			//POnData p=&(this->OnGo);
    			//socks.SetP((void*)&this->OnGo);
    	}
    
    	void show(){socks.Go();}
    };
    
    #endif
    
    
    int main(int argc, char* argv[])
    {
    	int iarr[10]={1,5,3,2,7,4,9,6,8,0};//BrdNo
    //回调函数 实现 的测试
    #ifdef __CallBackTestFun__
    	TSocket tr;
    	tr.Set(7);
    	tr.show();
    
    	TSocket ty;
    	ty.Set(8);
    	ty.show();
    #endif
    	scanf("%d",iarr);
    	return 0;
    }
    

      这是个完整的例子,转载请注明:http://www.cnblogs.com/lzpong/

  • 相关阅读:
    Vagrant box ubuntu/xenial64 添加vagrant用户解决没有登录密码的问题
    jQuery获取浏览器URL链接的值
    js防止客户端多触发
    Jquery实现一组复选框单选
    jQuery监听文本框值改变触发事件(propertychange)
    将http调用返回json中的有关中文的unicode转换为中文
    Visual Studio 2015 Bowser Link的功能不停的向服务端发送请求
    客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)
    java集群之session共享解决方案
    阻止保存要求重新创建表的更改选项
  • 原文地址:https://www.cnblogs.com/lzpong/p/4228357.html
Copyright © 2011-2022 走看看