1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-10 14:11:43 +00:00
Atmosphere/stratosphere/tma/source/tma_conn_connection.hpp
2018-11-30 18:18:04 -08:00

80 lines
No EOL
2.5 KiB
C++

/*
* Copyright (c) 2018 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "tma_conn_result.hpp"
#include "tma_conn_packet.hpp"
enum class ConnectionEvent : u32 {
Connected,
Disconnected
};
class TmaConnection {
protected:
HosMutex lock;
void (*connection_event_callback)(void *, ConnectionEvent) = nullptr;
void *connection_event_arg = nullptr;
bool has_woken_up = false;
bool is_initialized = false;
protected:
void OnReceivePacket(TmaPacket *packet);
void OnDisconnected();
void OnConnectionEvent(ConnectionEvent event);
void CancelTasks();
void Tick();
public:
/* Setup */
TmaConnection() { }
virtual ~TmaConnection() { }
void Initialize() {
if (this->is_initialized) {
std::abort();
}
this->is_initialized = true;
}
void SetConnectionEventCallback(void (*callback)(void *, ConnectionEvent), void *arg) {
this->connection_event_callback = callback;
this->connection_event_arg = arg;
}
void Finalize() {
if (this->is_initialized) {
this->StopListening();
if (this->IsConnected()) {
this->Disconnect();
}
this->is_initialized = false;
}
}
/* Packet management. */
TmaPacket *AllocateSendPacket();
TmaPacket *AllocateRecvPacket();
void FreePacket(TmaPacket *packet);
/* For sub-interfaces to implement, connection management. */
virtual void StartListening() { }
virtual void StopListening() { }
virtual bool IsConnected() = 0;
virtual TmaConnResult Disconnect() = 0;
virtual TmaConnResult SendPacket(TmaPacket *packet) = 0;
};