}
if (has_file)
- code = GetFileSharing(code, "this");
+ code = GetFileSharing(code, "this.send(payload);");
return code;
}
return name;
}
+std::string DartCionGeneratorBase::GetFullNameFromType(const BaseType& type) {
+ std::string str;
+
+ str += type.ToString();
+
+ if (type.GetMetaType() != nullptr) {
+ str += "_";
+ str += GetFullNameFromType(*type.GetMetaType());
+ }
+
+ return str;
+}
+
std::string DartCionGeneratorBase::GetFullNameFromType(const BaseType& type,
const Interface& iface) {
std::string str;
return str;
}
-bool DartCionGeneratorBase::HasFile(const Interface& iface) {
+bool DartCionGeneratorBase::HasFile(const Interface& iface, const BaseType& type) {
+ if (type.ToString() == "list" || type.ToString() == "array") {
+ std::string name = GetFullNameFromType(type, iface);
+ if (name == "array_file" || name == "list_file" || name == "file")
+ return true;
+ } else if (type.ToString() == "file") {
+ return true;
+ }
+
+ return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const BaseType& type) {
+ if (type.ToString() == "list" || type.ToString() == "array") {
+ std::string name = GetFullNameFromType(type);
+ if (name == "array_file" || name == "list_file" || name == "file")
+ return true;
+ } else if (type.ToString() == "file") {
+ return true;
+ }
+
+ return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const Declaration& decl) {
+ for (const auto& p : decl.GetParameters()) {
+ auto& param_type = p->GetParameterType();
+ auto& type = param_type.GetBaseType();
+
+ if (type.ToString() == "file")
+ return true;
+
+ if (type.ToString() == "list" || type.ToString() == "array") {
+ if (type.GetMetaType() != nullptr &&
+ type.GetMetaType()->ToString() == "file")
+ return true;
+
+ }
+ }
+ return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const Interface& iface, bool is_proxy) {
for (const auto& d : iface.GetDeclarations()) {
+ if (is_proxy) {
+ if (HasFile(iface, d->GetType()) == true)
+ return true;
+ }
+
for (const auto& p : d->GetParameters()) {
auto& param_type = p->GetParameterType();
- auto& type = param_type.GetBaseType();
- auto name = GetFullNameFromType(type, iface);
- if (name == "file" || name == "list_file" || name == "array_file")
- return true;
+ if (is_proxy) {
+ if (d->GetMethodType() == Declaration::MethodType::DELEGATE) {
+ if (param_type.GetDirection() != ParameterType::Direction::IN)
+ continue;
+ } else {
+ if (param_type.GetDirection() == ParameterType::Direction::IN)
+ continue;
+ }
+ if (HasFile(iface, param_type.GetBaseType()) == true)
+ return true;
+ } else {
+ if (d->GetMethodType() == Declaration::MethodType::DELEGATE) {
+ if (param_type.GetDirection() == ParameterType::Direction::IN)
+ continue;
+ } else {
+ if (param_type.GetDirection() != ParameterType::Direction::IN)
+ continue;
+ }
+ if (HasFile(iface, param_type.GetBaseType()) == true)
+ return true;
+ }
}
}
-
return false;
}
std::string DartCionGeneratorBase::GetFileSharing(std::string src, std::string sender) {
std::string file_send;
- file_send += ReplaceAll(CB_FILE_SEND, "<SENDER>", sender);
+ file_send += ReplaceAll(CB_FILE_SEND, "<SEND_ASYNC_FILE>", sender);
return CB_FILE_LIST + NLine(1) + src + NLine(1) + file_send + NLine(1);
}
std::string Tab(int cnt);
std::string Trim(const std::string& str);
- bool HasFile(const Interface& iface);
+ bool HasFile(const Interface& iface, bool is_proxy);
+ bool HasFile(const BaseType& type);
+ bool HasFile(const Interface& iface, const BaseType& type);
+ bool HasFile(const Declaration& decl);
std::string GetFileSharing(std::string src, std::string sender);
private:
void GenMethodId(std::ofstream& stream, const Interface& iface);
std::string GetFullNameFromType(const BaseType& type,
const Interface& iface);
+ std::string GetFullNameFromType(const BaseType& type);
protected:
const int TAB_SIZE = 2;
R"__dart_cb(
for (final String file in fileList) {
final Payload payload = FilePayload(file);
- <SENDER>.send(payload);
+ <SEND_ASYNC_FILE>
}
)__dart_cb";
const Interface& iface) {
std::string name = iface.GetID();
name[0] = ::toupper(name[0]);
+
+ bool has_file = HasFile(iface, true);
+
ReplaceAll(CB_INTERFACE_BASE, {
{ "<INTERFACE_NAME>", name },
- { "<METHODS>", GenInterfaceMethods(iface) }
+ { "<METHODS>", GenInterfaceMethods(iface) },
+ { "<FILE_REVEIVE_DEF>", has_file ? ", required OnFileReceived onFileReceived" : "" },
+ { "<FILE_REVEIVE_CB>", has_file ? "_onFileReceived = onFileReceived;" : "" }
})
.Transform([&](std::string str) {
return SmartIndent(std::move(str));
{ "<ARGS>", args },
{ "<ASYNC>", GenAsync(*d) },
{ "<METHOD_PARCEL_WRITE>", GenMethodParcelWrite(*d) },
+ { "<SEND_ASYNC_FILE>", "_client.send(payload);" },
});
}
{{"<PARCEL_WRITE>", code}, {"<METHOD_NAME>", method_id}});
if (has_file)
- code = GetFileSharing(code, "_client");
+ code = GetFileSharing(code,"_client.send(payload);");
} else {
std::string read_code = std::move(GenMethodResultParcelRead(decl));
read_code += GenMethodOutParamParcelRead(decl);
{"<FILE_SEND>", CB_FILE_SEND},
});
code = ReplaceAll(code, {
- {"<SENDER>", "_client"},
+ {"<SEND_ASYNC_FILE>", "_client.send(payload);"},
});
} else {
code = ReplaceAll(code, {
constexpr const char CB_INTERFACE_BASE[] =
R"__dart_cb(
typedef OnDisconnected = Future<void> Function(PeerInfo);
+typedef OnFileReceived = Future<void> Function(PeerInfo, Payload, PayloadTransferStatus);
/// [<INTERFACE_NAME>] class for Cion.
class <INTERFACE_NAME> {
Future<void> _onReceivedEvent(
PeerInfo peer, Payload payload, PayloadTransferStatus status) async {
+
+ if (payload.type == PayloadType.file) {
+ _onFileReceived?.call(peer, payload as FilePayload, status);
+ return;
+ }
+
final Uint8List raw = (payload as DataPayload).data;
final Parcel parcel = Parcel.fromRaw(raw);
}
/// Connects with the server.
- Future<bool> connect({required OnDisconnected onDisconnected}) async {
+ Future<bool> connect({required OnDisconnected onDisconnected<FILE_REVEIVE_DEF>}) async {
if (_isConnected) {
return true;
}
throw StateError('Already try to connect');
}
+
+ <FILE_REVEIVE_CB>
_onDisconnected = onDisconnected;
await _client.tryDiscovery(onDiscovered: _onDiscoveredEvent);
_connectCompleter = Completer<bool>();
final String serverDisplayName;
bool _isConnected = false;
OnDisconnected? _onDisconnected;
+ OnFileReceived? _onFileReceived;
Completer<bool>? _connectCompleter;
}
)__dart_cb";
{ "<CTOR>", GenInterfaceCtor(iface) },
{ "<EXTEND_NAME>", GetTransportable().Dart().GenStubExtend() },
{ "<FILE_RECEIVE>",
- HasFile(iface) ? GetTransportable().Dart().GenFileRecive() : "" },
+ HasFile(iface, false) ? GetTransportable().Dart().GenFileRecive() : "" },
{ "<PARAM_DEF>", GetTransportable().Dart().GenStubParamDef() },
{ "<SUPER_PARAM>", GetTransportable().Dart().GenStubSuperParam() },
{ "<INTERFACE_NAME>", name },
std::string DartCionStubGen::GenMethodHandlerParcelRead(const Declaration& decl) {
std::string code;
+
+ if (HasFile(decl.GetType()))
+ code += CB_FILE_LIST + NLine(1);
+
for (const auto& p : decl.GetParameters()) {
auto& param_type = p->GetParameterType();
if (param_type.GetDirection() != ParameterType::Direction::IN)
std::string code = std::move(GenMethodHandlerResultParcelWrite(decl));
code += GenMethodHandlerOutParamParcelWrite(decl);
- code = ReplaceAll(CB_METHOD_HANDLER_PARCEL_WRITE, {
- { "<PARCEL_WRITE>", code },
- { "<SEND_ASYNC>", GetTransportable().Dart().GenSendAsync(
- "result", "service._serverBase", "service.peer") }});
+
+ if (HasFile(decl.GetType()))
+ code += ReplaceAll(CB_FILE_SEND, "<SEND_ASYNC_FILE>",
+ "service._serverBase!.send(service.peer!, payload);");
+
+ code = ReplaceAll(CB_METHOD_HANDLER_PARCEL_WRITE, "<PARCEL_WRITE>", code);
return code;
}
{ "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
{ "<NAME>" , "ret" }
});
+
+ if (HasFile(type))
+ code += ReplaceAll(CB_FILE_ADD, "<FILE>", "ret");
code += NLine(1);
}
ReplaceAll(CB_SERVICE_BASE, {
{ "<PEER_INFO_T>", GetTransportable().Dart().GenPeerInfo() },
{ "<FILE_RECEIVE_DECL>",
- HasFile(iface) ? GetTransportable().Dart().GenFileReciveDef() : "" },
+ HasFile(iface, false) ? GetTransportable().Dart().GenFileReciveDef() : "" },
{ "<METHOD_DECLS>", GenServiceBaseMethodDecls(iface) }
})
.Transform([&](std::string str) {
{ "<DELEGATE_NAME>", GetClassName(decl.GetID()) },
{ "<DELEGATE_ID>", id },
{ "<DELEGATE_PARAMS>", GenDelegateParams(decl) },
- { "<SEND_ASYNC>", GetTransportable().Dart().GenSendAsync("parcel",
+ { "<SEND_FILE>", HasFile(decl) ? CB_FILE_SEND : "" },
+ { "<SEND_ASYNC_FILE>", "service._serverBase!.send(service.peer!, payload);" },
+ { "<SEND_ASYNC>", GetTransportable().Dart().GenStubSendAsync("parcel",
"service._serverBase", "service.peer" ) },
{ "<DELEGATE_PARCEL_WRITE>", GenDelegateParcelWrite(decl) }
})
}
if (has_file)
- code = GetFileSharing(code, "this");
+ code = CB_FILE_LIST + NLine(1) + code;
+
return code;
}
/// This abstract method will be called when the '<METHOD_NAME>' request is delivered.
Future<<RETURN_TYPE>> on<METHOD_NAME>(<METHOD_PARAMS>);)__dart_cb";
+
/**
* <DELEGATE_NAME> The name of the delegate.
* <DELEGATE_ID> The ID of the delegate.
<DELEGATE_PARCEL_WRITE>
- <SEND_ASYNC>
+ <SEND_ASYNC>
+
+ <SEND_FILE>
_wasInvoked = true;
}
if (service == null) {
return returnData;
}
- <FILE_RECEIVE>
final Parcel parcel = Parcel.fromRaw(data);
virtual ~DartTransportable() = default;
virtual std::string GenImport() const = 0;
virtual std::string GenPeerInfo() const = 0;
- virtual std::string GenSendAsync(std::string parcel,
+ virtual std::string GenStubSendAsync(std::string parcel,
std::string server, std::string peer) const = 0;
virtual std::string GenFileReciveDef() const = 0;
virtual std::string GenFileRecive() const = 0;
#include "idlc/gen/replace_all.h"
namespace {
-constexpr const char ___SEND_ASYNC[] =
+constexpr const char ___STUB_SEND_ASYNC[] =
R"__c_cb(
final DataPayload dp = DataPayload(<PARCEL_NAME>.asRaw());
<SERVER_NAME>!.send(<PEER_NAME>!, dp);
return "PeerInfo";
}
-std::string DefaultDartTransportable::GenSendAsync(std::string parcel,
+std::string DefaultDartTransportable::GenStubSendAsync(std::string parcel,
std::string server, std::string peer) const {
- return std::string(ReplaceAll(___SEND_ASYNC, {
+ return std::string(ReplaceAll(___STUB_SEND_ASYNC, {
{ "<PARCEL_NAME>", parcel },
{ "<PEER_NAME>", peer },
{ "<SERVER_NAME>", server } }));
virtual ~DefaultDartTransportable() = default;
std::string GenImport() const override;
std::string GenPeerInfo() const override;
- std::string GenSendAsync(std::string parcel,
+ std::string GenStubSendAsync(std::string parcel,
std::string server, std::string peer) const override;
std::string GenFileReciveDef() const override;
std::string GenFileRecive() const override;