Example #1
0
void StormService::onRequest(const Connection& conn, const char* buffer, uint32_t len) {
	int32_t ret = 0;
	RpcRequest req;
	RpcResponse resp;

	// 解析RpcRequest
	if (!req.ParseFromArray(buffer, len)) {
		STORM_ERROR << "rpc request error";
		m_loop->close(conn.id, CloseType_PacketError);
		return;
	}

	// 业务逻辑
	ret = onRpcRequest(conn, req, resp);

	if (req.invoke_type() == InvokeType_OneWay) {
		return;
	}

	//回包
	resp.set_ret(ret);
	resp.set_proto_id(req.proto_id());
	resp.set_request_id(req.request_id());

	IoBuffer* data = PacketProtocolLen::encode(resp);
	m_loop->send(conn.id, data->getHead(), data->getSize());
	delete data;
}
Example #2
0
void SocketListener::doRequest(Connection::ptr pack) {
	int ret = 0;
	RpcRequest req;
	RpcResponse resp;

	//解析RpcRequest
	if (!req.ParseFromString(pack->buffer)) {
		STORM_ERROR << "rpc request error";
		m_sockServer->close(pack->id, CloseType_Packet_Error);
		return;
	}

	//业务逻辑
	ret = doRpcRequest(pack, req, resp);

	if (req.invoke_type() == InvokeType_OneWay) {
		return;
	}

	//回包
	resp.set_ret(ret);
	resp.set_proto_id(req.proto_id());
	resp.set_request_id(req.request_id());

	IOBuffer::ptr respBuf = FrameProtocolLen::encode(resp);
	m_sockServer->send(pack->id, respBuf);
}
  void ConnectionManager::Inquired(RpcRequest &response)
  {
    Dissent::Messaging::ISender *from = response.GetFrom();
    Edge *edge = dynamic_cast<Edge *>(from);
    if(edge == 0) {
      qWarning() << "Received an inquired from a non-Edge: " << from->ToString();
      return;
    } else if(!edge->Outbound()) {
      qWarning() << "We would never make an inquire call on an incoming edge: " << from->ToString();
      return;
    }

    QByteArray brem_id = response.GetMessage()["peer_id"].toByteArray();

    if(brem_id.isEmpty()) {
      qWarning() << "Invalid ConnectionEstablished, no id";
      return;
    }

    Id rem_id(brem_id);


    if(_local_id < rem_id) {
      BindEdge(edge, rem_id);
    } else if(rem_id == _local_id) {
      Address addr = edge->GetRemoteAddress();
      qDebug() << "Attempting to connect to ourself";
      edge->Close("Attempting to connect to ourself");
      emit ConnectionAttemptFailure(addr, "Attempting to connect to ourself");
      return;
    }
  }
  void ConnectionManager::Inquire(RpcRequest &request)
  {
    Dissent::Messaging::ISender *from = request.GetFrom();
    Edge *edge = dynamic_cast<Edge *>(from);
    if(edge == 0) {
      qWarning() << "Received an inquired from a non-Edge: " << from->ToString();
      return;
    } else if(edge->Outbound()) {
      qWarning() << "We should never receive an inquire call on an outbound edge: " << from->ToString();
      return;
    }

    QByteArray brem_id = request.GetMessage()["peer_id"].toByteArray();

    if(brem_id.isEmpty()) {
      qWarning() << "Invalid Inqiure, no id";
      return;
    }

    Id rem_id(brem_id);

    QVariantMap response;
    response["peer_id"] = _local_id.GetByteArray();
    request.Respond(response);

    QString saddr = request.GetMessage()["persistent"].toString();
    Address addr = AddressFactory::GetInstance().CreateAddress(saddr);
    edge->SetRemotePersistentAddress(addr);

    if(_local_id < rem_id) {
      BindEdge(edge, rem_id);
    } else if(_local_id == rem_id) {
      edge->Close("Attempting to connect to ourself");
    }
  }
  void ConnectionManager::Close(RpcRequest &notification)
  {
    Edge *edge = dynamic_cast<Edge *>(notification.GetFrom());
    if(edge == 0) {
      qWarning() << "Connection attempt Edge close not from an Edge: " << notification.GetFrom()->ToString();
      return;
    }

    edge->Close("Closed from remote peer");
  }
  void ConnectionManager::Disconnect(RpcRequest &notification)
  {
    Connection *con = dynamic_cast<Connection *>(notification.GetFrom());
    if(con == 0) {
      qWarning() << "Received DisconnectResponse from a non-connection: " << notification.GetFrom()->ToString();
      return;
    }

    qDebug() << "Received disconnect for: " << con->ToString();
    _con_tab.Disconnect(con);
    con->GetEdge()->Close("Remote disconnect");
  }
  void ConnectionManager::Connect(RpcRequest &notification)
  {
    Edge *edge = dynamic_cast<Edge *>(notification.GetFrom());
    if(edge == 0) {
      qWarning() << "Connection attempt not from an Edge: " << notification.GetFrom()->ToString();
      return;
    }
    
    QByteArray brem_id = notification.GetMessage()["peer_id"].toByteArray();

    if(brem_id.isEmpty()) {
      qWarning() << "Invalid ConnectionEstablished, no id";
      return;
    }

    Id rem_id(brem_id);
    if(_local_id < rem_id) {
      qWarning() << "We should be sending CM::Connect, not the remote side.";
      return;
    }

    Connection *old_con = _con_tab.GetConnection(rem_id);
    // XXX if there is an old connection and the node doesn't want it, we need
    // to close it
    if(old_con != 0) {
      qDebug() << "Disconnecting old connection";
      old_con->Disconnect();
    }

    QSharedPointer<Edge> pedge = _con_tab.GetEdge(edge);
    if(pedge.isNull()) {
      qCritical() << "An edge attempted to create a connection, but there "
       "is no record of it" << edge->ToString();
      return;
    }

    CreateConnection(pedge, rem_id);
  }