⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asyncsocket.h

📁 一个可订制ip packet的程序
💻 H
字号:
/*
 *
 *
 *  Copyright (c) 2000 Barak Weichselbaum <barak@komodia.com>
 *  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 * Contact info:
 * Site: http://www.komodia.com
 * Email: barak@komodia.com
 */

#if !defined(AFX_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_)
#define AFX_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning(disable:4786)

#include "SpoofBase.h"
#include <map>

//Message handlers
#define WM_BASE				WM_USER
#define WM_SOCKET_GENERAL	WM_BASE+1
#define WM_SOCKET_ACCEPT	WM_BASE+2
#define WM_SOCKET_CONNECT	WM_BASE+3
#define WM_SOCKET_TIMEOUT	WM_BASE+4

//Definitions for no messaging
#define NO_OnSocketTimeout	virtual BOOL OnSocketTimeout() {return TRUE;}
#define NO_OnSocketConnect	virtual BOOL OnSocketConnect(int iErrorCode) {return TRUE;}
#define NO_OnSocketAccept	virtual BOOL OnSocketAccept(int iErrorCode) {return TRUE;}
#define NO_OnSocketClose	virtual BOOL OnSocketClose(int iErrorCode) {return TRUE;}
#define NO_OnSocketOOB		virtual BOOL OnSocketOOB(int iErrorCode) {return TRUE;}
#define NO_OnSocketWrite	virtual BOOL OnSocketWrite(int iErrorCode) {return TRUE;}
#define NO_OnSocketReceive	virtual BOOL OnSocketReceive(int iErrorCode) {return TRUE;}

#define SOCKET_WINDOW_NAME "Socket notification sink"

//Window class name
#define CAsyncSocket_Class "CAsyncSocketClass"
#define CAsyncShutdown_Class "CAsyncShutdown"

class CSocketThreadManager;

//##ModelId=3B43E6F500D3
class CAsyncSocket : protected CSpoofBase
	
{
public:
	//Initialize all the handlers
	//##ModelId=3B43E6F500DE
	static void Initialize();

	//Indicate a system shutdown
	//##ModelId=3B43E6F500E8
	static void Shutdown();

	//Disable the time
	//##ModelId=3B43E6F500F1
	BOOL KillTimer();

	//Create a timeout
	//##ModelId=3B43E6F500FB
	BOOL SetTimeout(int iMs);

	//Set the instace of our app
	//##ModelId=3B43E6F50105
	static void SetInstance(HINSTANCE hInst);

	//ctor and dtor
	//##ModelId=3B43E6F5010F
	CAsyncSocket();
	//##ModelId=3B43E6F50119
	virtual ~CAsyncSocket();
protected:
	//Messaging methods
	//##ModelId=3B43E6F50123
	virtual BOOL OnSocketTimeout()=0;
	//##ModelId=3B43E6F5012D
	virtual BOOL OnSocketConnect(int iErrorCode)=0;
	//##ModelId=3B43E6F50137
	virtual BOOL OnSocketAccept(int iErrorCode)=0;
	//##ModelId=3B43E6F50142
	virtual BOOL OnSocketClose(int iErrorCode)=0;
	//##ModelId=3B43E6F5014C
	virtual BOOL OnSocketOOB(int iErrorCode)=0;
	//##ModelId=3B43E6F50156
	virtual BOOL OnSocketWrite(int iErrorCode)=0;
	//##ModelId=3B43E6F5016A
	virtual BOOL OnSocketReceive(int iErrorCode)=0;

	//Get the ID of the socket
	//##ModelId=3B43E6F50174
	int GetSocketID();

	//Get the handle of the window
	//##ModelId=3B43E6F5017E
	HWND GetWindowHandle();

	//Get the socket handle
	//##ModelId=3B43E6F50188
	virtual SOCKET GetAsyncHandle()=0;

	//Go to async regular mode
	//##ModelId=3B43E6F50192
	virtual BOOL SetAsync()=0;

	//Remove the socket from the list
	//##ModelId=3B43E6F50194
	void RemoveSocketFromList();

	//Add the socket to the list
	//##ModelId=3B43E6F5019C
	void AddSocketToList();

	//Do we have a timeout
	//##ModelId=3B43E6F501A6
	BOOL IsTimeout();
private:
	//##ModelId=3B43E6F5032C
	typedef std::less<int> SocketLess;
	//##ModelId=3B43E6F50340
	typedef std::map<int,CAsyncSocket*,SocketLess> SocketMap;
private:
	//Remove from thread info
	//##ModelId=3B43E6F501B0
	void DeAllocateHandle();

	//Allocate ourself a window
	//##ModelId=3B43E6F501BA
	void AllocateHandle();

	//Get our thread manager (global or local)
	//##ModelId=3B43E6F501BB
	CSocketThreadManager* GetThreadManager();

	//Remove the handlers
	//##ModelId=3B43E6F501C4
	static BOOL RemoveHandlers();

	//Get the instance of our APP
	//##ModelId=3B43E6F501CE
	static HINSTANCE GetInstance();

	//Create our handlers
	//##ModelId=3B43E6F501D9
	static BOOL SetHandlers();
	
	//Register our window
	//##ModelId=3B43E6F501EC
	static BOOL RegisterWindow();

	//Find a socket
	//##ModelId=3B43E6F501F6
	static CAsyncSocket* GetSocketByID(int iSockID);
	
	//Our list of sockets
	//##ModelId=3B43E6F5020B
	static SocketMap m_SocketMap;

	//Do we have a window handle
	//##ModelId=3B43E6F5021E
	static BOOL m_Window;

	//Our window's handle
	//##ModelId=3B43E6F50232
	static HWND m_WindowHandle;

	//Instance of our window
	//##ModelId=3B43E6F50246
	static HINSTANCE m_Instance;

	//Are we initialized
	//##ModelId=3B43E6F50264
	static BOOL m_Initialized;

	//ID of our socket
	//##ModelId=3B43E6F50278
	int m_SocketID;

	//Are we in the list
	//##ModelId=3B43E6F5028C
	BOOL m_List;

	//Timeout indicator
	//##ModelId=3B43E6F50296
	BOOL m_Timeout;

	//Our window's handle
	//##ModelId=3B43E6F502AA
	HWND m_hLocalWindowHandle;

	//Are we shutting down
	//##ModelId=3B43E6F502B4
	static BOOL m_bShuttingDown;

	//Our thread manager (global)
	//##ModelId=3B43E6F502D3
	static CSocketThreadManager* m_pThreadManager;

	//Our local thread manager (to allow custom thread mangement)
	//##ModelId=3B43E6F502F1
	CSocketThreadManager* m_pLocalThreadManager;
private:
	//Our shutdown class (all of this to avoid father to know his sons)
	//##ModelId=3B43E6F50372
	class CAsyncShutdown : protected CSpoofBase
	{
	public:
		//##ModelId=3B43E6F5037D
		CAsyncShutdown();
		//##ModelId=3B43E6F50386
		virtual ~CAsyncShutdown();
	protected:
		//Shutdown notifier
		//##ModelId=3B43E6F50388
		virtual void NotifyShutdown();
	};
private:
	//Our window proc
	//##ModelId=3B43E6F50304
	static LRESULT CALLBACK SocketMessageHandler(HWND hwnd,      // handle to window
				    			  			     UINT uMsg,      // message identifier
										         WPARAM wParam,  // first message parameter
										         LPARAM lParam);
};

#endif // !defined(AFX_ASYNCSOCKET_H__980B05BD_A250_40D0_AD92_4F31AEC90E74__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -