cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/structures/CBObject/CBNetworkCommunicator/CBNetworkCommunicator.h File Reference

Used for communicating to other peers. The network communicator can send and receive bitcoin messages and uses function pointers for message handlers. The timeouts are in milliseconds. It is important to understant that a CBNetworkCommunicator does not guarentee thread safety for everything. Thread safety is only given to the "peers" list. This means it is completely okay to add and remove peers from multiple threads. Two threads may try to access the list at once such as if the CBNetworkCommunicator receives a socket timeout event and tries to remove an peer at the same time as a thread made by a program using cbitcoin tries to add a new peer. When using a CBNetworkCommunicator, threading and networking dependencies need to be satisfied,. More...

#include "CBPeer.h"
#include "CBDependencies.h"
#include "CBAddressManager.h"
#include "CBInventoryBroadcast.h"
#include "CBGetBlocks.h"
#include "CBTransaction.h"
#include "CBBlockHeaders.h"
#include "CBPingPong.h"
#include "CBAlert.h"

Go to the source code of this file.

Data Structures

struct  CBNetworkCommunicator
 Structure for CBNetworkCommunicator objects. More...

Functions

CBNetworkCommunicatorCBNewNetworkCommunicator (void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBNetworkCommunicator object.
CBNetworkCommunicatorCBGetNetworkCommunicator (void *self)
 Gets a CBNetworkCommunicator from another object. Use this to avoid casts.
bool CBInitNetworkCommunicator (CBNetworkCommunicator *self, void(*onErrorReceived)(CBError error, char *,...))
 Initialises a CBNetworkCommunicator object.
void CBFreeNetworkCommunicator (void *self)
 Frees a CBNetworkCommunicator object.
void CBNetworkCommunicatorAcceptConnection (void *vself, uint64_t socket)
 Accepts an incomming connection.
bool CBNetworkCommunicatorCanConnect (CBNetworkCommunicator *self, CBNetworkAddress *addr)
 Returns true if it is beleived the network address can be connected to, otherwise false.
CBConnectReturn CBNetworkCommunicatorConnect (CBNetworkCommunicator *self, CBPeer *peer)
 Connects to a peer. This peer will be added to the peer list if it connects correctly.
void CBNetworkCommunicatorDidConnect (void *vself, void *vpeer)
 Callback for the connection to a peer.
void CBNetworkCommunicatorDisconnect (CBNetworkCommunicator *self, CBPeer *peer, uint16_t penalty, bool stopping)
 Disconnects a peer.
CBVersionCBNetworkCommunicatorGetVersion (CBNetworkCommunicator *self, CBNetworkAddress *addRecv)
 Gets a new version message for this.
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoDiscovery (CBNetworkCommunicator *self, CBPeer *peer)
 Processes a new received message for auto discovery.
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoHandshake (CBNetworkCommunicator *self, CBPeer *peer)
 Processes a new received message for auto handshaking.
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoPingPong (CBNetworkCommunicator *self, CBPeer *peer)
 Processes a new received message for auto ping pongs.
void CBNetworkCommunicatorOnCanReceive (void *vself, void *vpeer)
 Called when a peer socket is ready for reading.
void CBNetworkCommunicatorOnCanSend (void *vself, void *vpeer)
 Called when a peer socket is ready for writing.
void CBNetworkCommunicatorOnHeaderRecieved (CBNetworkCommunicator *self, CBPeer *peer)
 Called when a header is received.
void CBNetworkCommunicatorOnLoopError (void *vself)
 Called on an error with the socket event loop. The error event is given with CB_ERROR_NETWORK_COMMUNICATOR_LOOP_FAIL.
void CBNetworkCommunicatorOnMessageReceived (CBNetworkCommunicator *self, CBPeer *peer)
 Called when an entire message is received.
void CBNetworkCommunicatorOnTimeOut (void *vself, void *vpeer, CBTimeOutType type)
 Called on a timeout error. The peer is removed.
bool CBNetworkCommunicatorSendMessage (CBNetworkCommunicator *self, CBPeer *peer, CBMessage *message)
 Sends a message by placing it on the send queue. Will serialise standard messages (unless serialised already) but not alternative messages or alert messages.
void CBNetworkCommunicatorSendPings (void *vself)
 Sends pings to all connected peers.
void CBNetworkCommunicatorSetAddressManager (CBNetworkCommunicator *self, CBAddressManager *addrMan)
 Sets the CBAddressManager.
void CBNetworkCommunicatorSetAlternativeMessages (CBNetworkCommunicator *self, CBByteArray *altMessages, uint32_t *altMaxSizes)
 Sets the alternative messages.
void CBNetworkCommunicatorSetOurIPv4 (CBNetworkCommunicator *self, CBNetworkAddress *ourIPv4)
 Sets the IPv4 address for the CBNetworkCommunicator.
void CBNetworkCommunicatorSetOurIPv6 (CBNetworkCommunicator *self, CBNetworkAddress *ourIPv6)
 Sets the IPv6 address for the CBNetworkCommunicator.
void CBNetworkCommunicatorSetUserAgent (CBNetworkCommunicator *self, CBByteArray *userAgent)
 Sets the user agent.
bool CBNetworkCommunicatorStart (CBNetworkCommunicator *self)
 Starts a CBNetworkCommunicator by connecting to the peers in the peers list. This starts the socket event loop.
void CBNetworkCommunicatorStartListening (CBNetworkCommunicator *self)
 Causes the CBNetworkCommunicator to begin listening for incomming connections. "isListeningIPv4" and/or "isListeningIPv6" should be set to true if either IPv4 or IPv6 sockets are active.
void CBNetworkCommunicatorStartPings (CBNetworkCommunicator *self)
 Starts the timer event for sending pings (heartbeat).
void CBNetworkCommunicatorStop (CBNetworkCommunicator *self)
 Closes all connections. This may be neccessary in case of failure in which case CBNetworkCommunicatorStart can be tried again to reconnect to the listed peers.
void CBNetworkCommunicatorStopListening (CBNetworkCommunicator *self)
 Stops listening for both IPv6 connections and IPv4 connections.
void CBNetworkCommunicatorStopPings (CBNetworkCommunicator *self)
 Stops the timer event for sending pings (heartbeat).
void CBNetworkCommunicatorTryConnections (CBNetworkCommunicator *self)
 Looks at the stored addresses and tries to connect to addresses up to the maximum number of allowed connections or as many as there are in the case the maximum number of connections is greater than the number of addresses, plus connected peers.

Detailed Description

Used for communicating to other peers. The network communicator can send and receive bitcoin messages and uses function pointers for message handlers. The timeouts are in milliseconds. It is important to understant that a CBNetworkCommunicator does not guarentee thread safety for everything. Thread safety is only given to the "peers" list. This means it is completely okay to add and remove peers from multiple threads. Two threads may try to access the list at once such as if the CBNetworkCommunicator receives a socket timeout event and tries to remove an peer at the same time as a thread made by a program using cbitcoin tries to add a new peer. When using a CBNetworkCommunicator, threading and networking dependencies need to be satisfied,.

See also:
CBDependencies.h Inherits CBObject

Function Documentation

void CBFreeNetworkCommunicator ( void *  self)

Frees a CBNetworkCommunicator object.

Parameters:
selfThe CBNetworkCommunicator object to free.

Gets a CBNetworkCommunicator from another object. Use this to avoid casts.

Parameters:
selfThe object to obtain the CBNetworkCommunicator from.
Returns:
The CBNetworkCommunicator object.
bool CBInitNetworkCommunicator ( CBNetworkCommunicator self,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Initialises a CBNetworkCommunicator object.

Parameters:
selfThe CBNetworkCommunicator object to initialise
Returns:
true on success, false on failure.
void CBNetworkCommunicatorAcceptConnection ( void *  vself,
uint64_t  socket 
)

Accepts an incomming connection.

Parameters:
vselfThe CBNetworkCommunicator object.
socketThe listening socket for accepting a connection.

Returns true if it is beleived the network address can be connected to, otherwise false.

Parameters:
selfThe CBNetworkCommunicator object.
addrThe CBNetworkAddress.
Returns:
true if it is beleived the peer can be connected to, otherwise false.
CBConnectReturn CBNetworkCommunicatorConnect ( CBNetworkCommunicator self,
CBPeer peer 
)

Connects to a peer. This peer will be added to the peer list if it connects correctly.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe peer to connect to.
Returns:
CB_CONNECT_OK if successful. CB_CONNECT_NO_SUPPORT if the IP version is not supported. CB_CONNECT_BAD if the connection failed and the address will be penalised. CB_CONNECT_FAIL if the connection failed but the address will not be penalised.
void CBNetworkCommunicatorDidConnect ( void *  vself,
void *  vpeer 
)

Callback for the connection to a peer.

Parameters:
vselfThe CBNetworkCommunicator object.
vpeerThe CBPeer that connected.
void CBNetworkCommunicatorDisconnect ( CBNetworkCommunicator self,
CBPeer peer,
uint16_t  penalty,
bool  stopping 
)

Disconnects a peer.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe peer.
penaltyPenalty to the score of the address.
stoppingIf true, do not call "onNetworkError" because the CBNetworkCommunicator is stopping.

Gets a new version message for this.

Parameters:
selfThe CBNetworkCommunicator object.
addRecvThe CBNetworkAddress of the receipient.
void CBNetworkCommunicatorOnCanReceive ( void *  vself,
void *  vpeer 
)

Called when a peer socket is ready for reading.

Parameters:
vselfThe CBNetworkCommunicator object.
vpeerThe CBPeer index with data to read.
void CBNetworkCommunicatorOnCanSend ( void *  vself,
void *  vpeer 
)

Called when a peer socket is ready for writing.

Parameters:
vselfThe CBNetworkCommunicator object.
vpeerThe CBPeer

Called when a header is received.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe CBPeer.
void CBNetworkCommunicatorOnLoopError ( void *  vself)

Called on an error with the socket event loop. The error event is given with CB_ERROR_NETWORK_COMMUNICATOR_LOOP_FAIL.

Parameters:
vselfThe CBNetworkCommunicator object.

Called when an entire message is received.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe CBPeer.
void CBNetworkCommunicatorOnTimeOut ( void *  vself,
void *  vpeer,
CBTimeOutType  type 
)

Called on a timeout error. The peer is removed.

Parameters:
vselfThe CBNetworkCommunicator object.
vpeerThe CBPeer index which timedout.
typeThe type of the timeout
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoDiscovery ( CBNetworkCommunicator self,
CBPeer peer 
)

Processes a new received message for auto discovery.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe peer
Returns:
true if peer should be disconnected, false otherwise.
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoHandshake ( CBNetworkCommunicator self,
CBPeer peer 
)

Processes a new received message for auto handshaking.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe peer
Returns:
true if peer should be disconnected, false otherwise.
CBOnMessageReceivedAction CBNetworkCommunicatorProcessMessageAutoPingPong ( CBNetworkCommunicator self,
CBPeer peer 
)

Processes a new received message for auto ping pongs.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe peer
Returns:
true if peer should be disconnected, false otherwise.
bool CBNetworkCommunicatorSendMessage ( CBNetworkCommunicator self,
CBPeer peer,
CBMessage message 
)

Sends a message by placing it on the send queue. Will serialise standard messages (unless serialised already) but not alternative messages or alert messages.

Parameters:
selfThe CBNetworkCommunicator object.
peerThe CBPeer.
messageThe CBMessage to send.
Returns:
true if successful, false otherwise.
void CBNetworkCommunicatorSendPings ( void *  vself)

Sends pings to all connected peers.

Parameters:
selfThe CBNetworkCommunicator object.

Sets the CBAddressManager.

Parameters:
selfThe CBNetworkCommunicator object.
addrThe CBAddressManager
void CBNetworkCommunicatorSetAlternativeMessages ( CBNetworkCommunicator self,
CBByteArray altMessages,
uint32_t *  altMaxSizes 
)

Sets the alternative messages.

Parameters:
selfThe alternative messages as a CBByteArray with 12 characters per message command, one after the other.
altMaxSizesAn allocated memory block of 32 bit integers with the max sizes for the alternative messages.
addrThe CBAddressManager

Sets the IPv4 address for the CBNetworkCommunicator.

Parameters:
selfThe CBNetworkCommunicator object.
addrThe IPv4 address as a CBNetworkAddress.

Sets the IPv6 address for the CBNetworkCommunicator.

Parameters:
selfThe CBNetworkCommunicator object.
addrThe IPv6 address as a CBNetworkAddress.

Sets the user agent.

Parameters:
selfThe CBNetworkCommunicator object.
addrThe user agent as a CBByteArray.

Starts a CBNetworkCommunicator by connecting to the peers in the peers list. This starts the socket event loop.

Parameters:
selfThe CBNetworkCommunicator object.
Returns:
true if the CBNetworkCommunicator started successfully, false otherwise.

Causes the CBNetworkCommunicator to begin listening for incomming connections. "isListeningIPv4" and/or "isListeningIPv6" should be set to true if either IPv4 or IPv6 sockets are active.

Parameters:
vselfThe CBNetworkCommunicator object.

Starts the timer event for sending pings (heartbeat).

Parameters:
vselfThe CBNetworkCommunicator object.

Closes all connections. This may be neccessary in case of failure in which case CBNetworkCommunicatorStart can be tried again to reconnect to the listed peers.

Parameters:
vselfThe CBNetworkCommunicator object.

Stops listening for both IPv6 connections and IPv4 connections.

Parameters:
vselfThe CBNetworkCommunicator object.

Stops the timer event for sending pings (heartbeat).

Parameters:
vselfThe CBNetworkCommunicator object.

Looks at the stored addresses and tries to connect to addresses up to the maximum number of allowed connections or as many as there are in the case the maximum number of connections is greater than the number of addresses, plus connected peers.

Parameters:
selfThe CBNetworkCommunicator object.
CBNetworkCommunicator* CBNewNetworkCommunicator ( void(*)(CBError error, char *,...)  onErrorReceived)

Creates a new CBNetworkCommunicator object.

Returns:
A new CBNetworkCommunicator object.