/** * Éú³Éhead buffer */ private function _gen_head_buff() { $body = 'test body'; $body_len = strlen($body); $head = array(); $head['body_len'] = $body_len; $head['magic_num'] = BigpipeCommonDefine::NSHEAD_CHECKSUM_MAGICNUM; $head['reserved'] = BigpipeUtilities::adler32($body); $head_builder = new NsHead(); $head_data = $head_builder->build_nshead($head); $head['magic_num'] = 0; $no_magic_num = $head_builder->build_nshead($head); $head['body_len'] = 0; $bad_head = $head_builder->build_nshead($head); $ret_arr = array('good' => $head_data, 'bad' => $bad_head, 'no_magic' => $no_magic_num); return $ret_arr; }
/** * 发送一条nshead封装的消息 * @return true on success or false on failure */ public function send($buffer, $buffer_size) { if (null == $this->_socket) { BigpipeLog::warning('[send error][lose connection][target:%s:%u]', $this->_target['socket_address'], $this->_target['socket_port']); return false; // 无连接 } // build nshead package if (0 == $buffer_size) { BigpipeLog::warning('[send error][empty buffer]'); return false; } $req_head = new NsHead(); $req_arr = array('body_len' => $buffer_size); if (true === $this->_check_frame) { // 整包校验写在nshead的reserved字段中 $req_arr['magic_num'] = BigpipeCommonDefine::NSHEAD_CHECKSUM_MAGICNUM; $req_arr['reserved'] = BigpipeUtilities::adler32($buffer); } $data = $req_head->build_nshead($req_arr) . $buffer; BigpipeLog::debug("[send data][%s]", $data); if (!$this->_socket->write($data)) { BigpipeLog::warning('[fail to send data][may be network error]'); return false; } return true; }