2019-08-31 19:18:15 -07:00
|
|
|
/*
|
2022-07-07 09:10:14 -07:00
|
|
|
* Copyright (c) 2016-2022 Vera Visions LLC.
|
2019-08-31 19:18:15 -07:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2022-07-07 09:10:14 -07:00
|
|
|
*/
|
2018-12-27 14:53:24 -08:00
|
|
|
|
2024-01-01 21:20:02 -08:00
|
|
|
/*! @file tcp.h
|
|
|
|
@brief TCP/IP Networking API
|
|
|
|
|
|
|
|
The TCP API manages the sending/receiving of data over TCP sockets.
|
|
|
|
|
|
|
|
First, establish a connection with TCP_Connect(), which will return
|
|
|
|
a file descriptior.
|
|
|
|
If TCP_Connect returns a value below 0, no connection is possible.
|
|
|
|
You can send data through TCP_Send() once TCP_GetState returns
|
|
|
|
STATE_CONNECTED.
|
|
|
|
|
|
|
|
You need to run TCP_Frame() on your tcpinfo_t struct every frame
|
|
|
|
in order to listen to network activity.
|
|
|
|
*/
|
|
|
|
|
2021-11-20 08:27:52 -08:00
|
|
|
#define TCP_BUFFER_LENGTH 32
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** State of a TCP connection. */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
STATE_DISCONNECTED, /**< We're disconnected/unconnected. */
|
|
|
|
STATE_CONNECTING, /**< We're in the process of connecting. */
|
|
|
|
STATE_CONNECTED /**< We're fully connected. */
|
|
|
|
} tcpstate_t;
|
|
|
|
|
2021-11-20 08:27:52 -08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
filestream m_fSocket;
|
|
|
|
string m_strBuffer[TCP_BUFFER_LENGTH];
|
|
|
|
int m_iBufferLines;
|
2022-12-30 00:15:10 -08:00
|
|
|
tcpstate_t m_iState;
|
2021-11-20 08:27:52 -08:00
|
|
|
} tcpinfo_t;
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** Open a TCP socket connection.
|
2021-11-20 08:27:52 -08:00
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
@param in Pointer to a tcpinfo_t struct to save state into.
|
|
|
|
@param path The address:port to which we should connect to.
|
|
|
|
@return The descriptor for the newly opened socket. Will be below 0 if invalid. */
|
2021-11-20 08:27:52 -08:00
|
|
|
int TCP_Connect(tcpinfo_t *in, string path);
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** Closes a TCP socket connection.
|
|
|
|
|
|
|
|
@parm in Pointer to a tcpinfo_t that is to be closed. */
|
2021-11-20 08:27:52 -08:00
|
|
|
void TCP_Disconnect(tcpinfo_t *in);
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** Send a string buffer to a specified TCP connection.
|
|
|
|
|
|
|
|
@param in Pointer to an active TCP connection.
|
|
|
|
@param msg The message to send to the TCP connection. */
|
2021-11-20 08:27:52 -08:00
|
|
|
int TCP_Send(tcpinfo_t *in, string msg);
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** Receive the latest message from a TCP connection.
|
|
|
|
|
|
|
|
@param in Pointer to an active TCP connection.*/
|
2021-11-20 08:27:52 -08:00
|
|
|
string TCP_Receive(tcpinfo_t *in);
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** You want to run this every frame.
|
|
|
|
|
|
|
|
@param in Pointer to an active TCP connection. */
|
2021-11-20 08:27:52 -08:00
|
|
|
void TCP_Frame(tcpinfo_t *in);
|
|
|
|
|
2022-12-30 00:15:10 -08:00
|
|
|
/** Returns the tcpstate_t of a connection.
|
|
|
|
|
|
|
|
@param in Pointer to an active TCP connection.*/
|
|
|
|
tcpstate_t TCP_GetState(tcpinfo_t *in);
|