Rise
The Vieneo Province
Loading...
Searching...
No Matches
isteamnetworking.h
Go to the documentation of this file.
1//====== Copyright © 1996-2008, Valve Corporation, All rights reserved. =======
2//
3// Purpose: interface to steam managing network connections between game clients & servers
4//
5//=============================================================================
6
7#ifndef ISTEAMNETWORKING
8#define ISTEAMNETWORKING
9#ifdef _WIN32
10#pragma once
11#endif
12
13#include "steam_api_common.h"
14
15// list of possible errors returned by SendP2PPacket() API
16// these will be posted in the P2PSessionConnectFail_t callback
18{
20 k_EP2PSessionErrorNoRightsToApp = 2, // local user doesn't own the app that is running
21 k_EP2PSessionErrorTimeout = 4, // target isn't responding, perhaps not calling AcceptP2PSessionWithUser()
22 // corporate firewalls can also block this (NAT traversal is not firewall traversal)
23 // make sure that UDP ports 3478, 4379, and 4380 are open in an outbound direction
24
25 // The following error codes were removed and will never be sent.
26 // For privacy reasons, there is no reply if the user is offline or playing another game.
29
31};
32
33// SendP2PPacket() send types
34// Typically k_EP2PSendUnreliable is what you want for UDP-like packets, k_EP2PSendReliable for TCP-like packets
36{
37 // Basic UDP send. Packets can't be bigger than 1200 bytes (your typical MTU size). Can be lost, or arrive out of order (rare).
38 // The sending API does have some knowledge of the underlying connection, so if there is no NAT-traversal accomplished or
39 // there is a recognized adjustment happening on the connection, the packet will be batched until the connection is open again.
41
42 // As above, but if the underlying p2p connection isn't yet established the packet will just be thrown away. Using this on the first
43 // packet sent to a remote host almost guarantees the packet will be dropped.
44 // This is only really useful for kinds of data that should never buffer up, i.e. voice payload packets
46
47 // Reliable message send. Can send up to 1MB of data in a single message.
48 // Does fragmentation/re-assembly of messages under the hood, as well as a sliding window for efficient sends of large chunks of data.
50
51 // As above, but applies the Nagle algorithm to the send - sends will accumulate
52 // until the current MTU size (typically ~1200 bytes, but can change) or ~200ms has passed (Nagle algorithm).
53 // Useful if you want to send a set of smaller messages but have the coalesced into a single packet
54 // Since the reliable stream is all ordered, you can do several small message sends with k_EP2PSendReliableWithBuffering and then
55 // do a normal k_EP2PSendReliable to force all the buffered data to be sent.
57
58};
59
60
61// connection state to a specified user, returned by GetP2PSessionState()
62// this is under-the-hood info about what's going on with a SendP2PPacket(), shouldn't be needed except for debuggin
63#if defined( VALVE_CALLBACK_PACK_SMALL )
64#pragma pack( push, 4 )
65#elif defined( VALVE_CALLBACK_PACK_LARGE )
66#pragma pack( push, 8 )
67#else
68#error steam_api_common.h should define VALVE_CALLBACK_PACK_xxx
69#endif
71{
72 uint8 m_bConnectionActive; // true if we've got an active open connection
73 uint8 m_bConnecting; // true if we're currently trying to establish a connection
74 uint8 m_eP2PSessionError; // last error recorded (see enum above)
75 uint8 m_bUsingRelay; // true if it's going through a relay server (TURN)
78 uint32 m_nRemoteIP; // potential IP:Port of remote host. Could be TURN server.
79 uint16 m_nRemotePort; // Only exists for compatibility with older authentication api's
80};
81#pragma pack( pop )
82
83
84// handle to a socket
85typedef uint32 SNetSocket_t; // CreateP2PConnectionSocket()
86typedef uint32 SNetListenSocket_t; // CreateListenSocket()
87
88// connection progress indicators, used by CreateP2PConnectionSocket()
90{
92
93 // communication is valid
95
96 // states while establishing a connection
97 k_ESNetSocketStateInitiated = 10, // the connection state machine has started
98
99 // p2p connections
100 k_ESNetSocketStateLocalCandidatesFound = 11, // we've found our local IP info
101 k_ESNetSocketStateReceivedRemoteCandidates = 12,// we've received information from the remote machine, via the Steam back-end, about their IP info
102
103 // direct connections
104 k_ESNetSocketStateChallengeHandshake = 15, // we've received a challenge packet from the server
105
106 // failure states
107 k_ESNetSocketStateDisconnecting = 21, // the API shut it down, and we're in the process of telling the other end
108 k_ESNetSocketStateLocalDisconnect = 22, // the API shut it down, and we've completed shutdown
109 k_ESNetSocketStateTimeoutDuringConnect = 23, // we timed out while trying to creating the connection
110 k_ESNetSocketStateRemoteEndDisconnected = 24, // the remote end has disconnected from us
111 k_ESNetSocketStateConnectionBroken = 25, // connection has been broken; either the other end has disappeared or our local network connection has broke
112
113};
114
115// describes how the socket is currently connected
122
123
124//-----------------------------------------------------------------------------
125// Purpose: Functions for making connections and sending data between clients,
126// traversing NAT's where possible
127//
128// NOTE: This interface is deprecated and may be removed in a future release of
131//-----------------------------------------------------------------------------
133{
134public:
136 //
137 // UDP-style (connectionless) networking interface. These functions send messages using
138 // an API organized around the destination. Reliable and unreliable messages are supported.
139 //
140 // For a more TCP-style interface (meaning you have a connection handle), see the functions below.
141 // Both interface styles can send both reliable and unreliable messages.
142 //
143 // Automatically establishes NAT-traversing or Relay server connections
144 //
145 // These APIs are deprecated, and may be removed in a future version of the Steamworks
146 // SDK. See ISteamNetworkingMessages.
147
148 // Sends a P2P packet to the specified user
149 // UDP-like, unreliable and a max packet size of 1200 bytes
150 // the first packet send may be delayed as the NAT-traversal code runs
151 // if we can't get through to the user, an error will be posted via the callback P2PSessionConnectFail_t
152 // see EP2PSend enum above for the descriptions of the different ways of sending packets
153 //
154 // nChannel is a routing number you can use to help route message to different systems - you'll have to call ReadP2PPacket()
155 // with the same channel number in order to retrieve the data on the other end
156 // using different channels to talk to the same user will still use the same underlying p2p connection, saving on resources
157 virtual bool SendP2PPacket( CSteamID steamIDRemote, const void *pubData, uint32 cubData, EP2PSend eP2PSendType, int nChannel = 0 ) = 0;
158
159 // returns true if any data is available for read, and the amount of data that will need to be read
160 virtual bool IsP2PPacketAvailable( uint32 *pcubMsgSize, int nChannel = 0 ) = 0;
161
162 // reads in a packet that has been sent from another user via SendP2PPacket()
163 // returns the size of the message and the steamID of the user who sent it in the last two parameters
164 // if the buffer passed in is too small, the message will be truncated
165 // this call is not blocking, and will return false if no data is available
166 virtual bool ReadP2PPacket( void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int nChannel = 0 ) = 0;
167
168 // AcceptP2PSessionWithUser() should only be called in response to a P2PSessionRequest_t callback
169 // P2PSessionRequest_t will be posted if another user tries to send you a packet that you haven't talked to yet
170 // if you don't want to talk to the user, just ignore the request
171 // if the user continues to send you packets, another P2PSessionRequest_t will be posted periodically
172 // this may be called multiple times for a single user
173 // (if you've called SendP2PPacket() on the other user, this implicitly accepts the session request)
174 virtual bool AcceptP2PSessionWithUser( CSteamID steamIDRemote ) = 0;
175
176 // call CloseP2PSessionWithUser() when you're done talking to a user, will free up resources under-the-hood
177 // if the remote user tries to send data to you again, another P2PSessionRequest_t callback will be posted
178 virtual bool CloseP2PSessionWithUser( CSteamID steamIDRemote ) = 0;
179
180 // call CloseP2PChannelWithUser() when you're done talking to a user on a specific channel. Once all channels
181 // open channels to a user have been closed, the open session to the user will be closed and new data from this
182 // user will trigger a P2PSessionRequest_t callback
183 virtual bool CloseP2PChannelWithUser( CSteamID steamIDRemote, int nChannel ) = 0;
184
185 // fills out P2PSessionState_t structure with details about the underlying connection to the user
186 // should only needed for debugging purposes
187 // returns false if no connection exists to the specified user
188 virtual bool GetP2PSessionState( CSteamID steamIDRemote, P2PSessionState_t *pConnectionState ) = 0;
189
190 // Allow P2P connections to fall back to being relayed through the Steam servers if a direct connection
191 // or NAT-traversal cannot be established. Only applies to connections created after setting this value,
192 // or to existing connections that need to automatically reconnect after this value is set.
193 //
194 // P2P packet relay is allowed by default
195 //
196 // NOTE: This function is deprecated and may be removed in a future version of the SDK. For
197 // security purposes, we may decide to relay the traffic to certain peers, even if you pass false
198 // to this function, to prevent revealing the client's IP address top another peer.
199 virtual bool AllowP2PPacketRelay( bool bAllow ) = 0;
200
201
203 //
204 // LISTEN / CONNECT connection-oriented interface functions
205 //
206 // These functions are more like a client-server TCP API. One side is the "server"
207 // and "listens" for incoming connections, which then must be "accepted." The "client"
208 // initiates a connection by "connecting." Sending and receiving is done through a
209 // connection handle.
210 //
211 // For a more UDP-style interface, where you do not track connection handles but
212 // simply send messages to a SteamID, use the UDP-style functions above.
213 //
214 // Both methods can send both reliable and unreliable methods.
215 //
216 // These APIs are deprecated, and may be removed in a future version of the Steamworks
217 // SDK. See ISteamNetworkingSockets.
218 //
220
221
222 // creates a socket and listens others to connect
223 // will trigger a SocketStatusCallback_t callback on another client connecting
224 // nVirtualP2PPort is the unique ID that the client will connect to, in case you have multiple ports
225 // this can usually just be 0 unless you want multiple sets of connections
226 // unIP is the local IP address to bind to
227 // pass in 0 if you just want the default local IP
228 // unPort is the port to use
229 // pass in 0 if you don't want users to be able to connect via IP/Port, but expect to be always peer-to-peer connections only
230 virtual SNetListenSocket_t CreateListenSocket( int nVirtualP2PPort, SteamIPAddress_t nIP, uint16 nPort, bool bAllowUseOfPacketRelay ) = 0;
231
232 // creates a socket and begin connection to a remote destination
233 // can connect via a known steamID (client or game server), or directly to an IP
234 // on success will trigger a SocketStatusCallback_t callback
235 // on failure or timeout will trigger a SocketStatusCallback_t callback with a failure code in m_eSNetSocketState
236 virtual SNetSocket_t CreateP2PConnectionSocket( CSteamID steamIDTarget, int nVirtualPort, int nTimeoutSec, bool bAllowUseOfPacketRelay ) = 0;
237 virtual SNetSocket_t CreateConnectionSocket( SteamIPAddress_t nIP, uint16 nPort, int nTimeoutSec ) = 0;
238
239 // disconnects the connection to the socket, if any, and invalidates the handle
240 // any unread data on the socket will be thrown away
241 // if bNotifyRemoteEnd is set, socket will not be completely destroyed until the remote end acknowledges the disconnect
242 virtual bool DestroySocket( SNetSocket_t hSocket, bool bNotifyRemoteEnd ) = 0;
243 // destroying a listen socket will automatically kill all the regular sockets generated from it
244 virtual bool DestroyListenSocket( SNetListenSocket_t hSocket, bool bNotifyRemoteEnd ) = 0;
245
246 // sending data
247 // must be a handle to a connected socket
248 // data is all sent via UDP, and thus send sizes are limited to 1200 bytes; after this, many routers will start dropping packets
249 // use the reliable flag with caution; although the resend rate is pretty aggressive,
250 // it can still cause stalls in receiving data (like TCP)
251 virtual bool SendDataOnSocket( SNetSocket_t hSocket, void *pubData, uint32 cubData, bool bReliable ) = 0;
252
253 // receiving data
254 // returns false if there is no data remaining
255 // fills out *pcubMsgSize with the size of the next message, in bytes
256 virtual bool IsDataAvailableOnSocket( SNetSocket_t hSocket, uint32 *pcubMsgSize ) = 0;
257
258 // fills in pubDest with the contents of the message
259 // messages are always complete, of the same size as was sent (i.e. packetized, not streaming)
260 // if *pcubMsgSize < cubDest, only partial data is written
261 // returns false if no data is available
262 virtual bool RetrieveDataFromSocket( SNetSocket_t hSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize ) = 0;
263
264 // checks for data from any socket that has been connected off this listen socket
265 // returns false if there is no data remaining
266 // fills out *pcubMsgSize with the size of the next message, in bytes
267 // fills out *phSocket with the socket that data is available on
268 virtual bool IsDataAvailable( SNetListenSocket_t hListenSocket, uint32 *pcubMsgSize, SNetSocket_t *phSocket ) = 0;
269
270 // retrieves data from any socket that has been connected off this listen socket
271 // fills in pubDest with the contents of the message
272 // messages are always complete, of the same size as was sent (i.e. packetized, not streaming)
273 // if *pcubMsgSize < cubDest, only partial data is written
274 // returns false if no data is available
275 // fills out *phSocket with the socket that data is available on
276 virtual bool RetrieveData( SNetListenSocket_t hListenSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, SNetSocket_t *phSocket ) = 0;
277
278 // returns information about the specified socket, filling out the contents of the pointers
279 virtual bool GetSocketInfo( SNetSocket_t hSocket, CSteamID *pSteamIDRemote, int *peSocketStatus, SteamIPAddress_t *punIPRemote, uint16 *punPortRemote ) = 0;
280
281 // returns which local port the listen socket is bound to
282 // *pnIP and *pnPort will be 0 if the socket is set to listen for P2P connections only
283 virtual bool GetListenSocketInfo( SNetListenSocket_t hListenSocket, SteamIPAddress_t *pnIP, uint16 *pnPort ) = 0;
284
285 // returns true to describe how the socket ended up connecting
287
288 // max packet size, in bytes
289 virtual int GetMaxPacketSize( SNetSocket_t hSocket ) = 0;
290};
291#define STEAMNETWORKING_INTERFACE_VERSION "SteamNetworking006"
292
293// Global interface accessor
296
297// Global accessor for the gameserver client
300
301// callbacks
302#if defined( VALVE_CALLBACK_PACK_SMALL )
303#pragma pack( push, 4 )
304#elif defined( VALVE_CALLBACK_PACK_LARGE )
305#pragma pack( push, 8 )
306#else
307#error steam_api_common.h should define VALVE_CALLBACK_PACK_xxx
308#endif
309
310// callback notification - a user wants to talk to us over the P2P channel via the SendP2PPacket() API
311// in response, a call to AcceptP2PPacketsFromUser() needs to be made, if you want to talk with them
313{
315 CSteamID m_steamIDRemote; // user who wants to talk to us
316};
317
318
319// callback notification - packets can't get through to the specified user via the SendP2PPacket() API
320// all packets queued packets unsent at this point will be dropped
321// further attempts to send will retry making the connection (but will be dropped if we fail again)
323{
325 CSteamID m_steamIDRemote; // user we were sending packets to
326 uint8 m_eP2PSessionError; // EP2PSessionError indicating why we're having trouble
327};
328
329
330// callback notification - status of a socket has changed
331// used as part of the CreateListenSocket() / CreateP2PConnectionSocket()
333{
335 SNetSocket_t m_hSocket; // the socket used to send/receive data to the remote host
336 SNetListenSocket_t m_hListenSocket; // this is the server socket that we were listening on; NULL if this was an outgoing connection
337 CSteamID m_steamIDRemote; // remote steamID we have connected to, if it has one
338 int m_eSNetSocketState; // socket state, ESNetSocketState
339};
340
341#pragma pack( pop )
342
343#endif // ISTEAMNETWORKING
virtual bool IsDataAvailable(SNetListenSocket_t hListenSocket, uint32 *pcubMsgSize, SNetSocket_t *phSocket)=0
virtual SNetSocket_t CreateConnectionSocket(SteamIPAddress_t nIP, uint16 nPort, int nTimeoutSec)=0
virtual bool SendP2PPacket(CSteamID steamIDRemote, const void *pubData, uint32 cubData, EP2PSend eP2PSendType, int nChannel=0)=0
virtual bool ReadP2PPacket(void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, CSteamID *psteamIDRemote, int nChannel=0)=0
virtual SNetListenSocket_t CreateListenSocket(int nVirtualP2PPort, SteamIPAddress_t nIP, uint16 nPort, bool bAllowUseOfPacketRelay)=0
virtual bool AllowP2PPacketRelay(bool bAllow)=0
virtual bool IsDataAvailableOnSocket(SNetSocket_t hSocket, uint32 *pcubMsgSize)=0
virtual bool IsP2PPacketAvailable(uint32 *pcubMsgSize, int nChannel=0)=0
virtual bool CloseP2PChannelWithUser(CSteamID steamIDRemote, int nChannel)=0
virtual bool GetListenSocketInfo(SNetListenSocket_t hListenSocket, SteamIPAddress_t *pnIP, uint16 *pnPort)=0
virtual SNetSocket_t CreateP2PConnectionSocket(CSteamID steamIDTarget, int nVirtualPort, int nTimeoutSec, bool bAllowUseOfPacketRelay)=0
virtual bool CloseP2PSessionWithUser(CSteamID steamIDRemote)=0
virtual bool SendDataOnSocket(SNetSocket_t hSocket, void *pubData, uint32 cubData, bool bReliable)=0
virtual int GetMaxPacketSize(SNetSocket_t hSocket)=0
virtual bool AcceptP2PSessionWithUser(CSteamID steamIDRemote)=0
virtual bool RetrieveData(SNetListenSocket_t hListenSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize, SNetSocket_t *phSocket)=0
virtual ESNetSocketConnectionType GetSocketConnectionType(SNetSocket_t hSocket)=0
virtual bool DestroySocket(SNetSocket_t hSocket, bool bNotifyRemoteEnd)=0
virtual bool DestroyListenSocket(SNetListenSocket_t hSocket, bool bNotifyRemoteEnd)=0
virtual bool RetrieveDataFromSocket(SNetSocket_t hSocket, void *pubDest, uint32 cubDest, uint32 *pcubMsgSize)=0
virtual bool GetSocketInfo(SNetSocket_t hSocket, CSteamID *pSteamIDRemote, int *peSocketStatus, SteamIPAddress_t *punIPRemote, uint16 *punPortRemote)=0
virtual bool GetP2PSessionState(CSteamID steamIDRemote, P2PSessionState_t *pConnectionState)=0
EP2PSessionError
@ k_EP2PSessionErrorNoRightsToApp
@ k_EP2PSessionErrorNone
@ k_EP2PSessionErrorMax
@ k_EP2PSessionErrorTimeout
@ k_EP2PSessionErrorNotRunningApp_DELETED
@ k_EP2PSessionErrorDestinationNotLoggedIn_DELETED
@ k_EP2PSendUnreliableNoDelay
@ k_EP2PSendReliable
@ k_EP2PSendReliableWithBuffering
@ k_EP2PSendUnreliable
#define STEAMNETWORKING_INTERFACE_VERSION
ISteamNetworking * SteamNetworking()
ISteamNetworking * SteamGameServerNetworking()
ESNetSocketConnectionType
@ k_ESNetSocketConnectionTypeUDP
@ k_ESNetSocketConnectionTypeUDPRelay
@ k_ESNetSocketConnectionTypeNotConnected
uint32 SNetSocket_t
ESNetSocketState
@ k_ESNetSocketStateConnected
@ k_ESNetSocketStateInitiated
@ k_ESNetSocketStateInvalid
@ k_ESNetSocketStateReceivedRemoteCandidates
@ k_ESNetSocketStateLocalCandidatesFound
@ k_ESNetSocketStateChallengeHandshake
@ k_ESNetSocketStateRemoteEndDisconnected
@ k_ESNetSocketStateTimeoutDuringConnect
@ k_ESNetSocketStateLocalDisconnect
@ k_ESNetSocketStateDisconnecting
@ k_ESNetSocketStateConnectionBroken
uint32 SNetListenSocket_t
const char *typedef uint32(S_CALLTYPE *SteamAPI_CheckCallbackRegistered_t)(int iCallbackNum)
#define STEAM_DEFINE_USER_INTERFACE_ACCESSOR(type, name, version)
@ k_iSteamNetworkingCallbacks
#define STEAM_DEFINE_GAMESERVER_INTERFACE_ACCESSOR(type, name, version)
unsigned short uint16
Definition steamtypes.h:56
int int32
Definition steamtypes.h:57
unsigned char uint8
Definition steamtypes.h:11
SNetListenSocket_t m_hListenSocket