void Connection::sendMsg(SendPacket* p) { if (m_status != CONN_STATUS_CONNECTED) { LOGERROR() << "sendmsg status not connected" <<"id"<<m_id <<LOGEND(); return; } short opCode = p->getOPCode(); std::string body = p->getData(); unsigned int pack = (13 << 24) + (body.size() & 0xFFFFFF); char header[MSG_HEADER_SIZE] = {0}; memmove(header, (char*)&opCode, sizeof(opCode)); memmove(header+sizeof(opCode), (char*)&pack, sizeof(pack)); std::stringstream ss; ss << std::string(header, 6) << body; m_outData.push_back(ss.str()); LOGNET() << "try send msg " << opCode << " pack size " << body.size() << LOGEND(); tryWrite(); }
// get some data void Connection::handleRead(const boost::system::error_code& error, size_t len) { if(!error) { LOGNET() << ">>> got " << len << " data from " <<"id"<<m_id <<LOGEND(); m_offSet += len; m_msgBuffer.push_back(std::string(m_buffer, m_buffer+m_offSet)); m_offSet = 0; // continue to read tryRead(); handlePacket(); } else { if (m_status == CONN_STATUS_CONNECTED) { m_status = CONN_STATUS_DISCONNECT; } else { // triggered by other event } if (m_writeStatus != SW_STATUS_WRITTING) { m_errHandler(error, this, "handleread"); } else { // write callback will call immediately } } }
// trigger void Connection::tryWrite() { if (m_status != CONN_STATUS_CONNECTED) { LOGERROR() << "write status not connected " <<"id"<<m_id <<LOGEND(); return; } if (m_outData.size() > 0 && m_writeStatus != SW_STATUS_WRITTING) { m_writeStatus = SW_STATUS_WRITTING; m_tempBuf = m_outData.front(); m_sock.async_write_some ( boost::asio::buffer(m_tempBuf, m_tempBuf.size()), boost::bind ( &Connection::handleWrite, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); } }
void Connection::handleWrite(const boost::system::error_code& error, size_t len) { if(!error) { m_writeStatus = SW_STATUS_WRITEDONE; assert(m_outData.size() > 0); // ok write some chars //std::string& buf = m_outData.front(); // first buffer all flushed if (len == m_tempBuf.length()) { LOGNET() << "<<< write full " << len << " " <<"id"<<m_id << LOGEND(); m_outData.pop_front(); tryWrite(); } else { //LOGNET() << "<<< write partial " << len << " should " << buf.length() << " " LOGNET() << "<<< write partial " << len << " should " << m_tempBuf.length() << " " <<"id"<<m_id << LOGEND(); assert(len < m_tempBuf.length()); //buf = buf.substr(len); m_tempBuf = m_tempBuf.substr(len); tryWrite(); } } else { if (m_status == CONN_STATUS_CONNECTED) { m_status = CONN_STATUS_DISCONNECT; } else { // triggered by other event } if (m_errHandler) { m_errHandler(error, this, "handlewrite"); } } }
void StreamAdapter::inputStreamData(const char *buff, const jint length) { LOGSTART(); //LOGD("%s:input size is %d.lSourceID(%d)", __FUNCTION__, length, lSourceID); pstMontagePlay->pw_mp_InputData((const GInt32) lSourceID, (const GInt8 *) buff, (GUInt32) length, "video_h264_single"); LOGEND(); //LOGD("%s: pstMontagePlay %d", __FUNCTION__, pstMontagePlay->pw_mp_InputData((const GInt32) lSourceID, (const GInt8 *) buff, (GUInt32) length, "mix_stream_pw1")); }
void ServerConnection::errHandler(const boost::system::error_code& e, Connection*, std::string dummy) { LOGERROR() << "serverconnection error" <<"ip"<<m_ip.c_str() <<"port"<<m_port <<"dummy"<<dummy <<LOGEND(); tryConnect(); }
// just trying to connect void ServerConnection::tryConnect() { this->clear(); ip::tcp::endpoint point(ip::address_v4::from_string(m_ip.c_str()), m_port); m_sock.async_connect(point, boost::bind(&ServerConnection::connectHandler, this, boost::asio::placeholders::error)); m_status = CONN_STATUS_CONNECTING; LOGNORMAL() << "trying to connect " << m_ip.c_str() << " " << m_port << LOGEND(); }
void ServerConnection::connectHandler(const boost::system::error_code& e) { if(!e) { LOGNORMAL() << "connect " << m_ip.c_str() << " " << m_port << " OK" << LOGEND(); //m_status = CONN_STATUS_CONNECTED; start(); // send handshake message // trigger read //tryRead(); } else { m_status = CONN_STATUS_DISCONNECT; LOGERROR() << "connect error " << m_ip.c_str() << " " << m_port << LOGEND(); tryConnect(); } }
jboolean StreamAdapter::initMontagePlay() { LOGSTART(); pstMontagePlay = new PMP_MONTAGE_PLAY; if (pstMontagePlay == NULL) { goto ERROR; } if ( GFalse == pstMontagePlay->pw_mp_Create()) { LOGI("%s:pstMontagePlay create fail.", __FUNCTION__); goto ERROR; } pstMontagePlay->pw_mp_SetErrorCallBack(ErrorCBFun, (GUInt64) this); lEngineID = pstMontagePlay->pw_mp_GetEngineID(); if ( PMP_ERR_ID == lEngineID) { LOGI("%s:pstMontagePlay get engine id error.", __FUNCTION__); goto ERROR; } lSourceID = pstMontagePlay->pw_mp_AddDataSource("single", "h264&g711a"); if ( PMP_ERR_ID == lSourceID) { LOGI("%s:pstMontagePlay get source id error.", __FUNCTION__); goto ERROR; } pstMontagePlay->pw_mp_SetInfo( lSourceID, "buff_cache_size", "1" ); if ( GFalse == pstMontagePlay->pw_mp_SetInfo( lSourceID, "steamcb_type", "block" )) { LOGI("%s:pstMontagePlay set callback type error.", __FUNCTION__); goto ERROR; } if ( GFalse == pstMontagePlay->pw_mp_SetInfo(lSourceID, "queue_full_method", "real_play")) { LOGI("%s:pstMontagePlay set callback type error.", __FUNCTION__); goto ERROR; } if ( GFalse == pstMontagePlay->pw_mp_SetInfo( lSourceID, "audio_decode_parsing", "n" )) { LOGI("%s:pstMontagePlay set pw_mp_SetInfo type error.", __FUNCTION__); goto ERROR; } if ( GFalse == pstMontagePlay->pw_mp_SetSteamCallBack( lSourceID, StreamCBFun, NULL )) { LOGI("%s:pstMontagePlay set pw_mp_SetSteamCallBack error.", __FUNCTION__); goto ERROR; } pstMontagePlay->pw_mp_Play(lSourceID); LOGEND(); InitPlayerGL(); return true; ERROR: if (pstMontagePlay != NULL) { delete pstMontagePlay; pstMontagePlay = NULL; } return false; }
bool App::init() { // *log thread if(!LOGTHREAD_P()->init()) { SLOWLOG() << "log thread init false!!!!!!!!!!!!!!!!" << LOGEND(); return false; } SLOWLOG() << "log init OK!" << LOGEND(); // *init connection to master m_pFSMaster = new ServerConnection(m_io); BaseConfig* bc = Config::get()->getBaseConfig(); m_pFSMaster->init("127.0.0.1", 1234); SLOWLOG() << "connection to master init OK!" << LOGEND(); m_pFS = new ConnectionManager(m_io, 5678); // init msg handler MsgHandler::init(); SLOWLOG() << "msg handler init OK!" << LOGEND(); return true; }
void StreamAdapter::init(){ LOGSTART(); initMontagePlay(); LOGEND(); }
GVoid ErrorCBFun(GInt32 lEngineID, MP_ERROR_TYPE eErrType, const GChar *pstrErrorDescrib, GUInt64 dw2UserData) { LOGSTART(); LOGI("%s:%d,%s.", __FUNCTION__, eErrType, pstrErrorDescrib); LOGEND(); }
void Connection::handlePacket() { int count = 0; while(count++ < 20) { if(this->getBufSize() <= 0) { break; } std::string header; bool flag = this->getMsgSize(&header, MSG_HEADER_SIZE); if (!flag) { LOGERROR() << "not enough header" << LOGEND(); break; } else { char* buf = &header[0]; // check msg header int offset = 0; short msgID = 0; memcpy(&msgID, buf, sizeof(msgID)); unsigned int pack = 0; memcpy(&pack, buf+sizeof(msgID), sizeof(pack)); int msgSize = pack & 0xFFFFFF; unsigned int chaos = pack >> 24; if (chaos != 13) { LOGERROR() << "wrong pack chaos " << chaos << " msg " << msgID << " from " <<"id"<<m_id << LOGEND(); return; } // todo check valid msgid // body std::string body; flag = getMsgSize(&body, MSG_HEADER_SIZE+msgSize); if(flag) { this->skipMsgSize(msgSize+MSG_HEADER_SIZE); body.erase(0, MSG_HEADER_SIZE); LOGNET() << ">>> got msg " << msgID << " size " << msgSize << LOGEND(); Packet p(msgID, body); MsgHandlerType handler = MsgHandler::getHandler(p.getOP()); if (handler) { bool flag = handler(this, &p); if (!flag) { LOGERROR() << "return false handle " << p.getOP() << LOGEND(); } } else { LOGERROR() << "didn't find handler for " << p.getOP() << LOGEND(); } } else { LOGNET() << "not enough body need " << msgSize << " but have " << m_offSet - MSG_HEADER_SIZE << " msgID=" << msgID << LOGEND(); } } } }