📄 iocpapi.h
字号:
/**
This is the interface of the iocp application
You surely can use the header "iocp.h" if you
know the details of this framework.
*/
#pragma once
#include <winsock2.h>
// message structure
// We can define NET_MESSAGE_SIZE to any positive number;
// I chose 4096-32 to presume it would be place in
// a single page
const int NET_MESSAGE_SIZE=4096-32;
struct NetMessage
{
int datalen;
char data[NET_MESSAGE_SIZE];
};
class IOCPNetServer
{
friend class IOCPImplServer;
public:
virtual ~IOCPNetServer();
//
//listenport is the listening port of your server
//
explicit IOCPNetServer(unsigned short listenport);
//start the framework
//Make sure you call start() once in the
//initialization of your application
void start();
//stop the framework
//You May call stop() once when you want shutdown
//the framework,the destructor of this class will
//call this function automatically
void stop();
//the message dispatcher engine
//you call this in some loop,probably in your
//main loop to deal the network messages
void runloop();
//close a SOCKET
//call this function when you want to close a SOCKET,
//the socket you closed would be returned in
//iocp_closed_handler some time later,Make sure
//DO NOT call the system function closesocket() directly
void close(SOCKET s);
// send a message
// call this function wherever and whenever
// you want to send a message
// IMPORTANT ! the NetMessage is allocated
// by the new operator and DO NOT free it
// after you call sendmessage to send it
void sendmessage(SOCKET s,NetMessage *m);
protected:
//when a client connected,this function would be called
//you need to deal with the new socket
virtual void iocp_open_handler(SOCKET s)=0;
//when a socket has been closed(the reason may be the network
//has been disconnected,the client close the connection,or you
//close it using close(SOCKET s),etc...),this function will be
//called.Here this socket has already been close,and you need
//to do something
virtual void iocp_closed_handler(SOCKET s)=0;
//when a message received,this function would be called
//you need to deal with the message
//DO NOT delete the NetMessage structure
virtual void iocp_message_handler(SOCKET s,NetMessage *msg)=0;
//when a client sends something that is not a message,this
//function will be called.That probably means your server
//is being attacked .You probably need to simply close the
//client using the member function close(SOCKET s);
virtual void iocp_illegal_handler(SOCKET s,int msglen)=0;
private:
IOCPImplServer *mimpl;
};
class IOCPNetClient
{
friend class IOCPImplClient;
public:
virtual ~IOCPNetClient();
IOCPNetClient();
void start();
void stop();
void runloop();
void close(SOCKET s);
void sendmessage(SOCKET s,NetMessage *m);
//connect a server,
//if connected ok it returns true and the SOCKET *s would
//be filled with the connected socket
//IMPORTANT ! the same socket would be returned in the
//iocp_open_handler some time later.so you do not need to
//fill this parameter
bool connect(const char *dotip,unsigned short port,SOCKET *s=NULL);
protected:
//when you use the member function connect to connect some server
//successfully,this function will be call some time later and
//return the new socket you connected
virtual void iocp_open_handler(SOCKET s)=0;
virtual void iocp_closed_handler(SOCKET s)=0;
virtual void iocp_message_handler(SOCKET s,NetMessage *msg)=0;
virtual void iocp_illegal_handler(SOCKET s,int msglen)=0;
private:
IOCPImplClient *mimpl;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -