-
Notifications
You must be signed in to change notification settings - Fork 0
Miknet is based around the concept of nodes. Each machine or session is a node, and can be connected to other nodes. Connections to other nodes are represented as an array of "peers". Each node has an array of slots where peers will be stored as they connect.
Once you have a node, you will need to call miknode_poll() on it at regular intervals. It will handle sending your packets and receiving information from peers, such as them connecting, leaving, and sending data. After a call, call mikevent() for a pointer to an event that needs handling. Keep calling it until it returns NULL, which means that you have handled all events.
To queue data for sending to peers, use miknode_send().
To connect to peers, use miknode_connect(). To disconnect from peers, call mikpeer_close() on the peer you wish to disconnect from.
None of the fields in the structures below should be changed, but the fields that might be useful to look at are included.
- mikip_t ip: the IP family of the node, either MIK_IPV4 or MIK_IPV6
- mikpeer_t *peers: an array of mikpeer_t objects of size peermax
- uint16_t peerc: the current amount of connected peers
- uint16_t peermax: the maximum amount of peers who can connect to this node
- void *data: this one should be modified; use it to point to your own data for the peer
- mikstate_t state: either MIK_CONN for connected, or MIK_DISC for an empty slot
- uint32_t sent: total amount of data in bytes sent to this peer
- uint32_t recvd: total amount of data in bytes received from this peer
- miktype_t type: the type of event received; can be MIK_JOIN, MIK_QUIT for connections, and MIK_DATA for packets
- uint32_t channel: the channel on which the event arrived
- uint16_t peer: the position of the peer from whom the event originated in the local node's peer array
- void *data: the data in the event; only set if it is of type MIK_DATA
- uint16_t len: length of the data pointed to by the data field
Call this to construct a node.
declaration: int miknode (miknode_t *n, mikip_t ip, uint16_t port, uint16_t peers);
parameters:
- miknode_t *n: a pointer to a zero-initialized miknode_t
- mikip_t ip: an ip family; MIK_IPV4 or MIK_IPV6
- uint16_t port: a port number; if the port is taken miknode creation will fail
- uint16_t peers: the maximum amount of peers this node will store
return:
- For an argument or system error, a value less than zero (call mik_errstr() on the error for info)
- If the port is taken, it will return zero
- If everything works as planned, it will return one
Call this to connect to a foreign host.
declaration: int miknode_connect(miknode_t *n, const char *a, uint16_t p)
parameters:
- miknode_t *n: pointer to the local node
- const char *a: a pointer to the address string, e.g. "10.0.0.63" or "www.google.com"
- uint16_t p: the port to connect on
return:
- For an argument or system error, a value less than 0
- When successful, the position of the newly connect peer in the local node's peer array (can be zero)
Call this to send data to peer in one of the node's slots. Do not call this on a peer marked MIK_DISC. This will only queue a packet to be sent; it will not actually send it until miknode_poll is called.
declaration: int miknode_send (mikpeer_t *p, ref *d, size_t len, uint32_t channel);
parameters:
- mikpeer_t *p: the peer to send data to
- red *d: a const void pointer to the data
- size_t len: the amount of bytes to read from the pointer
- uint32_t: the channel over which to send this data
return:
- In the case of an argument error, a value less than zero
- In the case of success, zero
declaration: int miknode_poll (miknode_t *n, int t);
parameters:
- miknode_t *n: pointer to the node to service
- int t: time in milliseconds to poll peers for incoming traffic
return:
- the amount of events that need to be handled
declaration: void miknode_close (miknode_t *n);
parameters:
- miknode_t *n: a pointer to the node you are done using
declaration: int mikpeer_close (mikpeer_t *p);
parameters:
- mikpeer_t *p: a pointer to the peer to close
return:
- irrelevant; always zero, at least for now
After a miknode_poll() call, call mikevent() until it returns NULL. It is important that all events are handled before polling again, or those events will be lost.
declaration: mikpack_t *mikevent (miknode_t *node);
parameters:
- miknode_t *node: pointer to the node to get events from
return:
- a pointer to the next event ready for handling, or NULL when all events are handled, type mikpack_t