Skip to content
Merged

Dev #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16.3)
project(znet-parent)

add_subdirectory(vendor/fmt)
#add_subdirectory(vendor/zstd/build/cmake)
add_subdirectory(vendor/zstd/build/cmake)
add_subdirectory(znet)

# rendezvous server
Expand Down
15 changes: 9 additions & 6 deletions rendezvous-server/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char* argv[]) {
std::string target = result["target"].as<std::string>();
ZNET_LOG_INFO("Starting relay on {}:{}...", target, port);

znet::ServerConfig config{target, port};
znet::ServerConfig config{target, port,std::chrono::seconds(5), znet::ConnectionType::TCP};
znet::Server relay{config};
relay.SetEventCallback(ZNET_BIND_GLOBAL_FN(OnEvent));

Expand Down Expand Up @@ -194,19 +194,22 @@ int main(int argc, char* argv[]) {

auto response = std::make_shared<znet::p2p::StartPunchRequestPacket>();
// add start_at
// handle ipv6 instead of hard coding the endpoint
// allow lan connection if possible
response->target_peer_ = other_data->peer_name_;
response->target_endpoint_ = other_data->session_->remote_address();
response->bind_endpoint_ = znet::InetAddress::from("0.0.0.0", session->remote_address()->port());
auto other_address = other_data->session_->remote_address();
auto local_address = session->remote_address();
response->target_endpoint_ = other_address;
response->bind_endpoint_ = znet::InetAddress::from(znet::GetAnyBindAddress(local_address->ipv()), local_address->port());
response->punch_id_ = punch_id;
response->connection_type_ = znet::ConnectionType::TCP;
session->SendPacket(response);

response = std::make_shared<znet::p2p::StartPunchRequestPacket>();
response->target_peer_ = data->peer_name_;
response->target_endpoint_ = session->remote_address();
response->bind_endpoint_ = znet::InetAddress::from("0.0.0.0", other_data->session_->remote_address()->port());
response->target_endpoint_ = local_address;
response->bind_endpoint_ = znet::InetAddress::from(znet::GetAnyBindAddress(other_address->ipv()), other_address->port());
response->punch_id_ = punch_id;
response->connection_type_ = znet::ConnectionType::TCP;
other_data->session_->SendPacket(response);
}
}
Expand Down
1 change: 1 addition & 0 deletions znet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(ZNET_SOURCES
src/pch.cc
src/init.cc
src/backend/tcp.cc
src/backend/backend.cc
src/p2p/locator.cc
src/p2p/dialer.cc
)
Expand Down
12 changes: 12 additions & 0 deletions znet/include/znet/backends/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "znet/peer_session.h"
#include "znet/precompiled.h"

#include <deque>

namespace znet {
namespace backends {

Expand All @@ -34,16 +36,26 @@ class TCPTransportLayer : public TransportLayer {

bool IsClosed() override { return is_closed_; }

void Update() override;

private:
std::shared_ptr<Buffer> ReadBuffer();

struct QueuedPacket {
std::shared_ptr<Buffer> buffer;
SendOptions options;
};

bool SendInternal(std::shared_ptr<Buffer> buffer, SendOptions options);

char data_[ZNET_MAX_BUFFER_SIZE]{};
int read_offset_ = 0;
ssize_t data_size_ = 0;
std::shared_ptr<Buffer> buffer_;
SocketHandle socket_;
bool has_more_;
bool is_closed_ = false;
std::deque<QueuedPacket> outbound_;

};

Expand Down
3 changes: 3 additions & 0 deletions znet/include/znet/base/inet_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ IPv6Address ParseIPv6(const std::string& ip_str);

int GetDomainByInetProtocolVersion(InetProtocolVersion version);

std::string GetAnyBindAddress(InetProtocolVersion version);
std::string GetLocalAddress(InetProtocolVersion version);

bool IsIPv4(const std::string& ip);
bool IsIPv6(const std::string& ip);

Expand Down
5 changes: 2 additions & 3 deletions znet/include/znet/base/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ inline std::string GetResultString(Result result) {

enum class ConnectionType {
TCP,
RUDP,
//RakNet,
//RUDP,
//ENet,
//WebSocket,
//QUIC
};

#if defined(TARGET_APPLE) || defined(TARGET_WEB) || defined(TARGET_LINUX)
Expand Down
Loading
Loading