Giter VIP home page Giter VIP logo

myserver's Introduction

出于学习的目的实现一个简单并发服务器

主要组件

  • Socket封装
  • 线程池
  • 日志类
  • 定时器
  • 单例模式
  • 观察者模式
  • 对象池

Socket封装

主要采用IPv4和TCP协议,将c语言网络库的socket, addr_in进行封装,使其更加符合面向对象的编程思维。

线程池

采用最基本的任务队列模型。

日志类

单例模式,使用双缓冲机制,目前仅支持函数式写入,不支持流写入。经测试,1s内大约能写入10w条日志。

定时器

采用时间轮盘算法,经过测试,能够支持10ms粒度的准确定时。经测试,CPU和内存占用率均很低。

单例模式

单例模式采用c++ 11规范静态变量只进行一次初始化的特性。

观察者模式

设置Subject和Observer两个基类,根据自己的用途进行派生,其中Observer是抽象类,必须重写response方法。

对象池

使用成倍扩容机制,使得对象池使用率保持在50%以上(初始化除外,可根据需要设置最小的容量),采用unique_ptr的RAII机制实现对象释放后的自动归还。

编译

mkdir build && cd build
make ..
cmake ..
make

测试

打开一个终端:

cd bin/ && ./Server

java客户端项目在另一个仓库。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.