no message

master
zct 2021-07-06 23:30:40 +08:00
parent 2a3e08582f
commit 37b0f641d9
9 changed files with 104 additions and 367 deletions

View File

@ -1,34 +0,0 @@
{
"graph_lock": {
"nodes": {
"0": {
"options": "OPENSSL:shared=False\nlibevent:shared=False",
"requires": [
"1"
],
"path": "conanfile.txt",
"context": "host"
},
"1": {
"ref": "libevent/2.1.2",
"options": "shared=False\nOPENSSL:shared=False",
"package_id": "915fde0f08e1a7b1285eb9692add2dc53fa23d1a",
"prev": "0",
"requires": [
"2"
],
"context": "host"
},
"2": {
"ref": "OPENSSL/1.1.1",
"options": "shared=False",
"package_id": "58687b9860a7859264803e7743813436a963299d",
"prev": "0",
"context": "host"
}
},
"revisions_enabled": false
},
"version": "0.4",
"profile_host": "[settings]\narch=x86_64\narch_build=x86_64\nbuild_type=Release\ncompiler=gcc\ncompiler.libcxx=libstdc++11\ncompiler.version=7.5\nos=Linux\nos_build=Linux\n[options]\n[build_requires]\n[env]\ncc=gcc"
}

View File

@ -1,190 +0,0 @@
[includedirs]
/root/.conan/data/libevent/2.1.2/_/_/package/915fde0f08e1a7b1285eb9692add2dc53fa23d1a/include
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/include
[libdirs]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/lib
[bindirs]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/bin
[resdirs]
[builddirs]
/root/.conan/data/libevent/2.1.2/_/_/package/915fde0f08e1a7b1285eb9692add2dc53fa23d1a/
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/
[libs]
libevent_core.a
libevent.a
libevent_openssl.a
libevent_extra.a
[system_libs]
[defines]
[cppflags]
[cxxflags]
[cflags]
[sharedlinkflags]
[exelinkflags]
[sysroot]
[frameworks]
[frameworkdirs]
[includedirs_libevent]
/root/.conan/data/libevent/2.1.2/_/_/package/915fde0f08e1a7b1285eb9692add2dc53fa23d1a/include
[libdirs_libevent]
[bindirs_libevent]
[resdirs_libevent]
[builddirs_libevent]
/root/.conan/data/libevent/2.1.2/_/_/package/915fde0f08e1a7b1285eb9692add2dc53fa23d1a/
[libs_libevent]
libevent_core.a
libevent.a
libevent_openssl.a
libevent_extra.a
[system_libs_libevent]
[defines_libevent]
[cppflags_libevent]
[cxxflags_libevent]
[cflags_libevent]
[sharedlinkflags_libevent]
[exelinkflags_libevent]
[sysroot_libevent]
[frameworks_libevent]
[frameworkdirs_libevent]
[rootpath_libevent]
/root/.conan/data/libevent/2.1.2/_/_/package/915fde0f08e1a7b1285eb9692add2dc53fa23d1a
[name_libevent]
libevent
[version_libevent]
2.1.2
[generatornames_libevent]
cmake_find_package=libevent
cmake_find_package_multi=libevent
[generatorfilenames_libevent]
[includedirs_OPENSSL]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/include
[libdirs_OPENSSL]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/lib
[bindirs_OPENSSL]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/bin
[resdirs_OPENSSL]
[builddirs_OPENSSL]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d/
[libs_OPENSSL]
[system_libs_OPENSSL]
[defines_OPENSSL]
[cppflags_OPENSSL]
[cxxflags_OPENSSL]
[cflags_OPENSSL]
[sharedlinkflags_OPENSSL]
[exelinkflags_OPENSSL]
[sysroot_OPENSSL]
[frameworks_OPENSSL]
[frameworkdirs_OPENSSL]
[rootpath_OPENSSL]
/root/.conan/data/OPENSSL/1.1.1/_/_/package/58687b9860a7859264803e7743813436a963299d
[name_OPENSSL]
OPENSSL
[version_OPENSSL]
1.1.1
[generatornames_OPENSSL]
[generatorfilenames_OPENSSL]
[USER_OPENSSL]
[USER_libevent]
[ENV_libevent]
[ENV_OPENSSL]

View File

