mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
added ftxui dependency
This commit is contained in:
@@ -6,47 +6,64 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
constexpr std::string_view sock_path = "/tmp/nearby_sharing.sock";
|
||||
|
||||
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
|
||||
#include "internal/base/file_path.h"
|
||||
#include "internal/flags/nearby_flags.h"
|
||||
#include "sharing/advertisement.h"
|
||||
#include "sharing/attachment_container.h"
|
||||
#include "sharing/common/nearby_share_enums.h"
|
||||
#include "sharing/file_attachment.h"
|
||||
#include "sharing/linux/platform/linux_sharing_platform.h"
|
||||
#include "sharing/linux/nearby_noop_analytics_recorder.h"
|
||||
#include "sharing/flags/generated/nearby_sharing_feature_flags.h"
|
||||
#include "sharing/nearby_sharing_service.h"
|
||||
#include "sharing/nearby_sharing_service_factory.h"
|
||||
#include "sharing/nearby_sharing_settings.h"
|
||||
#include "sharing/share_target.h"
|
||||
#include "sharing/share_target_discovered_callback.h"
|
||||
#include "sharing/transfer_metadata.h"
|
||||
#include "sharing/transfer_update_callback.h"
|
||||
#include "sharing/proto/enums.pb.h"
|
||||
|
||||
|
||||
int main() {
|
||||
// should always be waiting on the socket for new connections
|
||||
int retries = 3;
|
||||
while (retries >= 0) {
|
||||
int server_fd;
|
||||
struct sockaddr_un addr;
|
||||
namespace nearby::sharing::linux {
|
||||
|
||||
server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (server_fd == -1) {
|
||||
retries--;
|
||||
continue;
|
||||
}
|
||||
class DiscoveryCallback final : public ShareTargetDiscoveredCallback {
|
||||
public:
|
||||
explicit DiscoveryCallback() {}
|
||||
|
||||
// removes old socket
|
||||
unlink(sock_path.data());
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, sock_path.data(), sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
|
||||
close(server_fd);
|
||||
retries--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (listen(server_fd, 10) == -1) {
|
||||
close(server_fd);
|
||||
retries--;
|
||||
continue;
|
||||
}
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
|
||||
// blocks till connection is present
|
||||
if (accept(server_fd, (struct sockaddr*)&addr, &addr_len)) {
|
||||
close(server_fd);
|
||||
retries--;
|
||||
continue;
|
||||
void OnShareTargetDiscovered(const ShareTarget& share_target) override {
|
||||
std::cout << "discovered target=\"" << share_target.device_name
|
||||
<< "\" id=" << share_target.id << std::endl;
|
||||
if (share_target.receive_disabled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void OnShareTargetLost(const ShareTarget& share_target) override {
|
||||
std::cout << "lost target=\"" << share_target.device_name
|
||||
<< "\" id=" << share_target.id << std::endl;
|
||||
}
|
||||
|
||||
void OnShareTargetUpdated(const ShareTarget& share_target) override {
|
||||
std::cout << "updated target=\"" << share_target.device_name
|
||||
<< "\" id=" << share_target.id << std::endl;
|
||||
OnShareTargetDiscovered(share_target);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto analytics_recorder = nearby::sharing::linux::NoOpAnalyticsRecorder();
|
||||
auto linux_platform = nearby::sharing::linux::LinuxSharingPlatform("LinuxShare");
|
||||
auto service_ = nearby::sharing::NearbySharingServiceFactory::GetInstance()->CreateSharingService(
|
||||
linux_platform, &analytics_recorder, /*event_logger=*/nullptr,
|
||||
/*supports_file_sync=*/false);
|
||||
if (service_ == nullptr) {
|
||||
std::cerr << "failed to create NearbySharingService" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user