request.cpp
资源名称:stocks.rar [点击查看]
上传用户:wenyu2k02
上传日期:2016-04-13
资源大小:28k
文件大小:3k
源码类别:
金融证券系统
开发平台:
Delphi
- #include "config.h"
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/thread/recursive_mutex.hpp>
- #include <boost/smart_ptr.hpp>
- #include <boost/date_time/gregorian/gregorian.hpp>
- #include "tcpSocket.h"
- #include "stock.h"
- #include "request.h"
- namespace StockMarket
- {
- recursive_mutex req_queue_mutex;
- uint Request::seq_id = 1;
- bool Request::received = true;
- Request::Request() : first(true)
- {}
- Request::operator bool ()
- {
- if(first)
- {
- first = false;
- return true;
- }
- else
- {
- return false;
- }
- }
- void Request::res_seq_id(uint id)
- {
- // 只有收到较后面的时候,才设置新值
- // ( 考虑到有些时候收发的顺序不同)
- if(id >= seq_id || id == 0) // id == 0 rewind
- {
- seq_id = id;
- received = true;
- }
- }
- bool Request::ready()
- {
- static uint cnt = 0;
- if(!received)
- {
- if((++cnt) % 20 == 0) // 等待 30 个周期还没有收到response,则重发
- {
- ++seq_id;
- return true;
- }
- }
- else
- {
- cnt = 1;
- }
- return received;
- }
- void Request::next()
- {
- if(received)
- {
- this->operator ++();
- ++seq_id;
- }
- }
- Request& Request::operator++()
- {
- return *this;
- }
- void Request::send(TcpSocket& soc)
- {
- ReqHead* pHead = (ReqHead*)buff();
- pHead->set_seq_id(seq_id);
- soc.SendBuf(buff(), len());
- received = false;
- }
- Request::~Request(){}
- ReqHead::ReqHead( ushort cmd_id, ushort packet_len/*the total packet len*/ = 0)
- {
- zip = 0x0c;
- seq_id = 0;
- packet_type = 1;
- len = len1 = packet_len - sizeof(ReqHead) + 2;
- cmd = cmd_id;
- }
- uint ReqHead::get_seq_id()
- {
- return seq_id;
- }
- void ReqHead::set_seq_id(uint id)
- {
- seq_id = id;
- }
- void ReqHead::set_len(ushort payload_len)
- {
- len = len1 = payload_len;
- }
- ushort ReqHead::get_len()
- {
- return len;
- }
- char* StockHeartBeat::buff()
- {
- return (char*)&s;
- }
- ulong StockHeartBeat::len()
- {
- return sizeof(StockHeartBeatStruct);
- }
- StockListReq::StockListReq(MarketInfo::MarketType market_code, ushort record_offset
- , ushort record_count, ushort record_total)
- : s(market_code, record_offset, record_count), total(record_total)
- {
- if(0 == record_total)
- {
- total = record_count;
- }
- }
- char* StockListReq::buff()
- {
- return (char*)&s;
- }
- ulong StockListReq::len()
- {
- return sizeof(StockListStruct);
- }
- StockListReq::operator bool ()
- {
- return s.offset < total;
- }
- StockListReq& StockListReq::operator ++ ()
- {
- if(s.offset + s.count < total)
- {
- s.offset = s.offset + s.count ;
- s.count = (total - s.offset) > s.count ? s.count : (total - s.offset);
- }
- else
- {
- s.offset = total;
- }
- return *this;
- }
- char* StockHoldChgReq::buff()
- {
- return (char*)&s;
- }
- ulong StockHoldChgReq::len()
- {
- return sizeof(ReqHead) + sizeof(ushort) + sizeof(StockHoldStruct) * s.count;
- }
- bool StockHoldChgReq::add_stock(const string& stock_code)
- {
- return s.add_one_stock(stock_code);
- }
- }