123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- #include <log.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <stdio.h>
- #include <list>
- #include <vector>
- #include <set>
- #include <thread>
- #include <mutex>
- #include <atomic>
- #include <algorithm>
- #include <fstream>
- #include <time.h>
- #include <zio.h>
- #include <znet.h>
- #include <zloop.h>
- #include <clock.h>
- #include <worker.h>
- #include "config_file.h"
- #include "crc.h"
- #include "module_service/module_device_net.h"
- extern config_file config;
- struct client_ex:client
- {
- virtual void on_notify()=0;
- virtual void close_impl()=0;
- };
- struct io_context: zloop<std::shared_ptr<client>> ,service_handle
- {
- private:
- service_callback&m_serv;
- ev::timer m_timer;
- public:
-
- std::vector<std::shared_ptr<client>> m_thread_clts;
- io_context(service_callback&serv)
- :m_serv(serv)
- ,m_timer(*this)
- {
- m_thread_clts.reserve(2048);
- m_timer.set<io_context,&io_context::on_timer>(this);
- m_timer.start(0,1);
- }
- virtual ~io_context()
- {
- }
- void boardcast(const std::vector<char>&msg)
- {
- for(const auto&i:m_thread_clts)
- {
- std::vector<char> tmp(msg);
- i->send(std::move(tmp));
- }
- }
- void on_timer()
- {
- m_serv.on_timer();
- }
- void on_connect(const std::shared_ptr<client> &clt)
- {
- if(clt->handle()>=(int)m_thread_clts.size())
- {
- m_thread_clts.resize(clt->handle()+1);
- }
- m_thread_clts[clt->handle()]=clt;
- m_serv.on_connect(clt);
- }
- void on_close(const std::shared_ptr<client> &clt)
- {
- m_serv.on_close(clt);
- m_thread_clts[clt->handle()].reset();
- }
- void on_send_timeout(const std::shared_ptr<client> &clt)
- {
- m_serv.on_send_timeout(clt);
- }
- void on_recv_timeout(const std::shared_ptr<client> &clt)
- {
- m_serv.on_recv_timeout(clt);
- }
- void on_message(const std::shared_ptr<client>& clt,const char*data,size_t len)
- {
- m_serv.on_message(clt,data,len);
- }
- void close_all()
- {
- for(const auto&clt:m_thread_clts)
- {
- if(!clt)
- continue;
- ((client_ex*)clt.get())->close_impl();
- }
- }
- void on_async(const std::list<std::shared_ptr<client>>¬ify_clts)
- {
- for(const auto&clt:notify_clts)
- {
- ((client_ex*)clt.get())->on_notify();
- }
- }
- void stop()
- {
- async_stop();
- }
- };
- struct fd_io:ev::io
- {
- io_context&m_ic;
- int m_fd;
- fd_io(io_context&ic,int fd,int ev_flag=EV_READ)
- :ev::io(ic)
- ,m_ic(ic)
- ,m_fd(fd)
- {
- zio::setblocking(fd,false);
- this->set(this);
- this->set(m_fd,ev_flag);
- this->start();
- }
- void stop () throw () \
- {
- ev::io::stop();
- }
- io_context&context()
- {
- return m_ic;
- }
- virtual void operator()(ev::io &w, int)=0;
- virtual ~fd_io()
- {
- stop();
- zio::close(m_fd);
- }
- };
- struct stdin_io:fd_io
- {
- stdin_io(io_context&ic)
- :fd_io(ic,0)
- {}
- virtual void operator()(ev::io &w, int)
- {
- #if 0
- char buf[256];
- if(!gets(buf))
- return;
- if(strchr(buf,'X'))
- {
- log_info("stdin input 'X', exiting...");
- worker::instance()->stop();
- stop();
- m_ic.stop();
- }
- #endif
- }
- };
- struct sock_client:fd_io,client_ex
- {
- io_context&m_ic;
- std::string m_name;
- std::atomic<bool> m_close_flag{false};
- int m_type=1;//site
- char *m_b{0};
- int m_clen{0};
- int m_size{1<<16};
- int m_max_package_size{2048};
- int m_recv_time_out;
- ev::timer m_recv_timer;
- ev::timer m_sync_timer;
- // ev::timer m_send_timer;
-
- std::mutex m_mutex;
- std::list<std::vector<char>> m_olist;
- std::vector<char> m_obuf;
- size_t m_opos=0;
- bool m_can_write{false};
- int m_site_id{-1};
- sock_client(io_context&ic,const char*name,int fd,int recv_time_out,int max_package_size)
- :fd_io(ic,fd,EV_READ|EV_WRITE)
- ,m_ic(ic)
- ,m_name(name)
- ,m_recv_timer(ic)
- ,m_sync_timer(ic)
- {
- m_max_package_size=max_package_size;
- m_recv_time_out=recv_time_out;
- m_recv_timer.set<sock_client,&sock_client::on_recv_timeout>(this);
- int recv_timeout_first=config.get("service.recv_timeout_first",10);
- m_recv_timer.start(recv_timeout_first,0);
- int site_sync=config.get("site_sync",0);//分站时间同步,考虑到双IP双机情况,缺省关闭
- int site_sync_freq=config.get("site_sync.freq",10*60);//分站时间同步间隔
- m_sync_timer.set<sock_client,&sock_client::on_sync_timeout>(this);
- if(site_sync)
- {
- log_info("启动分站同步定时器:%s,%d",name,site_sync_freq);
- struct timeval tv;
- gettimeofday(&tv,0);
- m_sync_timer.start(2-tv.tv_usec/1000000.+0.01,site_sync_freq);
- }
- // m_send_timer.set(ic);
- // m_send_timer.set(5,0);
- // m_send_timer.set<sock_client,&sock_client::on_send_timeout>(this);
- // m_send_timer.start();
- m_b=(char*)malloc(m_size);
- // m_timestamp[0]=0;
- }
- ~sock_client()
- {
- free(m_b);
- }
- #if 0
- bool check_timestamp(const char*time)
- {
- //秒、分、时、天、周、月、年, 脑残的设计
-
- char buf[6];
- buf[0]=time[6];
- buf[1]=time[5];
- buf[2]=time[3];
- buf[3]=time[2];
- buf[4]=time[1];
- buf[5]=time[0];
- if(memcmp(m_timestamp,buf,6)<=0)
- {
- memcpy(m_timestamp,buf,6);
- return true;
- }
- return false;
- }
- #endif
- int type()
- {
- return m_type;
- }
- void set_conn_type(int t)
- {
- m_type=t;
- }
- void close()
- {
- m_close_flag.store(true);
- m_ic.async_request(shared_from_this());
- }
- void send(std::vector<char>&&b)
- {
- {
- std::unique_lock<std::mutex> _lock(m_mutex);
- m_olist.push_back(std::move(b));
- }
- m_ic.async_request(shared_from_this());
- }
- void on_sync_timeout()
- {
- // 从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
- unsigned char buf[20]={0,13,0x78,0x3b};
- struct timeval tv;
- gettimeofday(&tv,0);
- struct tm buff={0};
- const struct tm*t=localtime_r(&tv.tv_sec,&buff);
- int p=4;
- buf[p++]=(tv.tv_usec/1000)&0xFF;
- buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
- buf[p++]=t->tm_sec;
- buf[p++]=t->tm_min;
- buf[p++]=t->tm_hour;
- buf[p++]=t->tm_mday;
- buf[p++]=t->tm_wday;
- buf[p++]=t->tm_mon+1;
- buf[p++]=t->tm_year-100;
- uint16_t ccrc=do_crc(buf+2,11);
- buf[p++]=ccrc>>8;
- buf[p++]=ccrc&0xff;
- std::vector<char> tmp((char*)buf,(char*)buf+15);
- send(std::move(tmp));
- logn_info(1,"分站同步:ip=%s,time=%d-%02d-%02d %02d:%02d:%02d.%03d",m_name.c_str(),buf[12]+2000,buf[11],buf[9],buf[8],buf[7],buf[6],buf[5]*256+buf[4]);
- }
- void on_send_timeout()
- {
- m_ic.on_send_timeout(shared_from_this());
- }
- void on_recv_timeout()
- {
- m_ic.on_recv_timeout(shared_from_this());
- logn_warn(1,"socket %s recv timeout.",m_name.c_str());
- close_impl();
- }
- std::string name()
- {
- return m_name;
- }
- int handle()
- {
- return m_fd;
- }
- void grow_buf(int len)
- {
- if(m_size-m_clen>=len)
- return;
- int size=m_size;
- while(m_size-m_clen<len)
- size<<=1;
- if(size!=m_size)
- {
- m_b=(char*)realloc(m_b,m_size=size);
- }
- }
- int read_clt()
- {
- for(;m_clen<m_size;)
- {
- int rc=zio::read(m_fd,m_b+m_clen,m_size-m_clen);
- if(rc>0)
- {
- m_clen+=rc;
- continue;
- }
- if(rc==-2)
- return 0;
- if(rc==0)
- {
- logn_info(1,"socket %d(%s) close by remote",m_fd,m_name.c_str());
- }
- else if(rc==-1)
- {
- logn_errno(1,"hava a error on socket %d(%s)",m_fd,m_name.c_str());
- }
- return -1;
- }
- return 0;
- }
- void close_impl()
- {
- m_recv_timer.stop();
- fd_io::stop();
- // 清除网络下的所有设备
- module_device_net::instance()->clear(m_name);
- logn_info(1,"socket %s closed.",m_name.c_str());
- m_ic.on_close(shared_from_this());
- }
- size_t calc_length(uint8_t*b)const
- {
- return (b[0]<<8)|b[1];
- }
- int io_read()
- {
- if(read_clt()<0)
- return -1;
- try
- {
- int msg_len;
- for(;m_clen>=2;)
- {
- msg_len=calc_length((uint8_t*)m_b)+2;
- if(msg_len>m_max_package_size)
- {
- logn_error(1,"package too big:%d/%d,close socket. site=%s.",msg_len,m_max_package_size,m_name.c_str());
- return -1;
- }
- if(msg_len<=2)
- {
- logn_error(1,"package too small:%d,close socket. site=%s.",msg_len,m_name.c_str());
- return -1;
- }
- if(m_clen<msg_len)
- break;
- if(check_crc(m_b,msg_len))
- {
- on_message(m_b,msg_len);
- logn_bin(1,name().c_str(),m_b,msg_len);//输出二进制日志
- }
- else
- {
- logn_bin(1,name().c_str(),m_b,msg_len);//输出二进制日志
- logn_error(1,"check_crc_error,close socket. site=%s.",m_name.c_str());
- return -1;
- }
- memmove(m_b,&m_b[msg_len],m_clen-msg_len);
- m_clen-=msg_len;
- }
- }
- catch(const std::exception&e)
- {
- logn_error(1,"package error,close socket. site=%s,err_info=%s",m_name.c_str(),e.what());
- return -1;
- }
- return 0;
- }
- int io_write()
- {
- if(! m_can_write)
- {
- set(EV_READ|EV_WRITE);
- return 0;
- }
- for(;;)
- {
- if(m_obuf.size()==m_opos)
- {
- std::unique_lock<std::mutex> _lock(m_mutex);
- if(m_olist.empty())
- break;
- m_obuf.swap(m_olist.front());
- m_olist.pop_front();
- m_opos=0;
- }
- int left=m_obuf.size()-m_opos;
- int rc=zio::write(m_fd,&*m_obuf.begin()+m_opos,left);
- if(rc>0)
- {
- m_opos+=rc;
- if(m_opos==m_obuf.size())
- continue;
- }
- else if(rc==0||rc==-2)//缓冲区满
- {
- m_can_write=false;
- set(EV_READ|EV_WRITE);
- break;
- }
- else
- {
- logn_errno(1,"zio::write(%d,ptr,%d)",m_fd,m_obuf.size()-m_opos);
- return -1;
- }
- }
- if(m_olist.empty() && m_can_write)
- {
- set(EV_READ);
- }
- return 0;
- }
- void operator()(ev::io &w, int flag)
- {
- if(flag & EV_WRITE)
- {
- logn_debug(1,"socket %d(%s) can write,flag=%d." ,m_fd,m_name.c_str(),flag);
- m_can_write=true;
- if(io_write()<0)
- {
- close_impl();
- return;
- }
- }
- if(flag & EV_READ)
- {
- if(io_read()<0)
- {
- close_impl();
- return;
- }
- m_recv_timer.start(m_recv_time_out);
- }
- }
- bool check_crc(void *b0,size_t mlen)
- {
- uint8_t*b=(uint8_t*)b0;
- uint16_t crc=b[mlen-2];
- crc<<=8;
- crc|=b[mlen-1];
- uint16_t ccrc=do_crc((unsigned char*)b+2,mlen-4);
- return ccrc==crc;
- }
- void on_message(const char*m,int mlen)
- {
- m_ic.on_message(shared_from_this(),m,mlen);
- }
- void on_notify()
- {
- if(m_close_flag)
- {
- close_impl();
- return;
- }
- io_write();
- }
- };
- struct sock_listen: fd_io
- {
- sock_listen(io_context&ic,int fd):fd_io(ic,fd){}
- int recv_time_out=config.get("service.recv_timeout",3);
- int max_package_size=config.get("service.max_package",2048);
- void operator()(ev::io &w, int)
- {
- char name[32];
- int fd=zio::accept(m_fd,name);
- if(fd<0)
- {
- logn_errno(1,"socket %s connect error.",name);
- return;
- }
- zio::setiobuf(fd,32<<10,32<<10);
- m_ic.on_connect(std::make_shared<sock_client>(m_ic,name,fd,recv_time_out,max_package_size));
- logn_info(1,"socket %s connected.",name);
- }
- };
- struct signal_w:ev::sig
- {
- io_context&m_ic;
- signal_w(io_context&ic, int s)
- :ev::sig(ic)
- ,m_ic(ic)
- {
- this->set(this);
- this->set(s);
- this->start();
- }
- void operator()(ev::sig &w, int s)
- {
- log_info("recved signal %d",s);
- worker::instance()->stop();
- stop();
- m_ic.stop();
- }
- };
- struct main_loop:io_context
- {
- main_loop(service_callback&sc)
- :io_context(sc)
- {
- }
- virtual int run(int port)
- {
- int fd=zio::listen_on(port);
- if(fd<0)
- {
- log_errno("try listen_on %d", port);
- std_errno("listen %d failed", port);
- return -1;
- }
- sock_listen _1(*this,fd);
- block_sig(SIGPIPE);
- signal_w sint(*this,SIGINT),term(*this,SIGTERM);
- while(!check_stop_flag())
- {
- try
- {
- ev::dynamic_loop::run(0);
- }
- catch(const std::exception&e)
- {
- log_error("捕获到异常:%s",e.what());
- }
- catch(...)
- {
- log_error("捕获到未知异常");
- }
- }
- _1.stop();
- close_all();
- return 0;
- }
- void block_sig(int sig)
- {
- sigset_t signal_mask;
- sigemptyset(&signal_mask);
- sigaddset(&signal_mask, sig);
- if(pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) == -1)
- {
- log_errno("block signal:%d",sig);
- }
- }
- };
- service_handle*service_handle::instance(service_callback*sc)
- {
- static main_loop _impl(*sc);
- sc->set_handle(&_impl);
- return &_impl;
- }
|