* @param command Command event
*/
protected void encodeCommand(IoBuffer out, ICommand command) {
// TODO: tidy up here
Output output = new org.red5.io.amf.Output(out);
final IServiceCall call = command.getCall();
final boolean isPending = (call.getStatus() == Call.STATUS_PENDING);
log.debug("Call: {} pending: {}", call, isPending);
if (!isPending) {
log.debug("Call has been executed, send result");
Serializer.serialize(output, call.isSuccess() ? "_result" : "_error");
} else {
log.debug("This is a pending call, send request");
final String action = (call.getServiceName() == null) ? call.getServiceMethodName() : call.getServiceName() + '.' + call.getServiceMethodName();
Serializer.serialize(output, action); // seems right
}
if (command instanceof Invoke) {
Serializer.serialize(output, Integer.valueOf(command.getTransactionId()));
Serializer.serialize(output, command.getConnectionParams());
}
if (call.getServiceName() == null && "connect".equals(call.getServiceMethodName())) {
// response to initial connect, always use AMF0
output = new org.red5.io.amf.Output(out);
} else {
if (Red5.getConnectionLocal().getEncoding() == Encoding.AMF3) {
output = new org.red5.io.amf3.Output(out);
} else {
output = new org.red5.io.amf.Output(out);
}
}
if (!isPending && (command instanceof Invoke)) {
IPendingServiceCall pendingCall = (IPendingServiceCall) call;
if (!call.isSuccess()) {
log.debug("Call was not successful");
StatusObject status = generateErrorResult(StatusCodes.NC_CALL_FAILED, call.getException());
pendingCall.setResult(status);
}
Object res = pendingCall.getResult();
log.debug("Writing result: {}", res);
Serializer.serialize(output, res);
} else {
log.debug("Writing params");
final Object[] args = call.getArguments();
if (args != null) {
for (Object element : args) {
if (element instanceof ByteBuffer) {
// a byte buffer indicates that serialization is already complete, send raw
final ByteBuffer buf = (ByteBuffer) element;