⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libnet.txt

📁 Libnet is a cross-platform library aimed at game developers. It has an abstract high level API, whic
💻 TXT
📖 第 1 页 / 共 4 页
字号:
     net_assigntarget (chan, "127.0.0.1:12345");2.2.4 net_getlocaladdress-------------------------Prototype.........     char *net_getlocaladdress(NET_CHANNEL *channel);Purpose.......   This function is used to discover the local address of achannel.Parameters..........   CHANNEL is the channel whose local address is wanted.Return value............   The address of CHANNEL is returned in the driver's normaladdress format.Notes.....   "local address" means the address of the channel accordingto this computer.  This might not be the address othercomputers should use; for example, a serial port driver wouldhave no way of knowing what port the other computer should use.The Internet sockets drivers have a bit of trouble with thistoo, since a computer can have more than one IP address andit's not trivial to find out even one of these.   Because of all this, it's probably best to tell the userthis local address and let them figure out what the othercomputer should use.Example.......     NET_CHANNEL *chan;     chan = net_openchannel (driver, binding);     printf ("Local address of channel: %s\n", net_getlocaladdress (chan));2.2.5 net_send--------------Prototype.........     int net_send(CHANNEL *channel,void *buffer,int size);Purpose.......   Sends data down a channel.Parameters..........   CHANNEL is the channel to send the data through.  BUFFERpoints to the data to send.  SIZE is the size of the data inbytes.Return value............   Zero on success, non-zero on error.Example.......   See Subsec 2.2.6: net_receive.2.2.6 net_receive-----------------Prototype.........     int net_receive(CHANNEL *channel,void *buffer,int maxsize,char *from);Purpose.......   Receives data from a channel.Parameters..........   CHANNEL is the channel to receive from.  BUFFER is a bufferto hold the data, of length MAXSIZE.  If FROM is not `NULL',the address of the source of the data will be stored in thebuffer it points to (which should be able to holdNET_MAX_ADDRESS_LENGTH characters).Return value............   Returns the number of bytes received.  0 is valid; there wasno data to read.  -1 indicates that an error occured.Example.......     NET_CHANNEL *chan;     char buffer1[32] = "Data to send";     char buffer2[32] = "";     int x;          chan = net_openchannel (NET_DRIVER_WSOCK, "");     net_assigntarget (chan, "127.0.0.1");          net_send (chan, buffer1, strlen (buffer1) + 1);          do {        x = net_receive (chan, buffer2, 32, NULL);     } while (x == 0);          if (x > 0)        printf ("Received data: %s\n", buffer2);     else        printf ("Error receiving data.\n");2.2.7 net_query---------------Prototype.........     int net_query(CHANNEL *channel);Purpose.......   This function checks to see if there is data waiting to beread from the channel.Parameters..........   CHANNEL is the channel to query.Return value............   Returns nonzero if data is waiting, zero if not.Example.......     if (net_query (chan)) get_data(chan);2.3 Connection Functions========================   Libnet's channels are unreliable -- there's no guarantee thata packet will arrive at its destination, nor that packets won'tget duplicated en route, nor that packets will arrive in theright order.  If you bear those facts in mind, channels shouldbe fine for most uses (in particular, cases where data is maderedundant very quickly by new incoming data).   Sometimes though you want to be able to send a packet and besure that it will reach its destination.  Libnet's second typeof communicator is the "connection".  A connection is a fixedlink between two computers.  You can't assign a new target.Packets sent along a connection are guaranteed to arriveprecisely once, and in the correct order.   Conns are referred to through pointers to `NET_CONN' objects.2.3.1 net_openconn------------------Prototype.........     NET_CONN *net_openconn (int type, char *binding);Purpose.......   Opens a conn over the specified network type.Parameters..........   TYPE is the type of the network to use.  BINDING candetermine the local binding.  See Subsec 2.2.1:net_openchannel, for more information about the binding.Return value............   The function returns a pointer to the NET_CONN structcreated, or NULL on error.2.3.2 net_closeconn-------------------Prototype.........     int net_closeconn (NET_CONN *conn);Purpose.......   Closes a previously opened conn.Parameters..........   CONN is the connection to be closed.Return value............   Returns zero on success.2.3.3 net_listen----------------Prototype.........     int net_listen (NET_CONN *conn);Purpose.......   Makes a conn start listening (waiting for connectionattempts).  Only works on an idle conn.Parameters..........   CONN is the conn that should start listening.Return value............   Returns zero on success, nonzero otherwise.2.3.4 net_poll_listen---------------------Prototype.........     NET_CONN *net_poll_listen (NET_CONN *conn);Purpose.......   Polls a listening channel for incoming connections.  Ifthere are any, this function accepts the first one queued andcreates a new conn to talk to the connecting computer.Parameters..........   CONN is the (listening) conn to poll.Return value............   If a new conn is created, this function returns a newNET_CONN * which the user can use to talk to the connectingcomputer.  Otherwise NULL is returned.2.3.5 net_connect-----------------Prototype.........     int net_connect (NET_CONN *conn, char *addr);Purpose.......   Initiates a connection attempt.  See also: Subsec 2.3.7:net_connect_wait_time, Subsec 2.3.8: net_connect_wait_cb,Subsec 2.3.9: net_connect_wait_cb_time.Parameters..........   CONN is the conn to connect; ADDR is the target address.Return value............   Returns zero if successful in initiating; nonzero otherwise.If the return value is zero, the app should keep calling`net_poll_connect' until a connection is established orrefused, or until the app gets bored.2.3.6 net_poll_connect----------------------Prototype.........     int net_poll_connect (NET_CONN *conn);Purpose.......   Polls a connecting conn to monitor connection progress.Parameters..........   CONN is the (connecting) conn to poll.Return value............   Returns zero if the connection is still in progress, nonzeroif the connection process has ended.  A nonzero return value iseither positive (connection established) or negative(connection not established).2.3.7 net_connect_wait_time---------------------------Prototype.........     int net_connect_wait_time (NET_CONN *conn, char *addr, int time);Purpose.......   This function uses `net_connect' and `net_poll_connect' toestablish a connection.  It waits until the connection processis completed or the time runs out.Parameters..........   CONN is the conn to connect with.  ADDR is the targetaddress.  TIME is the time in seconds to wait before giving up.Return value............   Returns zero if the connection is established, negative ifthere is an error (e.g. connection refused) and positive if thetime ran out.2.3.8 net_connect_wait_cb-------------------------Prototype.........     int net_connect_wait_cb (NET_CONN *conn, char *addr, int (*cb)());Purpose.......   This function uses `net_connect' and `net_poll_connect' toestablish a connection.  It waits, calling the callback functionregularly (once per second), until the connection process iscompleted or the callback function returns nonzero.Parameters..........   CONN is the conn to connect with.  ADDR is the targetaddress.  CB is the address of the callback function.Return value............   Returns zero if the connection is established, negative ifthere is an error (e.g. connection refused) and positive if thecallback function returned nonzero.2.3.9 net_connect_wait_cb_time------------------------------Prototype.........     int net_connect_wait_cb_time (NET_CONN *conn, char *addr, int (*cb)(), int time)Purpose.......   This function uses `net_connect' and `net_poll_connect' toestablish a connection.  It waits, calling the callback functionregularly (once per second), until the connection process iscompleted, the time runs out, or the callback function returnsnonzero.   Note that if the callback function is time consuming, thetime limit will be inaccurate.Parameters..........   CONN is the conn to connect with.  ADDR is the targetaddress.  CB is the callback function and TIME is the time inseconds to wait before giving up.Return value............   Returns zero if the connection is established, negative ifthere is an error (e.g. connection refused) and positive ifeither the time ran out or the callback function returnednonzero.2.3.10 net_send_rdm-------------------Prototype.........     int net_send_rdm (NET_CONN *conn, void *buffer, int size);Purpose.......   Sends data down a conn.  Analogous to Subsec 2.2.5: net_send.Parameters..........   CONN is the conn to send the packet down, BUFFER points tothe data to send and SIZE is the number of bytes to send.Return value............   Returning zero to indicate success or non-zero if an erroroccurs.2.3.11 net_receive_rdm----------------------Prototype.........     int net_receive_rdm (NET_CONN *conn, void *buffer, int maxsize);Purpose.......   Receives data from a conn.  Analogous to Subsec 2.2.6:net_receive.Parameters..........   CONN is the conn to receive from.  BUFFER points somewhereto store the packet, and MAXSIZE is the maximum number of bytesto store.Return value............   Returns the number of bytes received.  0 is a valid returntype; there was no data to read.  -1 indicates that an erroroccured.2.3.12 net_query_rdm--------------------Prototype.........     int net_query_rdm (NET_CONN *conn);Purpose.......   Tests whether data can be read from a conn.  Analogous toSubsec 2.2.7: net_query, but this function actually returns thesize of the next queued packet.Parameters..........   CONN is the conn to test.Return value............   The size of the next queued incoming packet, or 0 if nopackets are queued.     if (net_query_rdm (conn)) process_data(conn);2.3.13 net_ignore_rdm---------------------Prototype.........     int net_ignore_rdm (NET_CONN *conn);Purpose.......   If there are any incoming packets waiting to be read, thiscauses the first to be dropped; otherwise nothing happens.Note that the sender isn't notified, and will have received aconfirmation of the packet's arrival.  This function isintended for use if a large packet is in the queue and youweren't expecting to have to deal with it; call this functionto remove the packet.Parameters..........   CONN is the conn to operate on.Return value............   Non-zero if a packet was removed; zero if no packets werequeued, or if an error occured.     char buffer[1024];     int size = net_query_rdm (conn);     if (size > 0) {                    /* got some data */         if (size > sizeof buffer)       /* too much data */             net_ignore_rdm (conn);         else {             net_receive_rdm (conn, buffer, sizeof buffer);             ...         }     }2.3.14 net_conn_stats---------------------Prototype.........     int net_conn_stats (NET_CONN *conn, int *in_q, int *out_q);Purpose.......   This function fills in *IN_Q and *OUT_Q with the numbers ofpackets in the incoming and outgoing queues for the conn.  Ifeither pointer is `NULL', its will not be filled in.   I'm not entirely sure how useful this information is; maybesomebody can use it to optimise the way their game treats thenetwork.Parameters..........   CONN is the conn to test; IN_Q and OUT_Q are pointers tointegers which, if not `NULL', will be filled with the lengths

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -