码迷,mamicode.com
首页 > 其他好文 > 详细

企业信息系统集成框架(设计思路)C模式

时间:2016-08-06 00:32:23      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

1.客户端.h文件

 1 #pragma once
 2 #include<stdlib.h>
 3 //A程序员定义接口形式和调用模式
 4 
 5 
 6 //初始化句柄
 7 typedef int(*Init_Socket)(void **handle);
 8 
 9 //发送数据,传入句柄中
10 typedef int(*Send_Socket)(void *handle, unsigned char *sendbuf, int sendLen);
11 
12 //从句柄中输出数据给内存
13 typedef int(*Recv_Socket)(void *handle, unsigned char *recvBuf, int *recvLen);
14 
15 //销毁句柄
16 typedef int(*Destroy_Socket)(void **handle);
17 
18 
19 //将函数指针组成一个结构体,用于联系控制厂商的软件要求
20 typedef struct _CSocketProtocol
21 {
22     
23     Init_Socket Init;
24     Send_Socket Send;
25     Recv_Socket Recv;
26     Destroy_Socket Destroy;
27 
28 }CSocketProtocol;
29 
30 
31 //中间控制层:将实现的函数与句柄中的变量关联在一起
32 int Init_CSocketProtocol(CSocketProtocol *csp,
33     Init_Socket Init,
34     Send_Socket Send,
35     Recv_Socket Recv,
36     Destroy_Socket Destroy
37     );

2.厂商的产品实现(包含两段代码1. 产品.h文件 2.产品代码实现)

 1 #pragma once
 2 #include<stdlib.h>
 3 //厂商实现各接口函数。
 4 
 5 typedef struct _HD
 6 {
 7     unsigned char *data;
 8     int dataLen;
 9 }HD;
10 
11 int Init_Socket1(void **handle);
12 int Send_Socket1(void *handle, unsigned char *sendbuf, int sendLen);
13 int Recv_Socket1(void *handle, unsigned char *recvBuf, int *recvLen);
14 int Destroy_Socket1(void **handle);
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include"CScklmp1.h"
 3 #include<string.h>
 4 
 5 //实现初始化句柄。
 6 int Init_Socket1(void **handle)
 7 {
 8     HD *hd = malloc(sizeof(HD));
 9     if (NULL == handle)
10         return -1;
11     if (NULL == hd)
12         return -2;
13     hd->data = NULL;
14     hd->dataLen = 0;
15     *handle = hd;
16 
17     return 0;
18 }
19 
20 //发送数据到句柄
21 int Send_Socket1 (void *handle, unsigned char *sendbuf, int sendLen)
22 {
23     HD *hd = (HD *)handle;
24     if (NULL == handle)
25         return -1;
26     if (NULL == sendbuf)
27         return -2;
28     
29     hd->data = malloc(sendLen+1);
30     if (hd->data == NULL)
31         return -3;
32     strcpy(hd->data, sendbuf);
33     hd->dataLen = sendLen;
34 
35     return 0;
36 }
37 
38 //有句柄中提取数据
39 int Recv_Socket1(void *handle, unsigned char *recvBuf, int *recvLen)
40 {
41     HD *hd = (HD *)handle;
42     if (NULL == handle)
43         return -1;
44     if (NULL == recvBuf)
45         return -2;
46     if (NULL == recvLen)
47         return -3;
48 
49     strcpy(recvBuf, hd->data);
50     *recvLen = hd->dataLen;
51 
52     return 0;
53 }
54 
55 //销毁句柄
56 int Destroy_Socket1(void **handle)
57 {
58     HD *hd = (HD *)(*handle);
59 
60     if (NULL == handle)
61         return -1;
62     
63     if (hd->data != NULL)
64     {
65         free(hd->data);
66         hd->data = NULL;
67     }
68     free(hd);
69     hd = NULL;
70 
71     return 0;
72 }

3.客户端加解密(.h文件和代码实现)

 1 #pragma once
 2 #include<stdlib.h>
 3 //加密数据
 4 typedef int(*DesEnc)(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen);
 5 //解密数据
 6 typedef int(*DesDec)(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen);
 7 
 8 typedef struct _Des
 9 {
10     //加密数据
11     DesEnc EC;
12 
13     //解密数据
14     DesDec DC;
15     
16 }Des;
17 
18 
19 int Init_DecEnc(Des *de, DesDec DC, DesEnc EC);
 1 #include "EncDec.h"
 2 
 3 int Init_DecEnc(Des *de,  DesEnc EC, DesDec DC)
 4 {
 5     if (de == NULL)
 6         return -1;
 7     if (DC == NULL)
 8         return -2;
 9     if (EC == NULL)
10         return -3;
11 
12     de->DC = DC;
13     de->EC = EC;
14 
15     return 0;
16 }

 

4.厂商的加解密文件(包含两段代码:1.头文件2.代码实现)

1 #pragma once
2 //加密数据
3 int DesEncHw(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen);
4 //解密数据
5 int DesDecHw(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen);
 1 #include "CHwImp.h"
 2 #include"des.h"
 3 //加密数据
 4 int DesEncHw(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen)
 5 {
 6     DesEnc(inDataBuf, inDataLen, outDataBuf, outDataLen);
 7     return 0;
 8 }
 9 
10 //解密数据
11 int DesDecHw(unsigned char *inDataBuf, int inDataLen, unsigned char *outDataBuf, int *outDataLen)
12 {
13     DesDec(inDataBuf, inDataLen, outDataBuf, outDataLen);
14     return 0;
15 }

