L_InetSetCommandCallback

#include "l_bitmap.h"
#include "ltnet.h"

L_LTNET_API L_INT L_InetSetCommandCallback(hComputer, pfnCallback, pUserData)

L_COMP hComputer;

/* computer to associate the callback data with */

INETCOMMANDCALLBACK pfnCallback;

/* callback to associate */

L_VOID * pUserData;

/* user data to associate */

Sets the command callback function to use for incoming command requests.

Parameter Description

hComputer

Handle of the host computer to be associated with the callback.

pfnCallback

Pointer to an optional callback. You can pass NULL if you want the default INETCALLBACK to be called when a command request is received.

pUserData

Void pointer that can be used to access a variable or structure containing data that the callback function needs. This gives you a way to receive data indirectly from the function that uses this callback.

 

Keep in mind that this is a void pointer, which must be cast to the appropriate data type within the callback function.

Returns

SUCCESS

This function was successful.

< 1

An occurred. Refer to Return Codes.

Comments

This function sets a specialized callback to handle the incoming command requests. By default, the INETCALLBACK handles the command requests, but INETCALLBACK is not suitable for handling the command parameters and extra data. Use an INETCOMMANDCALLBACK function to process the command requests properly.

Required DLLs and Libraries

LTNET

For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application

See Also

Functions:

INETCOMMANDCALLBACK, L_InetSetResponseCallback, INETRESPONSECALLBACK

Topics:

Sending Commands and Responses

 

A Client-Server Diagram: Sending Commands and Responses

Example

This example will create a server and set the command and response callback.

