├── .gitignore
├── .gitmodules
├── CMakeLists.txt
├── README.md
├── core
├── CMakeLists.txt
├── UdpAcceptor.cpp
├── UdpAcceptor.h
├── UdpCallbacks.h
├── UdpClient.cpp
├── UdpClient.h
├── UdpConnection.cpp
├── UdpConnection.h
├── UdpConnector.cpp
├── UdpConnector.h
├── UdpServer.cpp
├── UdpServer.h
├── UdpSocketsOps.cpp
├── UdpSocketsOps.h
└── any.h
├── test
├── CMakeLists.txt
├── tcp_chat_client.cpp
├── tcp_chat_server.cpp
├── tcp_codec.h
├── udp_chat_client.cpp
├── udp_chat_server.cpp
└── udp_codec.h
├── third
├── muduo-win
│ ├── .gitignore
│ ├── LICENSE
│ ├── README.md
│ └── src
│ │ ├── alds
│ │ └── min_heap.h
│ │ ├── base
│ │ ├── Mutex.h
│ │ ├── async_logging.cc
│ │ ├── async_logging.h
│ │ ├── blocking_queue.h
│ │ ├── bounded_blocking_queue.h
│ │ ├── config_file_reader.cc
│ │ ├── config_file_reader.h
│ │ ├── copyable.h
│ │ ├── count_down_latch.cc
│ │ ├── count_down_latch.h
│ │ ├── file_util.cc
│ │ ├── file_util.h
│ │ ├── log_file.cc
│ │ ├── log_file.h
│ │ ├── log_stream.cc
│ │ ├── log_stream.h
│ │ ├── logging.cc
│ │ ├── logging.h
│ │ ├── security
│ │ │ ├── aes.h
│ │ │ ├── aes_core.cpp
│ │ │ ├── aes_locl.h
│ │ │ ├── base64.cpp
│ │ │ ├── base64.h
│ │ │ ├── md5.cpp
│ │ │ └── md5.h
│ │ ├── stringpiece.h
│ │ ├── thread.h
│ │ ├── thread_pool.cc
│ │ ├── thread_pool.h
│ │ ├── timestamp.cc
│ │ ├── timestamp.h
│ │ ├── types.h
│ │ ├── uncopyable.h
│ │ ├── win_time.cc
│ │ └── win_time.h
│ │ ├── net
│ │ ├── Acceptor.cc
│ │ ├── Acceptor.h
│ │ ├── Buffer.cc
│ │ ├── Buffer.h
│ │ ├── Callbacks.h
│ │ ├── Channel.cc
│ │ ├── Channel.h
│ │ ├── Connector.cc
│ │ ├── Connector.h
│ │ ├── Endian.h
│ │ ├── EventLoop.cc
│ │ ├── EventLoop.h
│ │ ├── EventLoopThread.cc
│ │ ├── EventLoopThread.h
│ │ ├── EventLoopThreadPool.cc
│ │ ├── EventLoopThreadPool.h
│ │ ├── InetAddress.cc
│ │ ├── InetAddress.h
│ │ ├── Poller.cc
│ │ ├── Poller.h
│ │ ├── Poller
│ │ │ ├── DefaultPoller.cc
│ │ │ ├── SelectPoller.cc
│ │ │ └── SelectPoller.h
│ │ ├── Socket.cc
│ │ ├── Socket.h
│ │ ├── SocketsOps.cc
│ │ ├── SocketsOps.h
│ │ ├── TcpClient.cc
│ │ ├── TcpClient.h
│ │ ├── TcpConnection.cc
│ │ ├── TcpConnection.h
│ │ ├── TcpServer.cc
│ │ ├── TcpServer.h
│ │ ├── TimeMinHeap.h
│ │ ├── Timer.cc
│ │ ├── Timer.h
│ │ ├── TimerId.h
│ │ ├── TimerQueue.cc
│ │ ├── TimerQueue.h
│ │ └── poll.h
│ │ └── test
│ │ ├── ThreadPool_test.cc
│ │ ├── acceptor_test.cc
│ │ ├── async_logging_test.cc
│ │ ├── config_file_read_test.cc
│ │ ├── connector_test.cc
│ │ ├── echoclient_test.cc
│ │ ├── echoserver_test.cc
│ │ ├── eventloop_test.cc
│ │ ├── eventloopthread_test.cc
│ │ ├── eventloopthreadpool_test.cc
│ │ ├── log_file_test.cc
│ │ ├── log_stream_bench.cc
│ │ ├── logging_test.cc
│ │ ├── min_heap_test.cc
│ │ ├── security_test.cc
│ │ ├── socketsOps_test.cc
│ │ ├── tcpclient_test.cc
│ │ ├── time_min_heap_test.cc
│ │ └── timerqueue_test.cc
└── muduo
│ ├── CMakeLists.txt
│ ├── base
│ ├── Atomic.h
│ ├── Condition.cc
│ ├── Condition.h
│ ├── CountDownLatch.cc
│ ├── CountDownLatch.h
│ ├── CurrentThread.h
│ ├── Date.cc
│ ├── Date.h
│ ├── Exception.cc
│ ├── Exception.h
│ ├── LogStream.cc
│ ├── LogStream.h
│ ├── Logging.cc
│ ├── Logging.h
│ ├── Mutex.h
│ ├── StringPiece.h
│ ├── Thread.cc
│ ├── Thread.h
│ ├── ThreadLocal.h
│ ├── ThreadLocalSingleton.h
│ ├── ThreadPool.cc
│ ├── ThreadPool.h
│ ├── TimeZone.cc
│ ├── TimeZone.h
│ ├── Timestamp.cc
│ ├── Timestamp.h
│ ├── Types.h
│ ├── WeakCallback.h
│ ├── copyable.h
│ └── noncopyable.h
│ └── net
│ ├── Acceptor.cc
│ ├── Acceptor.h
│ ├── Buffer.cc
│ ├── Buffer.h
│ ├── Callbacks.h
│ ├── Channel.cc
│ ├── Channel.h
│ ├── Connector.cc
│ ├── Connector.h
│ ├── Endian.h
│ ├── EventLoop.cc
│ ├── EventLoop.h
│ ├── EventLoopThread.cc
│ ├── EventLoopThread.h
│ ├── EventLoopThreadPool.cc
│ ├── EventLoopThreadPool.h
│ ├── InetAddress.cc
│ ├── InetAddress.h
│ ├── Poller.cc
│ ├── Poller.h
│ ├── Socket.cc
│ ├── Socket.h
│ ├── SocketsOps.cc
│ ├── SocketsOps.h
│ ├── TcpClient.cc
│ ├── TcpClient.h
│ ├── TcpConnection.cc
│ ├── TcpConnection.h
│ ├── TcpServer.cc
│ ├── TcpServer.h
│ ├── Timer.cc
│ ├── Timer.h
│ ├── TimerId.h
│ ├── TimerQueue.cc
│ ├── TimerQueue.h
│ └── poller
│ ├── DefaultPoller.cc
│ ├── EPollPoller.cc
│ ├── EPollPoller.h
│ ├── PollPoller.cc
│ └── PollPoller.h
└── vs2015
└── realtinet
├── .gitignore
├── realtinet.sln
└── realtinet
├── realtinet.vcxproj
└── realtinet.vcxproj.filters
/.gitignore:
--------------------------------------------------------------------------------
1 | .history/
2 |
3 | # Visual Studio 2015 user specific files
4 | .vs/
5 |
6 | # VisualStudioCode
7 | .vscode
8 |
9 | build/
10 |
11 | bin_rs
12 |
13 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "third/kcpp"]
2 | path = third/kcpp
3 | url = git@github.com:no5ix/kcpp.git
4 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 |
2 | cmake_minimum_required (VERSION 2.8)
3 |
4 | # CMake disable -std=c++11 flag for C files
5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y")
6 |
7 | project (realtinet)
8 |
9 | set_property(GLOBAL PROPERTY USE_FOLDERS On)
10 |
11 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
12 | set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
13 |
14 |
15 | if(UNIX)
16 |
17 | if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
18 | # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
19 | # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w") # Inhibit all warning messages.
20 | set(CXX_FLAGS
21 | -g
22 | # -DVALGRIND
23 | # -DMUDUO_STD_STRING
24 | -DCHECK_PTHREAD_RETURN_VALUE
25 | -D_FILE_OFFSET_BITS=64
26 | -Wall
27 | -Wextra
28 | -Werror
29 | # -Wconversion
30 | -Wno-unused-parameter
31 | # -Wold-style-cast
32 | -Wno-multichar
33 | -Wno-unused-function
34 | -Wno-reorder
35 | -Woverloaded-virtual
36 | -Wpointer-arith
37 | # -Wshadow
38 | -Wwrite-strings
39 | -march=native
40 | # -MMD
41 | -std=c++0x
42 | -rdynamic
43 | )
44 | string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")
45 | endif()
46 |
47 | # For gdb
48 | set(CMAKE_BUILD_TYPE "Debug")
49 | set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
50 | set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
51 |
52 |
53 | include_directories(${PROJECT_SOURCE_DIR})
54 | include_directories(${PROJECT_SOURCE_DIR}/core)
55 | include_directories(${PROJECT_SOURCE_DIR}/third)
56 | include_directories(${PROJECT_SOURCE_DIR}/third/muduo)
57 |
58 | add_subdirectory(core)
59 | add_subdirectory(test)
60 | add_subdirectory(third/muduo)
61 | add_subdirectory(third/kcpp)
62 |
63 | else()
64 | message(SEND_ERROR "You are on an unsupported platform! (Not Unix)")
65 | ENDIF()
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | [muduo](https://github.com/chenshuo/muduo) + [kcp](https://github.com/skywind3000/kcp), a fast and two-channel (reliable & unreliable) udp lib.
4 |
5 | 一个快速的双通道(可靠与非可靠)udp网络库, 基于 muduo + kcp .
6 |
7 | # About This
8 |
9 |
10 | - realtinet = [kcp](https://github.com/skywind3000/kcp) + ([kcpp](https://github.com/no5ix/kcpp), a kcp session implementation) + [muduo](https://github.com/chenshuo/muduo) + (UDP support for muduo)
11 | - used in [realtime-server](https://github.com/no5ix/realtime-server) (A open source realtime dedicated game server for FPS / MOBA ), [Video Preview 视频演示](https://github.com/no5ix/realtime-server/blob/master/img/UE4DemoScreenshot.gif)
12 |
13 |
14 | # QQ群
15 |
16 | 496687140
--------------------------------------------------------------------------------
/core/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | set (PROJ_NAME realtinet)
2 | set (MUDUO_LIB_NAME muduo)
3 | set (KCPSESS_LIB_NAME kcpp)
4 |
5 | IF(UNIX)
6 |
7 | set(realtinet_SRCS
8 | UdpAcceptor.cpp
9 | UdpClient.cpp
10 | UdpConnection.cpp
11 | UdpConnector.cpp
12 | UdpServer.cpp
13 | UdpSocketsOps.cpp
14 | )
15 |
16 | add_library(${PROJ_NAME} ${realtinet_SRCS})
17 | target_link_libraries(${PROJ_NAME} ${MUDUO_LIB_NAME} ${KCPSESS_LIB_NAME})
18 |
19 | # elseif(WIN32) # Check if we are on Windows
20 |
21 | # create_win_proj(${PROJ_NAME})
22 |
23 | else()
24 | message(SEND_ERROR "You are on an unsupported platform! (Not Win32 or Unix)")
25 | ENDIF()
--------------------------------------------------------------------------------
/core/UdpAcceptor.cpp:
--------------------------------------------------------------------------------
1 | #include "UdpAcceptor.h"
2 | #include "UdpSocketsOps.h"
3 | #include "UdpConnector.h"
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | #include
11 | #include
12 | //#include
13 | //#include
14 |
15 | #ifndef _WIN32
16 | #include
17 | #endif
18 |
19 | using namespace muduo;
20 | using namespace muduo::net;
21 |
22 |
23 | UdpAcceptor::UdpAcceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport)
24 | : loop_(loop),
25 | acceptSocket_(sockets::createUdpNonblockingOrDie(listenAddr.family())),
26 | acceptChannel_(loop, acceptSocket_.fd()),
27 | listenning_(false),
28 | listenPort_(listenAddr.toPort()),
29 | listenAddr_(listenAddr)
30 | {
31 | acceptSocket_.setReuseAddr(true);
32 | acceptSocket_.setReusePort(reuseport);
33 | acceptSocket_.bindAddress(listenAddr);
34 |
35 | acceptChannel_.setReadCallback(
36 | std::bind(&UdpAcceptor::handleRead, this));
37 | }
38 |
39 | UdpAcceptor::~UdpAcceptor()
40 | {
41 | acceptChannel_.disableAll();
42 | acceptChannel_.remove();
43 |
44 | peerAddrToUdpConnectors_.empty();
45 | }
46 |
47 | void UdpAcceptor::listen()
48 | {
49 | loop_->assertInLoopThread();
50 | listenning_ = true;
51 |
52 | //#ifdef _WIN32
53 | // acceptSocket_.listen();
54 | //#endif
55 |
56 | acceptChannel_.enableReading();
57 | }
58 |
59 | void UdpAcceptor::handleRead()
60 | {
61 | loop_->assertInLoopThread();
62 | InetAddress peerAddr;
63 |
64 | struct sockaddr_in6 addr;
65 | //struct sockaddr_in addr;
66 | //bzero( &addr, sizeof addr );
67 | memset(&addr, 0, sizeof(addr));
68 | int readByteCount = sockets::recvfrom(acceptSocket_.fd(), &addr, recvfromBuf_, krecvfromBufSize);
69 |
70 | if (readByteCount >= 0)
71 | {
72 | peerAddr.setSockAddrInet6(addr);
73 | //peerAddr.setSockAddrIn(addr);
74 |
75 | if (peerAddrToUdpConnectors_.find(peerAddr)
76 | == peerAddrToUdpConnectors_.end()) // check whether is connecting
77 | {
78 | UdpConnectorPtr newUdpConnector(new UdpConnector(loop_, peerAddr, listenAddr_));
79 | peerAddrToUdpConnectors_[peerAddr] = newUdpConnector;
80 |
81 | newUdpConnector->setNewConnectionCallback(
82 | std::bind(&UdpAcceptor::newConnection, this, _1, peerAddr));
83 | newUdpConnector->start();
84 | }
85 | }
86 | else
87 | {
88 | LOG_SYSERR << "in UdpAcceptor::handleRead";
89 | }
90 | }
91 |
92 | void UdpAcceptor::newConnection(Socket* connectedSocket,
93 | const InetAddress& peerAddr)
94 | {
95 | loop_->assertInLoopThread();
96 | if (newConnectionCallback_)
97 | {
98 | newConnectionCallback_(connectedSocket, peerAddr);
99 | }
100 | else
101 | {
102 | //sockets::close( connfd );
103 | }
104 | }
105 |
106 | void UdpAcceptor::RemoveConnector(const InetAddress& peerAddr)
107 | {
108 | loop_->assertInLoopThread();
109 | assert(peerAddrToUdpConnectors_[peerAddr]);
110 | (peerAddrToUdpConnectors_[peerAddr])->stop();
111 | loop_->runAfter(1, std::bind(&UdpAcceptor::EraseConnector, this, peerAddr));
112 | }
113 |
114 | void UdpAcceptor::EraseConnector(const InetAddress& peerAddr)
115 | {
116 | loop_->assertInLoopThread();
117 | peerAddrToUdpConnectors_.erase(peerAddr);
118 | }
--------------------------------------------------------------------------------
/core/UdpAcceptor.h:
--------------------------------------------------------------------------------
1 |
2 | #ifndef UDP_MUDUO_NET_ACCEPTOR_H
3 | #define UDP_MUDUO_NET_ACCEPTOR_H
4 |
5 | #include
6 | #include