cwSocket.h/cpp,Makefile : New socket system implementation.

This commit is contained in:
kpl 2020-03-16 22:58:55 -04:00
parent f2ec64c722
commit f105d2d9db
4 changed files with 1418 additions and 3 deletions

View File

@ -1,6 +1,6 @@
HDR = cwCommon.h cwCommonImpl.h cwMem.h cwLog.h cwUtility.h
SRC = cwCommonImpl.cpp cwMem.cpp cwLog.cpp cwUtility.cpp
HDR = cwCommon.h cwCommonImpl.h cwMem.h cwLog.h cwUtility.h
SRC = cwCommonImpl.cpp cwMem.cpp cwLog.cpp cwUtility.cpp
HDR += cwFileSys.h cwText.h cwFile.h cwTime.h cwLex.h cwNumericConvert.h
SRC += cwFileSys.cpp cwText.cpp cwFile.cpp cwTime.cpp cwLex.cpp
@ -24,7 +24,10 @@ HDR += cwAudioBuf.h cwAudioDevice.h cwAudioDeviceAlsa.h
SRC += cwAudioBuf.cpp cwAudioDevice.cpp cwAudioDeviceAlsa.cpp cwAudioDeviceTest.cpp
HDR += cwTcpSocket.h cwTcpSocketSrv.h cwTcpSocketTest.h
SRC += cwTcpSocket.cpp cwTcpSocketSrv.cpp cwTcpSocketTest.cpp
SRC += cwTcpSocket.cpp cwTcpSocketSrv.cpp cwTcpSocketTest.cpp
HDR += cwSocket.h
SRC += cwSocket.cpp
HDR += cwMdns.h cwEuCon.h cwDnsSd.h dns_sd/dns_sd.h dns_sd/dns_sd_print.h dns_sd/dns_sd_const.h dns_sd/fader.h dns_sd/rpt.h
SRC += cwMdns.cpp cwEuCon.cpp cwDnsSd.cpp dns_sd/dns_sd.cpp dns_sd/dns_sd_print.cpp dns_sd/fader.cpp dns_sd/rpt.cpp

1239
cwSocket.cpp Normal file

File diff suppressed because it is too large Load Diff

142
cwSocket.h Normal file
View File

