Skip to content

Commit

Permalink
steam: Allow P2PConnect to connect over IP
Browse files Browse the repository at this point in the history
Also set the default peer name when using P2PConnectIP to "ip:<ip_addr>"
(which is the way Steam formats an IP Address SteamNetworkingIdentity)
  • Loading branch information
binji committed Nov 22, 2024
1 parent 049d928 commit 32daa4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
35 changes: 28 additions & 7 deletions dev/src/steamworks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,36 @@ struct SteamState {
ip.ToString(real_ip, sizeof(real_ip), true);
auto socket = SteamNetworkingSockets()->CreateListenSocketIP(ip, 0, nullptr);
listen_socket = socket;
LOG_INFO("P2PListen(): created listen socket for IP Address at ", real_ip);
LOG_INFO("P2PListenIP(): created listen socket for IP Address at ", real_ip);
SteamNetworkingIPAddr address;
if (!SteamNetworkingSockets()->GetListenSocketAddress(listen_socket, &address)) {
LOG_ERROR("P2PListen(): GetListenSocketAddress failed");
LOG_ERROR("P2PListenIP(): GetListenSocketAddress failed");
return false;
}
char ipaddr[SteamNetworkingIPAddr::k_cchMaxString]{};
address.ToString(ipaddr, sizeof(ipaddr), true);
LOG_INFO("P2PListen(): listen socket IP Address is ", ipaddr);
LOG_INFO("P2PListenIP(): listen socket IP Address is ", ipaddr);
return socket != k_HSteamListenSocket_Invalid;
}

bool P2PConnect(string_view_nt str_identity) {
SteamNetworkingIdentity identity{};
identity.ParseString(str_identity.c_str());
LOG_INFO("P2PConnect(): opening connection to ", str_identity);
auto connection = SteamNetworkingSockets()->ConnectP2P(identity, 1, 0, nullptr);
HSteamNetConnection connection = k_HSteamNetConnection_Invalid;
if (identity.m_eType == k_ESteamNetworkingIdentityType_IPAddress) {
const SteamNetworkingIPAddr *ip = identity.GetIPAddr();
if (ip == NULL) {
LOG_ERROR("P2PConnect(): identity ", str_identity, " has type IP but GetIPAddr() failed?");
return false;
}
char ip_addr[SteamNetworkingIdentity::k_cchMaxString]{};
ip->ToString(ip_addr, sizeof(ip_addr), true);
LOG_INFO("P2PConnect(): opened connection for IP address to ", ip_addr);
connection = SteamNetworkingSockets()->ConnectByIPAddress(*ip, 0, nullptr);
} else {
LOG_INFO("P2PConnect(): opening connection to ", str_identity);
connection = SteamNetworkingSockets()->ConnectP2P(identity, 1, 0, nullptr);
}
if (connection == k_HSteamNetConnection_Invalid) {
LOG_ERROR("P2PConnect(): failed to open connection to ", str_identity);
return false;
Expand All @@ -205,7 +218,13 @@ struct SteamState {
LOG_ERROR("P2PConnectIP(): failed to open connection to ", ip_addr);
return false;
}
peers.push_back(SteamPeer { string(ip_addr.sv), {}, connection, false, false, {} });
// Use the SteamNetworkingIdentity format for the peer's identity (e.g. "ip:<ip_addr>")
SteamNetworkingIdentity id;
id.m_eType = k_ESteamNetworkingIdentityType_IPAddress;
id.SetIPAddr(ip);
char str_id[SteamNetworkingIdentity::k_cchMaxString];
id.ToString(str_id, sizeof(str_id));
peers.push_back(SteamPeer { str_id, {}, connection, false, false, {} });
return true;
}

Expand Down Expand Up @@ -949,7 +968,9 @@ nfr("p2p_close_listen", "", "", "B", "close the listen socket and stop accepting
return STEAM_BOOL_VALUE(steam->CloseListen());
});

nfr("p2p_connect", "ident", "S", "B", "connect to a user with a given steam identity that has opened a listen socket",
nfr("p2p_connect", "ident", "S", "B", "connect to a user with a given steam "
"identity (e.g. \"steam:XXXX\") or ip address (e.g. \"ip:127.0.0.1:5105\") that "
"has opened a listen socket",
[](StackPtr &, VM &, Value &ident) {
return STEAM_BOOL_VALUE(steam->P2PConnect(ident.sval()->strvnt()));
});
Expand Down
8 changes: 1 addition & 7 deletions samples/shooter_tutorial/tut_net.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,7 @@ while gl.frame():
guard local_ident < c:
print "Skipping connection to \"{c}\""
print "Connecting to \"{c}\""
if c.find_string("ip:") == 0:
let ipaddr = c.substring(3, -1)
check(steam.p2p_connect_ipaddr(ipaddr), "Unable to connect to peer \'{c}\'")
elif c.find_string("steamid:") == 0:
check(steam.p2p_connect(c), "Unable to connect to peer \'{c}\'")
else:
ok = false
check(steam.p2p_connect(c), "Unable to connect to peer \'{c}\'")
// Notify peers of our correct identity.
send(m_peer_joined { local_ident })
player { c }
Expand Down

0 comments on commit 32daa4a

Please sign in to comment.