C + + Wisdom Note (6): Socket client API interface for sending message acceptance messages

Source: Internet
Author: User

#define _crt_secure_no_warnings#include "stdio.h" #include "stdlib.h" #include "string.h" #include "itcast_comm.h" # Include "Memwatch.h" #include "itcastlog.h"/* The following defines a set of API interfaces for the socket client to send packet acceptance messages write out the calling method of the interface API *//*1 first the basic framework for version Management 2 Memory Leak Check 3rd Log 4 version optimization *///develop the underlying library (development interface), providing a mechanism rather than a specific strategy. Dynamic library becomes framework//1 third party business portal comes in//2 cryptographic decryption business model abstraction//implement dynamic library encryption decrypt business model abstract typedef INT (*encdata) (unsigned char *indata,int indatalen,unsi gned Char *outdata,int *outdatalen,void *ref, int reflen); typedef int (*decdata) (unsigned char *indata,int indatalen,unsi gned Char *outdata,int *outdatalen,void *ref, int reflen); typedef struct_sck_handle{charversion[16];charserverip[16];intserverport;unsigned char *buf; int buflen; Encdata myencdatafunc;void *encref;int encreflen;decdata mydecdatafunc;void *decref;int DecRefLen;}Sck_handle; The abstract type is _sck_handle, and the object instance is Sck_handleClient initialization itcast_func_export (int) cltsocketinit (void **handle)//5day after{Sck_handle    *sh = NULL; int rv = 0; Itcast_log (__file__, __line__,loglevel[1], RV, "func cltsocketinit () Begin");if (handle = = NULL){RV =-1;Itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketinit (): check handle = = NULL [%d]", RV);return RV;}//allocating memory and initializing SH =(Sck_handle *) malloc (sizeof (Sck_handle));if (sh = = NULL) {RV = -2;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketinit (): malloc err [%d]", RV); return RV;}memset (SH, 0, sizeof (sck_handle));Domain Assignment strcpy (sh->version, "0.0.1"); strcpy (Sh->serverip, "192.168.0.211"); Sh->serverport = 8888;//Outgoing*handle = sh;Itcast_log (__file__, __line__,loglevel[1], RV, "func cltsocketinit () End");return RV;}//Client Sender itcast_func_export (int)Cltsocketsend(void *handle, unsigned char *buf, int buflen) {INTRV = 0; Sck_handle *sh = null;if (HANDLE = = NULL) {RV = -4;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketsend (): Ch Eck handle = = NULL [%d] ", RV); return RV;}if (buf = = NULL | | buflen<=0){RV = -5;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketsend (): (buf = = NULL | | len<=0) [%d]", RV); return RV;} sh = (Sck_handle *) HANDLE; sh->buf = (unsigned char *)malloc (sizeof (char) *buflen + +);if (sh->buf = = NULL) {RV = -6;itcast_log (__file__, __line__,loglevel[4], RV, "func cltsocketsend () buflen:%d)", Buflen) ; Itcast_log (__file__, __line__,loglevel[4], RV, func cltsocketsend () check (handle->buf = NULL)) [%d] ", RV); return RV;} if (sh->myencdatafunc! = NULL) {//Return to call the third-party vendor's cryptographic Business RV =Sh->myencdatafunc (buf, Buflen, Sh->buf, &sh->buflen, Sh->encref, Sh->encreflen);if (rv! = 0) {itcast_log (__file__, __line__,loglevel[4], RV, "Sh->myencdatafunc (buf, Buflen, Sh->buf, &sh-> Buflen):%d) ", Buflen); return rv;}} else{//sends the message data stored in the handle context memcpy (Sh->buf, buf, buflen); sh->buflen = Buflen;} return RV;} Second notation for callback functions//client-side itcast_func_export (int) cltsocketsend_enc (void *handle, unsigned char *buf, int buflen, Encdata Encdatacallbakfunc, void *ref, int reflen) {INTRV = 0; Sck_handle*sh = null;if (HANDLE = = NULL) {RV = -4;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketsend (): Che CK handle = = NULL [%d] ", RV); return RV;}  if (buf = = NULL | | buflen<=0) {RV = -5;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltsocketsend (): (BUF = = NULL || len<=0) [%d] ", RV); return RV;} SH = (Sck_handle *) Handle;sh->buf = (unsigned char *) malloc (sizeof (char) *buflen + N); if (sh->buf = = NULL) {RV =-6; Itcast_log (__file__, __line__,loglevel[4], RV, "func cltsocketsend () buflen:%d)", Buflen); Itcast_log (__FILE__, __LINE __,loglevel[4], RV, "funC Cltsocketsend () check (Handle->buf = = NULL)) [%d] ", RV); return RV; if (encdatacallbakfunc! = NULL) {//Return to call the third-party vendor's cryptographic Business RV = Encdatacallbakfunc (buf, Buflen, Sh->buf, &sh->buflen, Ref, Reflen); if (rv! = 0) {itcast_log (__file__, __line__,loglevel[4], RV, Encdatacallbak (buf, Buflen, Sh->buf, & Sh->buflen):%d) ", Buflen); return rv;}} else{//sends the message data stored in the handle context memcpy (Sh->buf, buf, buflen); sh->buflen = Buflen;} return RV;} Client ticker Itcast_func_export (int)Cltsocketrev (void *handle, unsigned char *buf, int *buflen){INTRV = 0; Sck_handle*sh = NULL;if (handle = = NULL){RV = -4;itcast_log (__file__, __line__,loglevel[4], RV, "func cltsocketrev:check handle = = NULL [%d]", RV); return RV;}if (Buflen = = NULL){RV = -5;itcast_log (__file__, __line__,loglevel[4], RV, "func cltsocketrev: (Buflen = = NULL) [%d]", RV); return RV;}sh = (Sck_handle *) HANDLE;Assignment to the context of the data, copy into the BUF space//support two calls, the first call to find the length of the second call can be the data copy buf in the IF (buf! = NULL) {memcpy (buf, Sh->buf, Sh->buflen) ;//buf[ci->buflen] = ' n ';} *buflen = Sh->buflen;return RV;} Client frees resource itcast_func_export (int)cltsocketdestory (void *handle){ Sck_handle *sh = Null;sh = HANDLE; if (sh = null) {free (SH-&GT;BUF); if (sh->encref! = null) {free (sh->encref);} if (sh->decref! = NULL) {free (sh->decref);} Free (SH);} return 0;} The second set of APIs implements///////////////////////////////////////////////////client initialization itcast_func_export (int) CltSocketInit2 (void **handle)//5day after{sck_handle*sh = NULL;INTRV = 0;itcast_log (__file__, __line__,loglevel[1], RV , "Func cltSocketInit2 () Begin"), if (handle = = NULL) {RV = -1;itcast_log (__file__, __line__,loglevel[4], RV, "func CltSocketInit2 (): check handle = = NULL [%d] ", RV); return RV;} Allocates memory and initializes sh = (Sck_handle *) malloc (sizeof (sck_handle)); if (sh = = NULL) {RV = -2;itcast_log (__file__, __line__,loglevel[ 4], RV, "Func cltSocketInit2 (): malloc err [%d]", RV); return RV; memset (SH, 0, sizeof (sck_handle));//Field Assignment strcpy (sh->version, "0.0.1"); strcpy (Sh->serverip, "192.168.0.211"); Sh->serverport = 8888;//Outgoing *handle = Sh;itcast_log (__file__, __line__,loglevel[1], RV, "func cltSocketInit2 () End"); return RV;} Client sender itcast_func_export (int)CltSocketSend2 (void *handle, unsigned char *buf, int buflen) {INTRV = 0; Sck_handle*sh = null;if (HANDLE = = NULL) {RV = -4;itcast_log (__file__, __line__,loglevel[4], RV, "Func cltSocketSend2 (): Ch Eck handle = = NULL [%d] ", RV); return RV;} if (buf = = NULL | | buflen<=0) {RV = -5;itcast_log (__file__, __line__,loglevel[4], RV, "Func CltSocketSend2 (): (buf = = NUL L | | len<=0) [%d] ", RV); return RV;} SH = (Sck_handle *) Handle;sh->buf = (unsigned char *) malloc (sizeof (char) *buflen), if (sh->buf = = NULL) {RV = -6;itcast _log (__file__, __line__,loglevel[4], RV, "func cltSocketSend2 () buflen:%d)", Buflen), Itcast_log (__file__, __line__, Loglevel[4], RV, "func cltSocketSend2 () check (Handle->buf = = NULL)) [%d]", RV); return RV; Send the message data stored in the handle context memcpy (Sh->buf, buf, buflen); sh->buflen = Buflen;return rv;} Client ticker itcast_func_export (int) cltSocketRev2 (void *handle, unsigned char **buf, int *buflen) {INTRV = 0; Sck_handle*sh = null;if (HANDLE = = NULL) {RV = -4;itcast_log (__file__, __line__,logLevel[4], RV, "func cltsocketrev2:check handle = = NULL [%d]", RV); return RV;} if (Buflen = = null) {RV = -5;itcast_log (__file__, __line__,loglevel[4], RV, "func cltSocketRev2: (Buflen = = null) [%d]", RV) ; return RV;} SH = (Sck_handle *) handle;//allocates memory data outgoing *BUF = (char *) malloc (Sh->buflen); if (*buf = = NULL) {RV = -6;itcast_log (__file__, __ Line__,loglevel[4], RV, "func cltSocketRev2: (Buflen = = NULL) [%d]", RV); return RV;} memcpy (*buf, Sh->buf, sh->buflen); *buflen = Sh->buflen;return rv;}  Itcast_func_export (int) cltsocketrev2_free (unsigned char **buf) {int RV = 0;unsigned char * tmp = *buf;if (buf = = NULL) {RV = -7;itcast_log (__file__, __line__,loglevel[4], RV, "func cltSocketRev2: (Buflen = = NULL) [%d]", RV); return RV;} if (tmp! = NULL) {free (TMP);} *buf = NULL;} Client frees resource itcast_func_export (int) cltSocketDestory2 (void **handle) {Sck_handle *sh = Null;sh = *handle; if (sh! = NULL) { Free (SH-&GT;BUF), free (SH);} *handle = Null;return 0;} Implementation of the upper application encryption interface entry address into the dynamic library inside __declspec (dllexport) intclitsocket_sEtencfunc (void *handle, Encdata encdatacallbak, void *ref, int reflen) {Sck_handle *sh = Null;sh = handle; if (handle = = NU LL) {return-1;} Sh->myencdatafunc = encdatacallbak;if (Reflen > 0) {sh->encref = (void *) malloc (Reflen); if (sh->encref = = NULL ) {return-2;} memcpy (Sh->encref, ref, Reflen); sh->encreflen = Reflen;}  return 0;} __declspec (dllexport) intclitsocket_setdecfunc (void *handle, Encdata decdatacallbak, void *ref, int reflen) {}

  

C + + Wisdom Note (6): Socket client API interface for sending message acceptance messages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.