@ -0,0 +1,142 @@
#ifndef cwSocket_H
#define cwSocket_H
namespace cw
{
namespace sock
{
typedef handle< struct mgr_str > handle_t;
typedef uint16_t portNumber_t;
typedef enum
{
kReceiveCbId,
kConnectCbId,
kDisconnectCbId
} cbId_t;
// userId is the id assigned to a socket created with kStreamFl | kListenFl
// connId is an automatically id which is assigned to represent the remote endpoint which is connected to 'userId'.
typedef void (*callbackFunc_t)( void* cbArg, cbId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, struct sockaddr_in* srcAddr );
enum
{
kNonBlockingFl = 0x000, // Create a non-blocking socket.
kBlockingFl = 0x001, // Create a blocking socket.
kTcpFl = 0x002, // Create a TCP socket rather than a UDP socket.
kBroadcastFl = 0x004, //
kReuseAddrFl = 0x008, //
kReusePortFl = 0x010, //
kMultiCastTtlFl = 0x020, //
kMultiCastLoopFl = 0x040, //
kListenFl = 0x080, // Use this socket to listen for incoming connections
kStreamFl = 0x100, // Connected stream (not Datagram)
};
enum
{
// port 0 is reserved by and is therefore a convenient invalid port number
kInvalidPortNumber = 0
};
rc_t createMgr( handle_t& hRef, unsigned recvBufByteN, unsigned maxSocketN );
rc_t destroyMgr( handle_t& hRef );
rc_t create( handle_t h,
unsigned userId,
short port,
unsigned flags,
unsigned timeOutMs = 100, // time out to use with recv() on blocking sockets
callbackFunc_t cbFunc = nullptr,
void* cbArg = nullptr,
const char* remoteAddr = nullptr,
portNumber_t remotePort = sock::kInvalidPortNumber,
const char* localAddr = nullptr );
rc_t destroy( handle_t h, unsigned userId );
rc_t set_multicast_time_to_live( handle_t h, unsigned userId, unsigned seconds );
rc_t join_multicast_group( handle_t h, unsigned userId, const char* addr );
// Send to the remote endpoint represented by connId over a connected socket.
// If 'connId' is kInvalidId then this data is sent to all connected endpoints.
rc_t send( handle_t h, unsigned userId, unsigned connId, const void* data, unsigned dataByteN );
// Send a message to a specific remote node over an unconnected UDP socket.
// Use the function initAddr() to setup the 'sockaddr_in';
rc_t send( handle_t h, unsigned userId, const void* data, unsigned dataByteCnt, const struct sockaddr_in* remoteAddr );
rc_t send( handle_t h, unsigned userId, const void* data, unsigned dataByteCnt, const char* remoteAddr, portNumber_t port );
// Set a destination address for this socket. Once a destination address is set
// the caller may use send() to communicate with the specified remote socket
// without having to specify a destination address on each call.
rc_t connect( handle_t h, unsigned userId, const char* remoteAddr, portNumber_t port );
// Return true if this socket is connected to a remote endpoint.
bool isConnected( handle_t h, unsigned userId );
// Blocking - Wait up to timeOutMs milliseconds for data to be available on any open sockets.
// Deliver received data via the port callback function.
// readN_Ref returns the total count of bytes read across all ports.
rc_t receive_all( handle_t h, unsigned timeOutMs, unsigned& readByteN_Ref );
// Receive incoming messages by directly checking the internal
// socket for waiting data. This function is used to receive
// incoming data when the internal listening thread is not used.
// Note that if kBlockingFl was set in create() this call will
// block for available data or for 'timeOutMs' milliseconds,
// whichever comes first (as set in create()). If
// kNonBlockingFl was set in create() then the function will
// return immediately if no incoming messages are waiting. If
// recvByteCntRef is valid (non-NULL) then it is set to the
// length of the received message or 0 if no msg was received.
rc_t receive(handle_t h, unsigned userId, unsigned& readByteN_Ref, void* buf=nullptr, unsigned bufByteN=0, struct sockaddr_in* fromAddr=nullptr );
// Note that
rc_t get_mac( handle_t h, unsigned userId, unsigned char buf[6], struct sockaddr_in* addr=nullptr, const char* netInterfaceName=nullptr );
const char* hostName( handle_t h, unsigned userId );
const char* ipAddress( handle_t h, unsigned userId );
unsigned inetAddress( handle_t h, unsigned useId );
portNumber_t port( handle_t h, unsigned userId );
rc_t peername( handle_t h, unsigned userId, struct sockaddr_in* addr );
rc_t get_info( const char* netInterfaceName, unsigned char mac[6], char* hostBuf=nullptr, unsigned hostBufN=_POSIX_HOST_NAME_MAX, struct sockaddr_in* addr=nullptr );
// Prepare a struct sockadddr_in for use with send()
rc_t initAddr( const char* addrStr, portNumber_t portNumber, struct sockaddr_in* retAddrPtr );
rc_t addrToString( const struct sockaddr_in* addr, char* buf, unsigned bufN=INET_ADDRSTRLEN );
bool addrIsEqual( const struct sockaddr_in* addr0, const struct sockaddr_in* addr1 );
}
//===============================================================================================
namespace socksrv
{
typedef handle< struct socksrv_str > handle_t;
rc_t createMgrSrv( handle_t& hRef, unsigned timeOutMs, unsigned recvBufByteN, unsigned maxSocketN );
rc_t destroyMgrSrv( handle_t& hRef );
sock::handle_t mgrHandle( handle_t h );
rc_t start( handle_t h );
rc_t stop( handle_t h );
rc_t test( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort );
rc_t testServer( sock::portNumber_t localPort );
rc_t testClient( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort );
}
}
#endif

View File

@ -13,6 +13,7 @@
#include "cwWebSockSvr.h"
#include "cwSerialPort.h"
#include "cwSerialPortSrv.h"
#include "cwSocket.h"
#include "cwMidi.h"
#include "cwTime.h"
#include "cwMidiPort.h"
@ -203,6 +204,33 @@ void socketSrvTcpTest( cw::object_t* cfg, int argc, const char* argv[] )
}
}
void sockServerTest( cw::object_t* cfg, int argc, const char* argv[] )
{
if( argc >= 2 )
{
unsigned short localPort = atoi(argv[1]);
printf("local port:%i\n", localPort );
cw::socksrv::testServer( localPort );
}
}
void sockClientTest( cw::object_t* cfg, int argc, const char* argv[] )
{
if( argc >= 4 )
{
unsigned short localPort = atoi(argv[1]);
const char* remoteIp = argv[2];
unsigned short remotePort = atoi(argv[3]);
printf("local:%i to remote:%s %i\n", localPort, remoteIp, remotePort);
cw::socksrv::testClient(localPort, remoteIp, remotePort );
}
}
void socketMdnsTest( cw::object_t* cfg, int argc, const char* argv[] )
{
cw::net::mdns::test();
@ -288,6 +316,9 @@ int main( int argc, const char* argv[] )
{ "socketTcp", socketTestTcp },
{ "socketSrvUdp", socketSrvUdpTest },
{ "socketSrvTcp", socketSrvTcpTest },
{ "sockServer", sockServerTest },
{ "sockClient", sockClientTest },
{ "socketMdns", socketMdnsTest },
{ "dnssd", dnsSdTest },
{ "eucon", euConTest },