425 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			425 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
| /* c4coml.c  (c)Copyright Sequiter Software Inc., 1988-1996. All rights reserved.*/
 | |
| 
 | |
| #include "d4all.h"
 | |
| #ifndef S4OFF_COMMUNICATIONS
 | |
| #ifdef S4LOCAL
 | |
| 
 | |
| CONNECTION4NET *S4FUNCTION connection4netAlloc( SOCKET4NET *socket4net )
 | |
| {
 | |
|    CONNECTION4NET *net ;
 | |
|    HINSTANCE hInst ;
 | |
|    int rc = 0 ;
 | |
|    CODE4 *c4 ;
 | |
| 
 | |
|    #ifdef E4PARM_LOW
 | |
|       if ( socket4net == 0 )
 | |
|       {
 | |
|          error4( 0, e4parm_null, E90201 ) ;
 | |
|          return 0 ;
 | |
|       }
 | |
|    #endif
 | |
| 
 | |
|    c4 = socket4net->c4 ;
 | |
| 
 | |
|    if ( c4->connectionNetMemory == 0 )
 | |
|    {
 | |
|       #ifdef S4SERVER
 | |
|          c4->connectionNetMemory = mem4create( c4, 10, sizeof( CONNECTION4NET ), 5, 0 ) ;
 | |
|       #else
 | |
|          c4->connectionNetMemory = mem4create( c4, 2, sizeof( CONNECTION4NET ), 1, 0 ) ;
 | |
|       #endif
 | |
|       if ( c4->connectionNetMemory == 0 )
 | |
|          return 0 ;
 | |
|    }
 | |
|    net = (CONNECTION4NET *)mem4alloc( c4->connectionNetMemory ) ;
 | |
|    if( net == 0 )
 | |
|       rc = e4memory ;
 | |
|    else
 | |
|    {
 | |
|       net->c4 = c4 ;
 | |
|       hInst = socket4net->hInst ;
 | |
|       if( hInst == 0 )
 | |
|          rc = e4loadlib ;
 | |
|       else
 | |
|       {
 | |
|          net->hInst = hInst ;
 | |
|          #ifdef S4WIN32
 | |
|             c4getFuncPtr( hInst, "connection4netAlloc", (void **)(&(net->alloc)) ) ;
 | |
|          #else
 | |
|             c4getFuncPtr( hInst, "CONNECTION4NETALLOC", (void **)(&(net->alloc)) ) ;
 | |
|          #endif
 | |
| 
 | |
|          if( net->alloc == 0 )
 | |
|             rc = e4loadlib ;
 | |
|          else
 | |
|          {
 | |
|             if( ( net->connection4net = (*net->alloc)(socket4net->socket4net) ) == 0 )
 | |
|                rc = e4memory ;
 | |
|             else
 | |
|             {
 | |
|                #ifdef S4WIN32
 | |
| 
 | |
|                c4getFuncPtr( hInst, "connection4netInit", (void **)(&net->init) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netInitUndo", (void **)(&net->initUndo) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netFree", (void **)(&net->free) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netDisconnect", (void **)(&net->disconnect) ) ;
 | |
|                #ifdef S4SERVER
 | |
|                   c4getFuncPtr( hInst, "connection4netDisconnected", (void **)(&net->disconnected) ) ;
 | |
|                   c4getFuncPtr( hInst, "connection4netUserId", (void **)(&net->userId) ) ;
 | |
|                #endif
 | |
|                c4getFuncPtr( hInst, "connection4netId", (void **)(&net->id) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netDestId", (void **)(&net->destId) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netSend", (void **)(&net->send) ) ;
 | |
|                c4getFuncPtr( hInst, "connection4netMessageLen", (void **)(&net->messageLen) ) ;
 | |
| 
 | |
|                #else /* Not S4WIN32 */
 | |
| 
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETINIT", (void **)(&net->init) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETINITUNDO", (void **)(&net->initUndo) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETFREE", (void **)(&net->free) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETDISCONNECT", (void **)(&net->disconnect) ) ;
 | |
|                #ifdef S4SERVER
 | |
|                   c4getFuncPtr( hInst, "CONNECTION4NETDISCONNECTED", (void **)(&net->disconnected) ) ;
 | |
|                   c4getFuncPtr( hInst, "CONNECTION4NETUSERID", (void **)(&net->userId) ) ;
 | |
|                #endif
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETID", (void **)(&net->id) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETDESTID", (void **)(&net->destId) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETSEND", (void **)(&net->send) ) ;
 | |
|                c4getFuncPtr( hInst, "CONNECTION4NETMESSAGELEN", (void **)(&net->messageLen) ) ;
 | |
| 
 | |
|                #endif /* S4WIN32 */
 | |
| 
 | |
|                if ( net->init == 0 || net->initUndo == 0 || net->free == 0 ||
 | |
|                     #ifdef S4SERVER
 | |
|                        net->disconnected == 0 || net->userId == 0 ||
 | |
|                     #endif
 | |
|                     net->disconnect == 0 || net->id == 0 || net->destId == 0 ||
 | |
|                     net->send == 0 || net->messageLen == 0 )
 | |
|                   rc = e4loadlib ;
 | |
|                else
 | |
|                   socket4net->hInst = hInst ;
 | |
|             }
 | |
|          }
 | |
|       }
 | |
|    }
 | |
| 
 | |
| 
 | |
|    if ( rc < 0 )
 | |
|    {
 | |
|       if( net )
 | |
|          u4free( net ) ;
 | |
|       if ( error4code( socket4net->c4 ) == 0 )
 | |
|          error4( socket4net->c4, rc, E90201 ) ;
 | |
|    }
 | |
| 
 | |
|    return net ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION connection4netDisconnect( CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->disconnect == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
|    return (( *net->disconnect )( net->connection4net )) ;
 | |
| }
 | |
| 
 | |
| #ifdef S4SERVER
 | |
| int S4FUNCTION connection4netDisconnected( CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->disconnected == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
|    return ( *net->disconnected )( net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| char *S4FUNCTION connection4netUserId( CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->userId == 0 )
 | |
|    {
 | |
|       error4( net->c4, e4loadlib, E90201 ) ;
 | |
|       return 0 ;
 | |
|    }
 | |
|    return (char *)(( *net->userId )( net->connection4net )) ;
 | |
| }
 | |
| #endif
 | |
| 
 | |
| int S4FUNCTION connection4netFree( CONNECTION4NET *net )
 | |
| {
 | |
|    int rc ;
 | |
| 
 | |
|    if ( net->free == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    rc = (*net->free)(net->connection4net) ;
 | |
|    mem4free( net->c4->connectionNetMemory, net ) ;
 | |
|    return rc ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION connection4netInit( CONNECTION4NET *net, SOCKET4NET *socket4net )
 | |
| {
 | |
|    if ( net->init == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->init )( net->connection4net, socket4net->socket4net ) ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION connection4netInitUndo( CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->initUndo == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->initUndo )( net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| long S4FUNCTION connection4netDestId( const CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->destId == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->destId )( net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| CONNECTION4ID S4FUNCTION connection4netId( const CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->id == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->id )( net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| 
 | |
| S4CONST unsigned short S4FUNCTION connection4netMessageLen( CONNECTION4NET *net )
 | |
| {
 | |
|    if ( net->messageLen == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->messageLen )( net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| 
 | |
| int S4FUNCTION connection4netSend( CONNECTION4NET *net, const MESSAGE4 *message )
 | |
| {
 | |
|    if ( net->send == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->send )( net->connection4net, message ) ;
 | |
| }
 | |
| 
 | |
| void S4FUNCTION c4getFuncPtr( HINSTANCE hInst, char * funcName, void **function )
 | |
| {
 | |
|    *function = (void *)GetProcAddress( hInst, funcName ) ;
 | |
| }
 | |
| 
 | |
| void S4FUNCTION socket4netYield( const SOCKET4NET *net )
 | |
| {
 | |
|    if ( net->yield == 0 )
 | |
|       error4( net->c4, e4loadlib, E90235 ) ;
 | |
|    else
 | |
|       ( *net->yield )( net->socket4net ) ;
 | |
| }
 | |
| 
 | |
| MESSAGE4 *S4FUNCTION socket4netReceive( SOCKET4NET *net )
 | |
| {
 | |
|    if ( net->receive == 0 )
 | |
|    {
 | |
|       error4( net->c4, e4loadlib, E90201 ) ;
 | |
|       return 0 ;
 | |
|    }
 | |
|    return (MESSAGE4 *)( *net->receive )( net->socket4net ) ;
 | |
| }
 | |
| 
 | |
| #ifdef S4SERVER
 | |
| int S4FUNCTION socket4netConnectClient( SOCKET4NET *net, CONNECTION4 *connection, CONNECTION4NET *connect4net )
 | |
| {
 | |
|    if ( net->connectClient == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->connectClient )( net->socket4net, connection, connect4net->connection4net ) ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION socket4netInitServer( SOCKET4NET *net, const char *c1, const char *c2 )
 | |
| {
 | |
|    if ( net->initServer == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->initServer )( net->socket4net, c1, c2 ) ;
 | |
| }
 | |
| #else
 | |
| int S4FUNCTION socket4netConnectServer( SOCKET4NET *net, CONNECTION4NET *connection, const char *s1, const char *s2 )
 | |
| {
 | |
|    if ( net->connectServer == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->connectServer )( net->socket4net, connection->connection4net, s1, s2 ) ;
 | |
| }
 | |
| #endif
 | |
| 
 | |
| int S4FUNCTION socket4netInitUndo( SOCKET4NET *net )
 | |
| {
 | |
|    int rc = 0 ;
 | |
| 
 | |
|    if ( net->initUndone == 0 )
 | |
|    {
 | |
|       if ( net->initUndo == 0 )
 | |
|          return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|       rc = ( *net->initUndo )( net->socket4net ) ;
 | |
|       net->initUndone = 1 ;
 | |
|    }
 | |
| 
 | |
|    return rc ;
 | |
| }
 | |
| 
 | |
| SOCKET4NET *S4FUNCTION socket4netAlloc( SOCKET4 *socket )
 | |
| {
 | |
|    SOCKET4NET *net ;
 | |
|    HINSTANCE hInst ;
 | |
|    int rc = 0 ;
 | |
| 
 | |
|    #ifdef E4PARM_LOW
 | |
|       if ( socket == 0 )
 | |
|       {
 | |
|          error4( 0, e4parm_null, E90201 ) ;
 | |
|          return 0 ;
 | |
|       }
 | |
|    #endif
 | |
| 
 | |
|    net = (SOCKET4NET *)u4allocFree( socket->cb, sizeof( SOCKET4NET ) ) ;
 | |
|    if( net == 0 )
 | |
|       rc = e4memory;
 | |
|    else
 | |
|    {
 | |
|       net->c4 = socket->cb ;
 | |
| 
 | |
|       if( net->c4->hInstLocal )
 | |
|          hInst = net->c4->hInstLocal ;
 | |
|       else
 | |
|          hInst = LoadLibrary( net->c4->protocol ) ;
 | |
| 
 | |
|       #ifdef S4WIN32
 | |
|          if( hInst == NULL )
 | |
|       #else
 | |
|          if( hInst < HINSTANCE_ERROR )
 | |
|       #endif
 | |
|          rc = e4loadlib ;
 | |
|       else
 | |
|       {
 | |
|          #ifdef S4WIN32
 | |
|             c4getFuncPtr( hInst, "socket4netAlloc", (void **)&net->alloc ) ;
 | |
|          #else
 | |
|             c4getFuncPtr( hInst, "SOCKET4NETALLOC", (void **)&net->alloc ) ;
 | |
|          #endif
 | |
| 
 | |
|          if( net->alloc == 0 )
 | |
|             rc = e4loadlib ;
 | |
|          else
 | |
|          {
 | |
|             if( ( net->socket4net = (*net->alloc)((LPSTR)socket) ) == 0 )
 | |
|                rc = e4memory ;
 | |
|             else
 | |
|             {
 | |
|                #ifdef S4WIN32
 | |
| 
 | |
|                c4getFuncPtr( hInst, "socket4netFree", (void **)(&net->free) ) ;
 | |
|                c4getFuncPtr( hInst, "socket4netInitUndo", (void **)(&net->initUndo) ) ;
 | |
|                c4getFuncPtr( hInst, "socket4netReceive", (void **)(&net->receive) ) ;
 | |
|                c4getFuncPtr( hInst, "socket4netYield", (void **)(&net->yield) ) ;
 | |
|                #ifdef S4SERVER
 | |
|                   c4getFuncPtr( hInst, "socket4netShutdownAdvertising", (void **)(&net->shutdownAdvertising) ) ;
 | |
|                   c4getFuncPtr( hInst, "socket4netConnected", (void **)(&net->connected) ) ;
 | |
|                   c4getFuncPtr( hInst, "socket4netConnectClient", (void **)(&net->connectClient) ) ;
 | |
|                   c4getFuncPtr( hInst, "socket4netInitServer", (void **)(&net->initServer) ) ;
 | |
|                #else
 | |
|                   c4getFuncPtr( hInst, "socket4netConnectServer", (void **)(&net->connectServer) ) ;
 | |
|                #endif
 | |
| 
 | |
|                #else  /* Not S4WIN32 */
 | |
| 
 | |
|                c4getFuncPtr( hInst, "SOCKET4NETFREE", (void **)(&net->free) ) ;
 | |
|                c4getFuncPtr( hInst, "SOCKET4NETINITUNDO", (void **)(&net->initUndo) ) ;
 | |
|                c4getFuncPtr( hInst, "SOCKET4NETRECEIVE", (void **)(&net->receive) ) ;
 | |
|                c4getFuncPtr( hInst, "SOCKET4NETYIELD", (void **)(&net->yield) ) ;
 | |
|                #ifdef S4SERVER
 | |
|                   c4getFuncPtr( hInst, "SOCKET4NETSHUTDOWNADVERTISING", (void **)(&net->shutdownAdvertising) ) ;
 | |
|                   c4getFuncPtr( hInst, "SOCKET4NETCONNECTED", (void **)(&net->connected) ) ;
 | |
|                   c4getFuncPtr( hInst, "SOCKET4NETCONNECTCLIENT", (void **)(&net->connectClient) ) ;
 | |
|                   c4getFuncPtr( hInst, "SOCKET4NETINITSERVER", (void **)(&net->initServer) ) ;
 | |
|                #else
 | |
|                   c4getFuncPtr( hInst, "SOCKET4NETCONNECTSERVER", (void **)(&net->connectServer) ) ;
 | |
|                #endif
 | |
| 
 | |
|                #endif   /* S4WIN32 */
 | |
| 
 | |
|                if ( net->free == 0 || net->initUndo == 0 ||
 | |
|                     net->receive == 0 || net->yield == 0 ||
 | |
|                     #ifdef S4SERVER
 | |
|                        net->connectClient == 0 || net->initServer == 0 || net->connected == 0 ||
 | |
|                        net->shutdownAdvertising == 0
 | |
|                     #else
 | |
|                        net->connectServer == 0
 | |
|                     #endif
 | |
|                     )
 | |
|                   rc = e4loadlib ;
 | |
|                else
 | |
|                {
 | |
|                   net->hInst = hInst ;
 | |
|                   net->c4->hInstLocal = hInst ;
 | |
|                }
 | |
|             }
 | |
|          }
 | |
|       }
 | |
|    }
 | |
| 
 | |
|    if ( rc < 0 )
 | |
|    {
 | |
|       if ( net != 0 )
 | |
|          if ( net->free != 0 )   /* make sure shutdown is done properly */
 | |
|             rc = (*net->free)(net->socket4net) ;
 | |
|       FreeLibrary( hInst ) ;
 | |
|       net->c4->hInstLocal = 0 ;
 | |
|       if ( net != 0 )
 | |
|       {
 | |
|          u4free( net ) ;
 | |
|          net = 0 ;
 | |
|       }
 | |
|       error4( socket->cb, rc, E90201 ) ;
 | |
|       return 0 ;
 | |
|    }
 | |
|    return net ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION socket4netFree( SOCKET4NET *net )
 | |
| {
 | |
|    int rc ;
 | |
| 
 | |
|    if ( net->free == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
|    rc = (*net->free)(net->socket4net) ;
 | |
| 
 | |
|    FreeLibrary( net->hInst ) ;
 | |
|    net->c4->hInstLocal = 0 ;
 | |
| 
 | |
|    u4free( net ) ;
 | |
| 
 | |
|    return rc ;
 | |
| }
 | |
| 
 | |
| #ifdef S4SERVER
 | |
| int S4FUNCTION socket4netConnected( SOCKET4NET *net )
 | |
| {
 | |
|    if ( net->connected == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90201 ) ;
 | |
| 
 | |
|    return ( *net->connected )( net->socket4net ) ;
 | |
| }
 | |
| 
 | |
| int S4FUNCTION socket4netShutdownAdvertising( SOCKET4NET *net )
 | |
| {
 | |
|    if ( net->shutdownAdvertising == 0 )
 | |
|       return error4( net->c4, e4loadlib, E90208 ) ;
 | |
| 
 | |
|    return ( *net->shutdownAdvertising )( net->socket4net ) ;
 | |
| }
 | |
| #endif
 | |
| 
 | |
| #endif /* S4LOCAL */
 | |
| #endif /* S4OFF_COMMUNICATIONS */
 |