1 Star 1 Fork 3

陈广鹏 / PNetProcess

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

1 简介

NetProcess是一个在Windows系统中实现TCP连接和数据传输功能的代码模块。

轻便易用,只需4个文件即可在你的代码中整合网络传输功能。

提供性能和可扩展性的保障,模块使用线程池机制实现,同时处理多至数千个连接并收发数据,可同时建立多个网络服务和客户端连接。

适合VC++32位及64位编译环境,测试程序使用Visual Studio 2019及MFC实现。

以下简要说明代码的使用方式,具体实现见测试代码。

2 文件说明

在你的工程中包含以下4个代码文件即可整合网络传输功能:

NetProcess.h -- 网络传输功能代码头文件;

NetProcess.cpp -- 网络传输功能代码实现文件;

PQueue.h -- 一个队列模板类;

PSyncQueue.h -- 一个线程安全的队列模板类。

其他文件为测试程序使用。

3 代码说明

3.1 class PNetLink

指示一个线程中的socket连接,并定义数据连接和发送的功能。其中id指定本地标识,用于使用者标明一个连接。

3.2 class PLinkStat

定义连接状态的类,包括IP地址和收发数据量等信息。

3.3 class PNetItem

包括连接、状态、数据及数据长度的类,使用智能指针实现,可自动销毁资源,便于在线程之间传送状态和数据。

3.4 传输模式

代码中定义以下几种数据传输模式,跟据不同模式收发数据包。模式由listen和connect函数的mode参数指定,要求服务端和客户端一致。

#define _NET_MODE_MESSAGE 1 //字节消息传输,要求指定结束符
#define _NET_MODE_WIDE_MESSAGE 2 //宽字节消息传输,要求指定结束符
#define _NET_MODE_PACKET 3 //整包传输,包头指定数据长度
#define _NET_MODE_FIXED 4 //接收定长数据,不改变发送数据
#define _NET_MODE_CELL 5 //传输(发送和接收)定长数据,发送数据按定长分组
#define _NET_MODE_DATA 6 //传输无格式数据

_NET_MODE_MESSAGE:以结束符分隔的数据传输模式,可用于文本消息的传输。listen和connect函数的letter参数指定分隔符。发送数据时在包的结尾自动添加分隔符,接收数据时检查并去掉分隔符,保证用户收到为完整的数据包(不包括分隔符)。

_NET_MODE_WIDE_MESSAGE:用于宽字节(wchar_t)文本消息的传输,其它与_NET_MODE_MESSAGE模式相同。

_NET_MODE_PACKET:包数据传输模式,可用于二进制数据的传输。发送数据时在用户数据前添加数据长度信息,接收数据时首先接收长度信息,再接收此长度的数据完成后提供给用户,保证用户收到为完整的数据包(不包括附加的数据长度)。

_NET_MODE_FIXED:定长数据包传输模式,发送原始数据,不添加任何信息。listen和connect函数的letter参数指定数据长度,接收到letter长度的数据后提供给用户,保证用户收到该长度的数据包。

_NET_MODE_CELL:信元传输模式,程序将数据分割为指定长度的信元并发送。listen和connect函数的letter参数指定信元数据长度,接收到letter长度的数据后提供给用户。此模式可能用在快速数据转发的场景中;此模式不会将数据组合为完整的数据包,因此需要使用者自定义信元数据的处理方式(例如:使用第一个信元指定接收者、数据类型、长度、信元数等信息)。

_NET_MODE_DATA:无格式数据传输模式,发送和接收原始数据,不添加任何信息。收到的数据可能因缓冲的原因而被分割在不同的包中。

4 使用方法

4.1 启动

初始化网络环境,启动线程。调用startNetProcess函数,指定回调函数。

//启动网络连接线程,参数为指定的数据接收回调函数
bool startNetProcess(PNotifyFunction fun);

程序使用回调机制实现与使用者的通讯,fun 为接收数据和状态消息的回调函数指针;使用者需定义这个函数以接收网络状态和数据通知,回调函数类型的定义如下:

typedef void (*PNotifyFunction)(PNetItem item);//接收网络数据和连接状态的回调函数类型

4.2 结束

调用exitNetProcess函数,退出网络线程,断开所有当前的连接及销毁关联的资源。

//退出网络线程,断开所有当前的连接
void exitNetProcess();

4.3 服务端

建立一个监听连接(同时指定连接模式及参数),调用静态函数listen,代码形式如下:

	PNetLink::listen("0.0.0.0", 9999);

或者

    PNetLink::listen("0.0.0.0", 9999, 0, _NET_MODE_MESSAGE, 127);

listen函数参数说明:

ip,port:ip地址和端口;

_id:本地标识,由使用者指定此连接的编号;

_limit:数据包的最大长度。

//网络服务端,建立网络监听 _id:监听socket的本地标识,limit数据包的最大长度
	static void listen(const char* ip, int port, int _id = 0, int _mode = _NET_MODE_PACKET, int _letter = 0, int _limit = _NET_PACKET_LIMIT);
	static void listen(const wchar_t* ip, int port, int _id = 0, int _mode = _NET_MODE_PACKET, int _letter = 0, int _limit = _NET_PACKET_LIMIT);