5.中间层代码实现

 1 #include "CSocketProtocol.h"
 2 
 3 //A程序员的调用中间层
 4 
 5 int Init_CSocketProtocol(CSocketProtocol *csp,
 6     Init_Socket Init,
 7     Send_Socket Send,
 8     Recv_Socket Recv,
 9     Destroy_Socket Destroy
10     )
11 {
12     if (NULL == csp)
13         return -1;
14     if (NULL == Init)
15         return -2;
16     if (NULL == Send)
17         return -3;
18     if (NULL == Recv)
19         return -4;
20     if (NULL == Destroy)
21         return -5;
22 
23     csp->Init = Init;
24     csp->Send = Send;
25     csp->Recv = Recv;
26     csp->Destroy = Destroy;
27 
28     return 0;
29 }

6.测试端界面

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<string.h>
  5 
  6 #include"CSocketProtocol.h"
  7 #include "CHwImp.h"
  8 #include "CScklmp1.h"
  9 #include "EncDec.h"
 10 
这段条件编译的代码没有引入加密内容。 11 #if 0 12 int FrameWork(CSocketProtocol *csp, unsigned char *sendBuf, int sendLen, unsigned char *recvBuf, int *recvLen) 13 { 14 if (NULL == csp) 15 return -1; 16 if (NULL == sendBuf) 17 return -2; 18 if (NULL == recvBuf) 19 return -3; 20 if (NULL == recvLen) 21 return -4; 22 23 void *handle = NULL; 24 csp->Init(&handle); 25 26 csp->Send(handle, sendBuf, sendLen); 27 28 csp->Recv(handle, recvBuf, recvLen); 29 30 csp->Destroy(&handle); 31 32 return 0; 33 } 34 35 36 void TestFrameWork01() 37 { 38 39 //创建结构体指针, 40 CSocketProtocol csp; 41 42 //给句柄初始化,使接口与实现函数建立关联 43 Init_CSocketProtocol(&csp, Init_Socket1, Send_Socket1, Recv_Socket1, Destroy_Socket1); 44 //要发送的数据和长度 45 unsigned char *sendBuf = "Good Night!!!"; 46 int sendLen = strlen(sendBuf) + 1; 47 48 //要接收数据的内存和长度 49 unsigned char recvBuf[1024] = { 0 }; 50 int recvLen = 0; 51 52 FrameWork(&csp, sendBuf, sendLen, recvBuf, &recvLen); 53 54 printf("接收的数据是:%s\n", recvBuf); 55 printf("接收的数据长度是:%d\n", recvLen); 56 57 } 58 #endif 59 60 int FrameWorkPro(CSocketProtocol *csp, Des *de, unsigned char *sendBuf, int sendLen, unsigned char *recvBuf, int *recvLen) 61 { 62 if (NULL == csp) 63 return -1; 64 if (NULL == sendBuf) 65 return -2; 66 if (NULL == recvBuf) 67 return -3; 68 if (NULL == recvLen) 69 return -4; 70 if (NULL == de) 71 return -5; 72 73 void *handle = NULL; 74 //初始化句柄 75 csp->Init(&handle); 76 77 //创建加密内存(接收加密后的文件),加密长度(保存加密文件的长度) 78 unsigned char DesEncHwBuf[1024] = { 0 }; 79 int DesEncHwLen = 0; 80 //将发送内容先传入加密工具,进行处理 81 de->EC(sendBuf, sendLen, DesEncHwBuf, &DesEncHwLen); 82 csp->Send(handle, DesEncHwBuf, DesEncHwLen); 83 84 85 //创建解密内存(先将句柄中的内容解密,放入这个内存),解密长度(保存解密的文件长度) 86 unsigned char DesDecHwBuf[1024] = { 0 }; 87 int DesDecHwLen = 0; 88 89 csp->Recv(handle, DesDecHwBuf, &DesDecHwLen); 90 91 de->DC(DesDecHwBuf, DesDecHwLen, recvBuf, recvLen); 92 93 csp->Destroy(&handle); 94 95 return 0; 96 } 97 98 99 void TestFrameWork02() 100 { 101 102 //创建结构体, 103 CSocketProtocol csp; 104 Des de; 105 //给句柄初始化,使接口与实现函数建立关联 106 Init_CSocketProtocol(&csp, Init_Socket1, Send_Socket1, Recv_Socket1, Destroy_Socket1); 107 Init_DecEnc(&de, DesEncHw, DesDecHw); 108 109 //要发送的数据和长度 110 unsigned char *sendBuf = "Good Night!!!"; 111 int sendLen = strlen(sendBuf) + 1; 112 113 //要接收数据的内存和长度 114 unsigned char recvBuf[1024] = { 0 }; 115 int recvLen = 0; 116 117 118 FrameWorkPro(&csp, &de, sendBuf, sendLen, recvBuf, &recvLen); 119 120 printf("接收的数据是:%s\n", recvBuf); 121 printf("接收的数据长度是:%d\n", recvLen); 122 123 } 124 125 126 int main(void) 127 { 128 129 TestFrameWork02(); 130 131 printf("\n"); 132 system("pause"); 133 return 0; 134 }

 

企业信息系统集成框架(设计思路)C模式

标签:

原文地址:http://www.cnblogs.com/yyx1-1/p/5743016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!