64,637
社区成员
发帖
与我相关
我的任务
分享
#ifndef _OUT_LIB_H_
#define _OUT_LIB_H_
//#include "stdafx.h"
#include "IoHandle.h"
#include "Session.h"
#include "Network.h"
#include "RecvBuf.h"
#include "SendBuf.h"
#include "MyObject.h"
//class AsioNetwork;
class MeNetObject;
//class IoHandle;
//class RecvBuffer;
//class SendBuffer;
//class Session;
namespace ASIO_NET
{
class COutLib
{
public:
COutLib();
~COutLib();
static COutLib * Instance();
AsioNetwork * InstanceNetWork();
NetObject * InstanceNetObject();
IoHandle * InstanceIoHandle();
Session * InstanceSession();
RecvBuffer * InstanceRecvBuffer();
SendBuffer * InstanceSendBuffer();
private:
static COutLib * g_pInstance;
AsioNetwork * g_pNetWorkInstance;
MeObject * g_pNetObjectInstance;
IoHandle * g_pIoHandleInstance;
Session * g_pSessionInstance;
RecvBuffer * g_pRecvBufferInstance;
SendBuffer * g_pSendBufferInstance;
};
/***************************************************************************************************************************/
COutLib* COutLib::g_pInstance = 0;
COutLib::COutLib()
{
// g_pInstance = new COutLib();
g_pNetWorkInstance = new AsioNetwork();
g_pNetObjectInstance = new MeObject();
g_pIoHandleInstance = new IoHandle();
g_pSessionInstance = new Session(1,g_pIoHandleInstance);
g_pRecvBufferInstance = new RecvBuffer();
g_pSendBufferInstance = new SendBuffer();
}
COutLib::~COutLib()
{
if(g_pInstance != NULL)
{
// delete g_pInstance;
delete g_pNetWorkInstance;
delete g_pNetObjectInstance;
delete g_pIoHandleInstance;
delete g_pSessionInstance;
delete g_pRecvBufferInstance;
delete g_pSendBufferInstance;
}
}
/***************************************************************************************************************************/
inline
COutLib*
COutLib::Instance()
{
return g_pInstance;
}
/***************************************************************************************************************************/
inline
AsioNetwork*
COutLib::InstanceNetWork()
{
return g_pNetWorkInstance;
}
///***************************************************************************************************************************/
inline
NetObject*
COutLib::InstanceNetObject()
{
return g_pNetObjectInstance;
}
///***************************************************************************************************************************/
inline
IoHandle*
COutLib::InstanceIoHandle()
{
return g_pIoHandleInstance;
}
///***************************************************************************************************************************/
inline
RecvBuffer*
COutLib::InstanceRecvBuffer()
{
return g_pRecvBufferInstance;
}
///***************************************************************************************************************************/
inline
SendBuffer*
COutLib::InstanceSendBuffer()
{
return g_pSendBufferInstance;
}
///***************************************************************************************************************************/
inline
Session*
COutLib::InstanceSession()
{
return g_pSessionInstance;
}
///***************************************************************************************************************************/
}
#endif
//声明
extern DLLAPI bool //__stdcall
Asio_Init( unsigned int max_client, unsigned int max_sendbuf, unsigned int max_recvbuf,
unsigned int max_client_pack, unsigned int ioThreads, unsigned int preAcceptAmount );
//实现
AsioNetwork ank;//使用这个声明在下面调用就没有错误!!!
////Asio_Init
//封装的网络模块
bool
//__stdcall
Asio_Init(unsigned int max_client ,unsigned int max_sendbuf ,unsigned int max_recvbuf ,unsigned int max_client_pack ,unsigned int ioThreads ,unsigned int preAcceptAmount)
{
AsioNetwork::Config config;
config.nStartId = 1; //Client开始ID
config.nMaxClient = max_client;
config.nMaxSendBuf = max_sendbuf;
config.nMaxRecvBuf = max_recvbuf;
config.nMaxClientPack = max_client_pack;
config.nIoThreads = ioThreads;
config.nTimeOut = 0;
config.nPreAcceptAmount = preAcceptAmount;//连接总数
config.fnCreateNetObject = Fn_CallBackCreateAcceptedObject;
config.fnDestroyNetObject = Fn_CallBackDestroyAcceptedObject;
bool bInit;
//如果用下面这一行输出的DLL,在C#中运行会出现错误提示
//bInit = COutLib::Instance()->InstanceNetWork()->StartListen("127.0.0.1", 9998);
//如果换成下面的直接调用就不会出错,但是我想用上面的全局实例调用DLL中的所有函数,请教代码到底哪里有问题!
ank.StartListen("127.0.0.1", 9998);
printf("Asio_Init dll !");
// system("pause");
return bInit;
}
public const string DLL_NAME = "AsioNetwork.dll";
////Asio_Init
[DllImport(DLL_NAME, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern bool Asio_Init(uint max_client, uint max_sendbuf, uint max_recvbuf,
uint max_client_pack, uint ioThreads, uint preAcceptAmount);
...
Asio_Init(5,32,32,16,5,1);// 这一行调用DLL出错。