@ -1,37 +0,0 @@
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=7.5
os=Linux
[requires]
libevent/2.Y.Z
[options]
[full_settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=7.5
os=Linux
os_build=Linux
[full_requires]
OPENSSL/1.1.1:58687b9860a7859264803e7743813436a963299d
libevent/2.1.2:915fde0f08e1a7b1285eb9692add2dc53fa23d1a
[full_options]
OPENSSL:shared=False
libevent:shared=False
[recipe_hash]
[env]
cc=gcc

View File

@ -1,18 +0,0 @@
{
"options": [
[
"OPENSSL:shared",
"False"
],
[
"libevent:shared",
"False"
]
],
"root": {
"name": null,
"version": null,
"user": null,
"channel": null
}
}

View File

@ -1,11 +1,13 @@
/*
* @Author: your name
* @Date: 2021-06-30 10:02:04
* @LastEditTime: 2021-07-05 22:49:38
* @LastEditTime: 2021-07-06 21:55:15
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\main.cpp
*/
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include <iostream>
#include "tcp_server_libevent.h"
#ifdef WIN32

View File

@ -1,11 +1,14 @@
/*
* @Author: your name
* @Date: 2021-06-30 10:02:04
* @LastEditTime: 2021-07-05 00:55:11
* @LastEditTime: 2021-07-06 23:03:05
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\main.cpp
*/
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include <iostream>
#include "tcp_swarm_libevent.h"
#ifdef WIN32
@ -46,9 +49,9 @@ int main(){
return 0;
}
#endif
std::cout<<"start server libevent\r\n";
TcpSwarmClientLibevent swarm(8000);
swarm.ConnectToServer("127.0.0.1",8000);
std::cout<<"start swarm client libevent\r\n";
TcpSwarmClientLibevent swarm(20);
swarm.ConnectToServer("192.168.100.110",8000);
while(true){
#ifdef WIN32
Sleep(1000);

View File

@ -1,17 +1,19 @@
//
// Created by 29019 on 2020/4/18.
//
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include "tcp_client.h"
#include <stdio.h>
#include <cstring>
#include <string.h>
static void conn_writecb(struct bufferevent *, void *);
static void conn_readcb(struct bufferevent *, void *);
static void conn_eventcb(struct bufferevent *, short, void *);
void delay(int ms);
int ThreadRun(TcpClientLibevent *p);
//conn_writecwritecb函数将在bufferevent中的output evbuffer缓冲区发送完成后被调用。
//此时evbuffer_get_length(output) = 0说明output evbuffer缓冲区被清空。
//假设发现有10000条记录要发送出去1次发送10000条将占用大量内存所以我们要分批发送
@ -20,21 +22,7 @@ int ThreadRun(TcpClientLibevent *p);
//数据
void conn_writecb(struct bufferevent *bev, void *user_data)
{
// struct evbuffer *output = bufferevent_get_output(bev);
// if (evbuffer_get_length(output) == 0)
// {
// printf("Output evbuffer is flushed\n");
// bufferevent_free(bev);
// }
//delay 1 second
//delay(1000);
//static int msg_num = 1;
//char reply_msg[1000] = { '\0' };
//char *str = "I receive a message from client ";
//memcpy(reply_msg, str, strlen(str));
//sprintf(reply_msg + strlen(str), "%d", msg_num);
//bufferevent_write(bev, reply_msg, strlen(reply_msg));
//msg_num++;
}
// 运行线程

View File

@ -1,6 +1,13 @@
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include "tcp_server_libevent.h"
#include <cstring>
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent::ConnectionLibevent(TcpServerLibevent *p,struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1):
m_parent_server(nullptr),
m_event(nullptr),
@ -12,7 +19,11 @@ m_addr(nullptr)
m_fd = fd;
m_addr = p1;
}
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent::ConnectionLibevent(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1):
m_parent_server(nullptr),
m_event(nullptr),
@ -23,26 +34,47 @@ m_addr(nullptr)
m_fd = fd;
m_addr = p1;
}
/**
* @description:
* @param {*}
* @return {*}
*/
ConnectionLibevent* defaultConnAccept(struct bufferevent *ev,uint32_t fd,struct sockaddr_in *p1){
return new ConnectionLibevent(ev,fd,p1);
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnRecv(char *p,uint32_t len){
std::cout<<"OnRecv "<<p<<std::endl;
m_bytes_recv += len;
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnClose(){
std::cout<<"close "<<this->m_fd << " "<<this->IpAddress()<<std::endl;
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::OnWrite(){
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::WriteData(const char *p,uint16_t len){
if(nullptr == p){
return -1;
@ -53,7 +85,11 @@ int ConnectionLibevent::WriteData(const char *p,uint16_t len){
uint32_t ConnectionLibevent::SocketFd(){
return m_fd;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int ConnectionLibevent::SetServer(TcpServerLibevent *p){
if(nullptr != p){
this->m_parent_server = p;
@ -81,41 +117,46 @@ void read_cb(struct bufferevent *bev, void *arg)
cout << "client " << conn->IpAddress() << " say:" << buf << endl;
conn->OnRecv(buf,sizeof(buf));
}
/************************************
@ Author: woniu201
@ Created: 2018/11/21
@ Return:
************************************/
void write_cb(struct bufferevent *bev, void *arg)
{
ConnectionLibevent* conn = (ConnectionLibevent*)arg;
std::cout<<"connection "<<conn->IpAddress()<<" sended data success"<< std::endl;
}
/**
* @description:
* @param {*}
* @return {*}
*/
void event_cb(struct bufferevent *bev, short events, void *arg)
{
ConnectionLibevent *conn = (ConnectionLibevent*)(arg);
TcpServerLibevent *server = conn->Server();
if (events & BEV_EVENT_EOF)
{
conn->OnClose();
cout << "connection closed: " << conn->IpAddress() << " " << conn->SocketFd() << endl;
conn->OnClose();
bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd());
}
else if (events & BEV_EVENT_ERROR)
{
conn->OnClose();
bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd());
cout << "some other error !" << endl;
}
bufferevent_free(bev);
server->RemoveConnection(conn->SocketFd());
delete conn;
}
void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr)
{
/**
* @description:
* @param {*}
* @return {*}
*/
void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr) {
struct sockaddr_in* client = (sockaddr_in*)addr ;
cout << "connect new client: " << inet_ntoa(client->sin_addr) << fd << " ::"<< ntohs(client->sin_port)<< endl;
cout << "connect new client: " << inet_ntoa(client->sin_addr) << " " << fd << " ::"<< ntohs(client->sin_port)<< endl;
TcpServerLibevent *server = (TcpServerLibevent*)ptr;
if(server != nullptr){
struct bufferevent *bev;
@ -128,44 +169,11 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
}
}
int test_tcp_server()
{
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
(void)WSAStartup(wVersionRequested, &wsaData);
#endif
// init server
struct sockaddr_in serv;
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(8888);
serv.sin_addr.s_addr = htonl(INADDR_ANY);
struct event_base * base;
base = event_base_new();
struct evconnlistener* listener;
listener = evconnlistener_new_bind(base,
cb_listener,
base,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
30000,
(struct sockaddr*)&serv,
sizeof(serv));
if(NULL != listener){
event_base_dispatch(base);
evconnlistener_free(listener);
event_base_free(base);
return 0;
}else{
return -1;
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
void server_run(TcpServerLibevent *p){
if(nullptr != p){
if(p->m_status == TcpServerLibevent::STOP){
@ -176,7 +184,6 @@ void server_run(TcpServerLibevent *p){
}
}
}
/**
* @description:
* @param {*}
@ -185,7 +192,11 @@ void server_run(TcpServerLibevent *p){
TcpServerLibevent::SERVER_STATUS TcpServerLibevent::Status(){
return m_status;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){
if( m_map_client.find(fd) == m_map_client.end()){
if(nullptr != p)
@ -195,7 +206,11 @@ int TcpServerLibevent::AddConnection(uint32_t fd,ConnectionLibevent *p){
}
return 0;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::RemoveConnection(uint32_t fd){
if( m_map_client.find(fd) != m_map_client.end()){
m_map_client.erase(fd);
@ -204,12 +219,15 @@ if( m_map_client.find(fd) != m_map_client.end()){
return -1;
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::SetNewConnectionHandle(OnAccept p){
m_handle_accept = p;
return 0;
}
/**
* @description:
* @param {int} ports
@ -284,7 +302,11 @@ int TcpServerLibevent::StartServerAsync(){
}
return -1;
}
/**
* @description: start server asynchronous
* @param {*}
* @return {*}
*/
TcpServerLibevent::~TcpServerLibevent(){
if(this->m_status == RUNNING){
m_thread->detach();

View File

@ -1,11 +1,13 @@
/*
* @Author: your name
* @Date: 2021-07-04 16:06:47
* @LastEditTime: 2021-07-05 11:11:00
* @LastEditTime: 2021-07-06 22:40:15
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\tcp_swarm_libevent.cpp
*/
#define _WSPIAPI_H_
#define _WINSOCKAPI_
#include"tcp_swarm_libevent.h"
#include <cstring>
void conn_writecb(struct bufferevent *, void *);
@ -48,6 +50,7 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
if (events & BEV_EVENT_EOF)
{
evutil_socket_t fd = bufferevent_getfd(bev);
bufferevent_free(bev);
server->removeConection(fd);
}
else if (events & BEV_EVENT_ERROR)
@ -59,7 +62,6 @@ void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
evutil_socket_t fd = bufferevent_getfd(bev);
server->addConection(fd,bev);
}
bufferevent_free(bev);
}
@ -120,7 +122,6 @@ int TcpSwarmClientLibevent::ConnectToServer(string server, int port){
return -1;
}
}
this->m_thread = new thread(thread_dispatch,this);
return 0;
}