当前位置:网站首页>Interface oriented programming in C language
Interface oriented programming in C language
2022-06-13 06:33:00 【Road and distance】
C Interface oriented programming of language
Create an interface
- The header file
// hello_socket.h
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
typedef void (* initHandle)(void ** handle);
typedef void (* sendMessage)(void * handle,char * message,int length);
typedef void (* receiveMessage)(void * handle,char * message,int *length);
typedef void (* closeHandle)(void * handle);
void FrameWork(initHandle _init,sendMessage _send,receiveMessage _receive,closeHandle _close);
- C file
// hello_socket.c
#include "hello_socket.h"
void FrameWork(initHandle _init, sendMessage _send, receiveMessage _receive, closeHandle _close) {
// Initialize connection
void *handle = NULL;
_init(&handle);
// send data
char buff[] = "hello world";
int length = strlen(buff);
_send(handle,buff, length);
printf(" send data ---> %s\n length ---> %d\n",buff,length);
// receive data
char receiveBuff[1024] = {0};
int receiveLength = 0;
_receive(handle,receiveBuff, &receiveLength);
// Close the connection
_close(handle);
handle = NULL;
printf(" receive data ---> %s\n length ---> %d\n",receiveBuff,receiveLength);
}
Implementation interface
- The header file
// hello_socket_impl.h
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
void initHandleImpl(void ** handle);
void sendMessageImpl(void * handle,char * message,int length);
void receiveMessageImpl(void * handle,char * message,int *length);
void closeHandleImpl(void * handle);
- C file
// hello_socket_impl.c
#include "hello_socket_impl.h"
struct Info{
char data[1024];
int length;
};
void initHandleImpl(void ** handle){
struct Info *info = malloc(sizeof(struct Info));
memset(info,0, sizeof(struct Info));
*handle = info;
}
void sendMessageImpl(void * handle,char * message,int length){
if(handle == NULL || message == NULL || length <= 0) return;
struct Info *info =(struct Info *)handle;
strncpy(info->data,message,length);
info->length = length;
}
void receiveMessageImpl(void * handle,char * message,int *length){
if(handle == NULL || message == NULL || length == NULL) return;
struct Info *info =(struct Info *)handle;
strncpy(message,info->data,info->length);
*length = info->length;
}
void closeHandleImpl(void * handle){
if(handle == NULL)
return;
free(handle);
handle = NULL;
}
Test interface
#include "hello_socket.h"
#include "hello_socket_impl.h"
int main(){
FrameWork(initHandleImpl,sendMessageImpl,receiveMessageImpl,closeHandleImpl);
return 0;
}
边栏推荐
- Scrcpy source code walk 3 what happened between socket and screen refresh
- 【Kernel】驱动编译的两种方式:编译成模块、编译进内核(使用杂项设备驱动模板)
- Jetpack - basic use of room
- Usegeneratedkeys=true configuration
- ADB shell CMD overlay debugging command facilitates viewing system framework character resource values
- Huawei developer certification and deveco studio compiler Download
- Uni app provincial and urban linkage
- Notifyitemchanged flash back
- Wechat applet (get location)
- JVM基础
猜你喜欢
推荐扩容工具,彻底解决C盘及其它磁盘空间不够的难题
Dragon Boat Festival wellbeing, use blessing words to generate word cloud
【sketchup 2021】草图大师中CAD文件的导入与建模(利用cad图纸在草图大师中建立立面模型)、草图大师导出成品为dwg格式的二维、三维、立面效果到cad中打开预览】
SSM integration
Solutions to common problems in small program development
JetPack - - - Navigation
Kotlin data flow - flow
You should consider upgrading via
【2022高考季】作为一个过来人想说的话
JS to realize bidirectional data binding
随机推荐
Download and installation of universal player potplayer, live stream m3u8 import
【案例】一个超级好用的工具 —— 给程序员用的计算器
JetPack - - - DataBinding
[written examination questions of meituan]
The processing and application of C language to documents
Uni app disable native navigation bar
1+1 > 2, share creators can help you achieve
Kotlin collaboration - flow+room database
ADB shell CMD overlay debugging command facilitates viewing system framework character resource values
Uni app upload file
【2022高考季】作为一个过来人想说的话
JS convert text to language for playback
MFS详解(七)——MFS客户端与web监控安装配置
【js】var、let、const
Echart rectangular tree diagram: simple implementation of rectangular tree diagram
Usegeneratedkeys=true configuration
pthon 执行 pip 指令报错 You should consider upgrading via ...
Dragon Boat Festival wellbeing, use blessing words to generate word cloud
Wechat applet (pull-down refresh data) novice to
Detailed explanation of PHP distributed transaction principle