Server模块如果仅仅作为一个lib包装出来自然不是最好,虽然可在c++工程很容易使用,效率也比较高,但要使用到其他语言上去就没有办法,如果包装一下自然是最好的,但如何包装呢,包装成一个什么样子也是很重要的,最常见的包装自然是包装成一个dll,输出一些接口,其他上层模块在该接口的基础上使用。最简单的包装一般可输出如下一些接口,
Public IServer:
{
Virtual void StartServer(LPCTSTR ports, LPCTSTR udpoorts, int iothreads, int syncthreads, int asyntchreads);
Virtual void StopServer();
Virtual IClient *CreateClient();
Virtual void CloseClient(IClient *);
Virtual void OnCloseClient(IClient *);
Virtual void OnConnectClient(IClient *);
Virtual void PostSyncEvent(DWORD event, WPARAM wParam, LPARAM lParam)
Virtual void PostAsyncEvent(DWORD event, WPARAM wparam, LPARAM lParam);
Virtual void OnAsyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void OnSyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void OnTimer(UINT uid);
Virtual void AddTimer(UINT uid, DWORD tk1, DWORD duetk);
};
Public IClient
{
Virtual bool SendData(void *pdata, int len);
Virtual bool SendFile(LPCTSTR lpfilename);
Virtual bool SendFile(HANDLE hFile);
Virtual bool SendMsg(PKHEAD *ph);
Virtual bool SendMsg(PKHEAD *ph, void *pdata, int len);
Virtual bool SendMsg(PKHEAD *ph, void *pdata, int len, byte encrypt, byte compress);
Virtual void DelayClose();
Virtual void OnDelayClose();
Virtual void OnClose();
Virtual void OnConnect();
Virtual void OnAsyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void OnSyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void PostSyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void PostAsyncEvent(DWORD event, WPARAM wParam, LPARAM lParam);
Virtual void SetTag(DWORD wParam);
Virtual DWORD GetTag();
Virtual long GetId();
};
IServer *DllCreateObject();
等
通过结合LoadLibrary并提供一个类似DllCreateObject的形式可很容易的 模拟类com实现,上层可很容易的是复用dll的实现。