/*<struct>*/
#if !defined(COMMANDSTRUCT)
typedef struct _SETCOMMANDSTRUCT
{
   L_COMP ghServer;
   HBITMAPLIST ghBitmapList;
   // Bitmap loaded on the remote computer using L_InetSendCmdLoad
   L_UINT guBitmapID; // the bitmap ID which is loaded on the remote computer
   L_COMP ghComputer; // the computer where the bitmap is loaded
   L_UINT guWindowID; // the id of the window that is created on the remote computer
}SETCOMMANDSTRUCT, * LPSETCOMMANDSTRUCT;
#endif
/*</struct>*/
// This can be either the local or the remote computer
//**********  Code on the remote computer ****************/
L_INT RemoveBitmap(L_UINT uBitmapID,LPSETCOMMANDSTRUCT pSetCommandData);
L_INT GetLocalBitmap(pBITMAPHANDLE pBitmap, L_UINT uBitmapID,LPSETCOMMANDSTRUCT pSetCommandData);
L_INT AddBitmap(pBITMAPHANDLE pBitmap, L_UINT * puBitmapID,LPSETCOMMANDSTRUCT pSetCommandData);
static L_INT EXT_CALLBACK InetCallback(L_COMP   hComp,
                                       L_INT    nMessage,
                                       L_INT    nError,
                                       L_CHAR * pBuffer,
                                       L_UINT32 lSize,
                                       L_VOID * pUserData)
{
   UNREFERENCED_PARAMETER(pBuffer);
   UNREFERENCED_PARAMETER(lSize);
   UNREFERENCED_PARAMETER(pUserData);
   
   L_TCHAR s[100]; /* set up a buffer to retrieve the name of the server */
   L_SIZE_T sizeof_s = sizeof(s); /* the size of s */
   switch(nMessage)
   {
      case INET_CONNECT:
      /* check the status and try to get server's name */
      if(nError == SUCCESS)
      {
         /* now you can assume you are connected to computer 'hComp' */
         if(L_InetGetHostName(hComp, (L_CHAR*) s, HOST_NAME_IP, &sizeof_s) == SUCCESS)
         {
            MessageBox(NULL, TEXT("You have successfully connected to"), s, MB_OK);
         }
         /* do whatever you want to do with the server here ... */
         /* remove the next line if you want to keep the connection open */
         L_InetClose(hComp, TRUE);
      }
      else
         MessageBox(NULL, TEXT("You have failed to connect!"), TEXT("Error"), MB_OK);
      break;
   }
   return SUCCESS;
}
static L_INT EXT_CALLBACK InetCommandCallback(L_COMP     hComputer,
                                              CMDTYPE    uCommand,
                                              L_UINT     uCommandID,
                                              L_INT      nError,
                                              L_UINT     uParams,
                                              pPARAMETER pParams,
                                              L_UINT     uExtra,
                                              L_CHAR   * pExtra,
                                              L_VOID   * pUserData)
{
   UNREFERENCED_PARAMETER(uExtra);
   UNREFERENCED_PARAMETER(pExtra);
   UNREFERENCED_PARAMETER(pUserData);
   LPSETCOMMANDSTRUCT pSetCommandData = (LPSETCOMMANDSTRUCT)pUserData; 
   L_INT  nStatus = ERROR_FEATURE_NOT_SUPPORTED;
   BITMAPHANDLE Bitmap;
   L_UINT uBitmapID = (L_UINT)-1;
   COLORREF *pColor=NULL;
   L_UINT32 lColorSize=0;
   if(nError != SUCCESS)
      nStatus = ERROR_TRANSFER_ABORTED;
   else
   switch(uCommand)
   {
      case CMD_LOAD:
      // check the validity of the parameters
      if(uParams == 4 && pParams[0].uType == PARAM_STRING && pParams[1].uType == PARAM_INT32 && pParams[2].uType == PARAM_INT32 && pParams[3].uType == PARAM_UINT32)
      {
         nStatus = L_LoadFile((L_TCHAR*)pParams[0].pValue, &Bitmap,sizeof(Bitmap), pParams[1].iValue, pParams[2].iValue, pParams[3].iValue, NULL, NULL, NULL, NULL);
         if(nStatus == SUCCESS)
         {
            // add the bitmap to the list of currently open bitmaps
            nStatus = AddBitmap(&Bitmap, &uBitmapID,pSetCommandData);
         }
      }
      else
         nStatus = ERROR_INV_PARAMETER; // invalid parameters
         return L_InetSendLoadRsp(hComputer, uCommandID, uBitmapID, 0, NULL, nStatus);
      case CMD_SAVE:
         if(uParams == 6 && pParams[0].uType == PARAM_STRING && pParams[1].uType == PARAM_UINT32 && pParams[2].uType == PARAM_INT32 && pParams[3].uType == PARAM_INT32 && pParams[4].uType == PARAM_INT32 && pParams[5].uType == PARAM_UINT32)
         {
            uBitmapID = pParams[1].uiValue;
            nStatus = GetLocalBitmap(&Bitmap, uBitmapID,pSetCommandData);
            if(nStatus == SUCCESS)
               nStatus = L_SaveFile((L_TCHAR*)pParams[0].pValue, &Bitmap, pParams[2].iValue, pParams[3].iValue, pParams[4].iValue, pParams[5].uiValue, NULL, NULL, NULL);
         }
         else
            nStatus = ERROR_INV_PARAMETER;
         return L_InetSendSaveRsp(hComputer, uCommandID, 0, NULL, nStatus); 
      case CMD_FREE_BITMAP:
         if(uParams == 1 && pParams[0].uType == PARAM_UINT32)
            nStatus = RemoveBitmap(pParams[0].uiValue,pSetCommandData);
         else
            nStatus = ERROR_INV_PARAMETER;
         return L_InetSendFreeBitmapRsp(hComputer, uCommandID, 0, NULL, nStatus);
      case CMD_GET_MAGGLASS_DATA:
         if(uParams == 5 && pParams[0].uType == PARAM_UINT32 && pParams[1].uType == PARAM_UINT32 && pParams[2].uType == PARAM_USTRING && pParams[3].uType == PARAM_INT16 && pParams[4].uType == PARAM_INT16)
         {
            uBitmapID = pParams[0].uiValue;
            nStatus = GetLocalBitmap(&Bitmap, uBitmapID,pSetCommandData);
            if(nStatus == SUCCESS)
            {
               nStatus = L_InetGetMagGlassData(&Bitmap, &lColorSize, NULL, pParams[2].puValue, pParams[3].sValue, pParams[4].sValue);
               if(nStatus == SUCCESS)
               {
                  pColor = (COLORREF*) GlobalAllocPtr(GHND, lColorSize);
                  nStatus = L_InetGetMagGlassData(&Bitmap, &lColorSize, pColor, pParams[2].puValue, pParams[3].sValue, pParams[4].sValue);
            }
         }
      }
      else
         nStatus = ERROR_INV_PARAMETER;
         L_InetSendGetMagGlassDataRsp (hComputer, uCommandID, lColorSize, pColor, pParams[1].uiValue, pParams[2].puValue, pParams[3].sValue, pParams[4].sValue, 0, NULL, nStatus);
         if(pColor)
         {
            GlobalFreePtr(pColor);
            pColor=NULL;
         }
      break;
   }
   return L_InetSendRsp(hComputer, uCommand, uCommandID, NULL, nStatus);
}
//**********  Code on the local computer ****************/
L_VOID EXT_CALLBACK InetResponseCallback(L_COMP     hComputer,
                                         CMDTYPE    uCommand,
                                         L_UINT     uCommandID,
                                         L_INT      nError,
                                         L_INT      nStatus,
                                         L_UINT     uParams,
                                         pPARAMETER pParams,
                                         L_UINT     uExtra,
                                         L_CHAR   * pExtra,
                                         L_VOID   * pUserData)
{
   UNREFERENCED_PARAMETER(uCommandID);
   UNREFERENCED_PARAMETER(uExtra);
   UNREFERENCED_PARAMETER(pExtra);
   UNREFERENCED_PARAMETER(pUserData);
   LPSETCOMMANDSTRUCT pSetCommandData = (LPSETCOMMANDSTRUCT)pUserData;
   if(nError != SUCCESS)
   {
      OutputDebugString(TEXT("An error has occurred processing the response!"));
      return;
   }
   switch(uCommand)
   {
      case CMD_LOAD:
         if(nStatus == SUCCESS)
         {
            if(uParams == 1 && pParams[0].uType == PARAM_UINT32)
            {
               pSetCommandData->ghComputer = hComputer;
               pSetCommandData->guBitmapID = pParams[0].uiValue;
               OutputDebugString(TEXT("Load SUCCEEDED!"));
            }
         }
         else
            OutputDebugString (TEXT("Load failed!"));
         break;
      case CMD_CREATE_WIN:
         if(nStatus == SUCCESS)
         {
            if(uParams == 1 && pParams[0].uType == PARAM_UINT32)
            {
               // the window has been created - store the window ID for later use
               pSetCommandData->ghComputer = hComputer;
               pSetCommandData->guWindowID = pParams[0].uiValue;
            }
            OutputDebugString(TEXT("Create window SUCCEEDED!"));
         }
         else
            OutputDebugString (TEXT("Create Window failed"));
         break;
      case CMD_FREE_BITMAP:
         if(nStatus == SUCCESS)
            OutputDebugString(TEXT("Free bitmap SUCCEEDED!"));
         else
            OutputDebugString (TEXT("Free bitmap failed"));
         break;
   }
}
L_INT InetSetCommandCallbackExample(LPSETCOMMANDSTRUCT pSetCommandData)
{
   L_INT  nRet;
   L_COMP ctemp = pSetCommandData->ghServer;
   
   nRet = L_InetServerInit(1000, &ctemp,(INETCALLBACK) InetCallback,(L_VOID *)pSetCommandData);
   if(nRet==SUCCESS)
   {
      nRet = L_InetSetCommandCallback(pSetCommandData->ghServer, (INETCOMMANDCALLBACK) InetCommandCallback, NULL);
      if(nRet != SUCCESS)
         return nRet;
      nRet = L_InetSetResponseCallback(pSetCommandData->ghServer, (INETRESPONSECALLBACK) InetResponseCallback, NULL);
      if(nRet != SUCCESS)
         return nRet;
   }
   else
      return nRet;
   return SUCCESS;
}
/* The local bitmaps are identified by the index in the bitmap list.*/
L_INT AddBitmap(pBITMAPHANDLE pBitmap,
                L_UINT      * puBitmapID,
                LPSETCOMMANDSTRUCT pSetCommandData)
{
   L_INT nRet;
   if(! pSetCommandData->ghBitmapList)
   {
      nRet = L_CreateBitmapList(&pSetCommandData-&gt;ghBitmapList);
      if(nRet != SUCCESS)
      {
         L_FreeBitmap(pBitmap);
         return nRet;
      }
   }
   nRet = L_InsertBitmapListItem(pSetCommandData->ghBitmapList, (L_UINT)-1, pBitmap);
   if(nRet == SUCCESS)
      L_GetBitmapListCount(pSetCommandData->ghBitmapList, puBitmapID);
   else
      // free the bitmap if an error occurs because the caller is not doing anything else with it
      L_FreeBitmap(pBitmap);
   return nRet;
}
L_INT GetLocalBitmap(pBITMAPHANDLE pBitmap,
                     L_UINT        uBitmapID,
                     LPSETCOMMANDSTRUCT pSetCommandData)
{
   if(pSetCommandData->ghBitmapList)
      return L_GetBitmapListItem(pSetCommandData->ghBitmapList, uBitmapID, pBitmap, sizeof(pBitmap));
   return ERROR_INV_PARAMETER;
}
L_INT RemoveBitmap(L_UINT uBitmapID,LPSETCOMMANDSTRUCT pSetCommandData)
{
   L_INT nRet = ERROR_INV_PARAMETER;
   if(pSetCommandData->ghBitmapList)
   {
      nRet = L_DeleteBitmapListItems(pSetCommandData->ghBitmapList, uBitmapID, 1);
      if(nRet == SUCCESS)
      {
         // Go to all the places that you might have used this bitmap and make the
         // necessary changes
      }
   }
   return nRet;
}