接受连接:

程序通过回调函数通知用户,item的code()值为_NET_CODE_ACCEPT时,即为接受到新的连接。

以下代码作为说明,其中item为传入的参数,link为用户定义的连接。

	PNetItem item;//收到的连接参数
	if (item.code() == _NET_CODE_ACCEPT)
	{
		PNetLink link = item;//将收到的连接赋值给本地连接
	}

接受的连接会继承监听连接的属性(包括模式、letter及数据包最大长度)。

4.4 客户端

建立一个连接,调用静态函数connect,代码形式如下:

	PNetLink::connect("127.0.0.1", 9999, 1);

或者

    PNetLink::connect("127.0.0.1", 9999, 1, _NET_MODE_MESSAGE, 127);

connect函数参数说明:

ip,port:ip地址和端口;

_id:本地标识,由使用者指定此连接的编号;

_limit:数据包的最大长度。

//客户端建立网络连接,指定ip地址和端口,_id本地标识,_limit数据包的最大长度
	static void connect(const char* ip, int port, int _id = 0, int _mode = _NET_MODE_PACKET, int _letter = 0, int _limit = _NET_PACKET_LIMIT);
	static void connect(const wchar_t* ip, int port, int _id = 0, int _mode = _NET_MODE_PACKET, int _letter = 0, int _limit = _NET_PACKET_LIMIT);

连接成功通知:

调用connect函数之后并不会立即得到网络连接,程序会调用回调函数通知用户,当item的code()值为_NET_CODE_CONNECT时,表示连接成功。

item为新连接,id()属性确定此连接的身份,因此必要时使用者需在调用connect函数时指定id。

code()值为_NET_FAIL_CONNECT时,表示网络连接失败。

如何接受成功的连接,以下代码作为说明,其中item为传入的参数,link为用户定义的连接。

	PNetItem item;//收到的连接参数
	if (item.code() == _NET_CODE_CONNECT)
	{
		PNetLink link = item;//将收到的连接赋值给本地连接
	}

4.5 发送数据

调用PNetLink类的send函数发送数据。

	//以下发送函数发送数据包,数据传输格式由连接的mode确定,函数将多组数据合并发送
	//CELL模式将每组数据按cell发出,不会跨越组
	void send(const char* p);
	void send(const wchar_t* p);
	void send(const char* p, int len);
	void send(const char* p1, int len1, const char* p2, int len2);
	void send(const char* p1, int len1, const char* p2, int len2, const char* p3, int len3);

4.6 接收数据

程序调用回调函数通知用户,当item的code()值为_NET_CODE_DATA时,表示接收到数据。

_NET_MODE_MESSAGE、_NET_MODE_WIDE_MESSAGE及_NET_MODE_PACKET传输模式,此时收到的为完整数据包(不包括分隔符、数据长度等附加信息);

_NET_MODE_FIXED、_NET_MODE_CELL传输模式,此时收到的为定长(长度为letter)数据包。

对于声明形式如下的回调函数

void recvFunction(PNetItem item);

参数item为收到数据的连接,item的data()和length()属性为数据指针和长度。

程序在发送和接收数据失败时会断开此连接,并向用户通知错误信息。

4.7 网络状态

程序调用回调函数向用户通知网络状态。

对于声明形式如下的回调函数

void recvFunction(PNetItem item);

参数item为状态的连接,item的code()为网络状态码;当item的code()为_NET_CODE_STAT时,state()包含连接状态的信息(ip地址、端口、收发数据量等)。

以下为网络状态编码定义:

#define _NET_CODE_DATA 0
#define _NET_CODE_LISTEN 1
#define _NET_CODE_CONNECT 2
#define _NET_CODE_ACCEPT 3
#define _NET_CODE_DISCONNECT 4
#define _NET_CODE_STAT 5
#define _NET_CODE_EXIT -1
#define _NET_FAIL_THREAD -2
#define _NET_FAIL_LISTEN -3
#define _NET_FAIL_CONNECT -4
#define _NET_FAIL_ACCEPT -5
#define _NET_REJECT_CONNECT -6
#define _NET_LOST_CONNECT -7
#define _NET_FAIL_SEND -8
#define _NET_FAIL_RECV -9
#define _NET_ERROR_EVENT -10

code > 0 时表示一个状态正常的网络通知消息。

code == 0 时表示收到数据的通知消息。

code < 0 时表示网络连接发生错误,这时此连接已经断开。

调用getNetCodeDescribe函数,可获得状态代码的文本描述。

//获得消息编码的文本说明
const char * getNetCodeDescribe(int code);
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

轻量级、简单易用的,在Windows系统中实现TCP连接和数据传输功能的代码。 编译环境Visual Studio 2019,使用MFC实现测试界面。 展开 收起
C++
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chengp2020/pnet-process.git
git@gitee.com:chengp2020/pnet-process.git
chengp2020
pnet-process
PNetProcess
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891