Skip to content

Commit

Permalink
整理
Browse files Browse the repository at this point in the history
  • Loading branch information
object.he authored and hebaichuan committed Aug 15, 2017
1 parent b163295 commit 206b0f3
Show file tree
Hide file tree
Showing 38 changed files with 3,352 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.h linguist-language=C++
*.lua linguist-language=C++
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# cpp_hodgepodge
cpp杂,cpp11环境。

## lua cpp对象绑定
## C++11 多线程计算n皇后
## libuv C++11封装(TCP网络,定时器,时间轮,信号……)
48 changes: 48 additions & 0 deletions libuv_cpp11/Clinet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef CLIENT_H
#define CLIENT_H

#include <iostream>
#include "uv/TcpClient.h"


class Client : public uv::TcpClient
{
public:
Client(uv_loop_t* loop)
:TcpClient(loop)
{
setConnectCloseCallback(std::bind(&Client::onConnectClose,this));
setConnectCallback(std::bind(&Client::onConnect,this,std::placeholders::_1));
setMessageCallback(std::bind(&Client::newMessage,this,std::placeholders::_1,std::placeholders::_2));
}

void connectToServer(std::string& ip,int port)
{
this->port = port;
this->ip = ip;
std::cout<<ip<<port<<std::endl;
connect(ip.c_str(),port);
}

void onConnectClose()
{
connect(ip.c_str(),port);
}
void onConnect(bool successed)
{
if(!successed)
{
connect(ip.c_str(),port);
}
}

void newMessage(const char* buf,ssize_t size)
{
write(buf,size,nullptr);
}

private:
std::string ip;
int port;
};
#endif
27 changes: 27 additions & 0 deletions libuv_cpp11/EchoServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "EchoServer.h"
#include <cstring>

using namespace uv;
using namespace std;

EchoServer::EchoServer(uv_loop_t* loop,int port ,const char* ip)
:TcpServer(loop,port,ip)
{
setMessageCallback(std::bind(&EchoServer::newMessage,this,placeholders::_1,placeholders::_2,placeholders::_3));
}

void EchoServer::newMessage(shared_ptr<TcpConnection> connection,const char* buf,ssize_t size)
{
connection->write(buf,size,nullptr);

#if 0
char temp[] = {"hello world"};
char* data = new char [11]();
memcpy(data,temp,11);
connection->writeInLoop(data,11,
[](char* buf,unsigned int size)
{
delete [] buf;
});
#endif
}
15 changes: 15 additions & 0 deletions libuv_cpp11/EchoServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef ECHOSERVER_H
#define ECHOSERVER_H

#include "uv/TcpServer.h"


class EchoServer :public uv::TcpServer
{
public:
EchoServer(uv_loop_t* loop,int port ,const char* ip);
private :
void newMessage(std::shared_ptr<uv::TcpConnection> connection,const char* buf,ssize_t size);
};

#endif // ECHOSERVER_H
21 changes: 21 additions & 0 deletions libuv_cpp11/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include "EchoServer.h"
#include "uv/SignalCtrl.h"
#include "Clinet.h"

using namespace uv;

int main(int argc,char** args)
{
uv_loop_t* loop = uv_default_loop();
SignalCtrl signalCtrl(loop);

EchoServer server(loop, 10001, "0.0.0.0");
server.setTimeout(10);
server.start();

Client client(loop);
std::string ip ("192.168.1.132");
client.connectToServer(ip,10002);
return ::uv_run(loop, UV_RUN_DEFAULT);
}
37 changes: 37 additions & 0 deletions libuv_cpp11/uv/SignalCtrl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2017, [email protected] All rights reserved.
Author: [email protected]
Last modified: 2017-8-8
Description:
*/

#include <iostream>
#include "SignalCtrl.h"

using namespace uv;
using namespace std;

SignalCtrl::SignalCtrl(uv_loop_t* loop)
{
::uv_signal_init(loop, &signal);
::uv_signal_start(&signal,&SignalCtrl::onSignal, SIGPIPE);
}

void SignalCtrl::onSignal(uv_signal_t* handle, int signum)
{
switch (signum)
{

case SIGPIPE:
{
cout<<"signal: broken pipe"<<endl;
}
default:
cout<<"signal:"<<signum<<endl;
break;
}

}
31 changes: 31 additions & 0 deletions libuv_cpp11/uv/SignalCtrl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2017, [email protected] All rights reserved.
Author: [email protected]
Last modified: 2017-8-8
Description:
*/

#ifndef SIGNAL_CTRL_H
#define SIGNAL_CTRL_H

#include <uv.h>


namespace uv
{

class SignalCtrl
{
public:
SignalCtrl(uv_loop_t* loop);
private:
uv_signal_t signal;

static void onSignal(uv_signal_t* handle, int signum);
};

}
#endif
86 changes: 86 additions & 0 deletions libuv_cpp11/uv/TcpAccepter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2017, [email protected] All rights reserved.
Author: [email protected]
Last modified: 2017-8-8
Description:
*/

#include "TcpAccepter.h"
#include <iostream>


using namespace std;
using namespace uv;

TcpAccepter::TcpAccepter(uv_loop_t* loop,const char* ip,int port)
:loop(nullptr),
newConnectionCallback(nullptr)
{
struct sockaddr_in addr;
this->loop =loop;
::uv_ip4_addr(ip, port,&addr);
::uv_tcp_init(loop, &server);
::uv_tcp_bind(&server, (const sockaddr*)&addr,0);
server.data = (void* )this;
}



TcpAccepter:: ~TcpAccepter()
{

}

uv_loop_t* TcpAccepter::getLoop()
{
return loop;
}

void TcpAccepter::onNewConnect(uv_tcp_t* client)
{
if(nullptr !=newConnectionCallback)
{
newConnectionCallback(loop,client);
}
}

void TcpAccepter::listen()
{
::uv_listen((uv_stream_t*) &server, 128,
[](uv_stream_t *server, int status)
{
if (status < 0)
{
cout<< "New connection error"<<uv_strerror(status)<<endl;
return;
}
TcpAccepter* accept = (TcpAccepter*)(server->data);
uv_tcp_t* client =new uv_tcp_t();
uv_tcp_init(accept->getLoop(), client);

if ( 0 == ::uv_accept(server, (uv_stream_t*) client))
{
accept->onNewConnect(client);
}
else
{
::uv_close((uv_handle_t*) client, NULL);
delete client;
}
});
listened = true;
}


bool TcpAccepter::isListen()
{
return listened;
}

void TcpAccepter::setNewConnectinonCallback(NewConnectionCallback callback)
{
newConnectionCallback = callback;
}
46 changes: 46 additions & 0 deletions libuv_cpp11/uv/TcpAccepter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2017, [email protected] All rights reserved.
Author: [email protected]
Last modified: 2017-8-8
Description:
*/

#ifndef TCP_ACCEPTER_H
#define TCP_ACCEPTER_H

#include <uv.h>
#include <functional>

namespace uv
{

typedef std::function<void(uv_loop_t* ,uv_tcp_t*)> NewConnectionCallback;

class TcpAccepter
{
public:
TcpAccepter(uv_loop_t* loop,const char* ip,int port);
virtual ~TcpAccepter();

void listen();
bool isListen();
void setNewConnectinonCallback( NewConnectionCallback callback);

uv_loop_t* getLoop();
void onNewConnect(uv_tcp_t* client);

private:
bool listened;
uv_loop_t* loop;
NewConnectionCallback newConnectionCallback;

uv_tcp_t server;

};

}

#endif
Loading

0 comments on commit 206b0f3

Please sign in to comment.