Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
HR_Socket_Msg_Demo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
马伊齐
HR_Socket_Msg_Demo
Commits
f3df337e
Commit
f3df337e
authored
Dec 19, 2023
by
马伊齐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
进程间信号通信(完成)
parent
ad3e990b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
25 deletions
+95
-25
sock_msg.c
lib/sock_msg.c
+74
-25
sock_msg.h
lib/sock_msg.h
+21
-0
No files found.
lib/sock_msg.c
100644 → 100755
View file @
f3df337e
...
...
@@ -13,10 +13,14 @@
#include <sys/msg.h>
#include <mqueue.h>
#include <sys/ipc.h>
#include <sys/shm.h>
Message
messageQueue
[
MAX_MESSAGES
];
// 消息队列
int
messageCount
=
0
;
// 消息队列中消息数量
#define SHM_KEY 1234
int
msgid
=
-
1
;
// 全局变量,用于存储消息队列ID
int
shmid
;
messageQueue
*
queue
;
// socket信息结构体
typedef
struct
...
...
@@ -284,41 +288,86 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
return
0
;
}
int
msg_local_
recv_pulse
(
uint16_t
*
type
,
uint32_t
*
code
)
int
msg_local_
ssignal_init
(
void
)
{
if
(
messageCount
>
0
)
// 获取已存在的共享内存
if
((
shmid
=
shmget
(
SHM_KEY
,
sizeof
(
int
),
IPC_CREAT
|
0666
))
<
0
)
{
perror
(
"shmget"
);
return
-
1
;
}
// 将共享内存连接到进程地址空间
if
((
queue
=
shmat
(
shmid
,
NULL
,
0
))
==
(
messageQueue
*
)
-
1
)
{
*
type
=
messageQueue
[
0
].
type
;
*
code
=
messageQueue
[
0
].
code
;
// 移动队列
for
(
int
i
=
0
;
i
<
messageCount
-
1
;
i
++
)
perror
(
"shmat"
);
return
-
1
;
}
queue
->
front
=
0
;
queue
->
rear
=
-
1
;
}
int
msg_local_rsignal_init
(
void
)
{
shmid
=
shmget
(
SHM_KEY
,
sizeof
(
int
),
0666
);
// 获取已存在的共享内存
if
((
shmid
=
shmget
(
SHM_KEY
,
sizeof
(
int
),
0666
))
<
0
)
{
perror
(
"shmget"
);
return
-
1
;
}
// 将共享内存连接到进程地址空间
if
((
queue
=
shmat
(
shmid
,
NULL
,
0
))
==
(
messageQueue
*
)
-
1
)
{
messageQueue
[
i
]
=
messageQueue
[
i
+
1
];
perror
(
"shmat"
);
return
-
1
;
}
messageCount
--
;
return
0
;
// 成功
}
int
msg_local_send_pulse
(
uint16_t
type
,
uint32_t
code
)
{
if
(
queue
->
rear
==
queue
->
front
)
{
printf
(
"signal queue already full
\n
"
);
return
-
1
;
}
else
if
(
queue
->
rear
==
-
1
)
{
queue
->
rear
=
0
;
queue
->
front
=
1
;
queue
->
mQueue
[
0
].
type
=
type
;
queue
->
mQueue
[
0
].
code
=
code
;
return
0
;
}
else
{
errno
=
EAGAIN
;
// 队列空
return
-
1
;
// 失败
queue
->
mQueue
[
queue
->
front
].
type
=
type
;
queue
->
mQueue
[
queue
->
front
].
code
=
code
;
queue
->
front
=
(
queue
->
front
+
1
)
%
MAX_QSIZE
;
return
0
;
}
}
int
msg_local_
send_pulse
(
uint16_t
type
,
uint32_t
code
)
{
if
(
messageCount
<
MAX_MESSAGES
)
int
msg_local_
recv_pulse
(
uint16_t
*
type
,
uint32_t
*
code
)
{
if
(
queue
->
rear
==
-
1
)
{
messageQueue
[
messageCount
].
type
=
type
;
messageQueue
[
messageCount
].
code
=
code
;
messageCount
++
;
return
0
;
// 成功
printf
(
"Null signal queue
\n
"
);
return
-
1
;
}
else
if
((
queue
->
rear
+
1
)
%
MAX_QSIZE
==
queue
->
front
)
{
*
type
=
queue
->
mQueue
[
queue
->
rear
].
type
;
*
code
=
queue
->
mQueue
[
queue
->
rear
].
code
;
queue
->
rear
=
-
1
;
return
0
;
}
else
{
errno
=
EAGAIN
;
// 队列满
return
-
1
;
// 失败
*
type
=
queue
->
mQueue
[
queue
->
rear
].
type
;
*
code
=
queue
->
mQueue
[
queue
->
rear
].
code
;
queue
->
rear
=
(
queue
->
rear
+
1
)
%
MAX_QSIZE
;
return
0
;
}
}
int
msg_recv
(
int
channel
,
msg_data_t
*
msg
)
...
...
lib/sock_msg.h
100644 → 100755
View file @
f3df337e
...
...
@@ -7,10 +7,25 @@
// 读写模式宏定义
#define SOCK_READ 0x01
#define SOCK_WRITE 0x02
#define MAX_QSIZE 100 // 消息队列最大长度
#define BUFFER_SIZE 4096 // 缓冲区大小
#define MAX_CLIENTS 10 // 最大连接客户端数量
typedef
unsigned
short
uint16_t
;
typedef
unsigned
int
uint32_t
;
typedef
struct
message
{
uint16_t
type
;
uint32_t
code
;
}
message
;
typedef
struct
messageQueue
{
int
front
;
int
rear
;
message
mQueue
[
MAX_QSIZE
];
}
messageQueue
;
/** 定义函数指针类型 */
typedef
int
(
*
sock_callback_t
)(
int
,
uint8_t
*
,
int
);
...
...
@@ -188,6 +203,11 @@ int msg_local_recv(msg_data_t *msg);
*/
int
msg_local_send
(
uint16_t
type
,
uint32_t
code
,
void
*
pad
,
int
len
);
// 以下三个函数是用于进程间信号通讯的
int
msg_local_ssignal_init
(
void
);
int
msg_local_rsignal_init
(
void
);
int
msg_local_recv_pulse
(
uint16_t
*
type
,
uint32_t
*
code
);
/**
* @brief 本地进程间发送信号(整形)
*
...
...
@@ -197,6 +217,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len);
*/
int
msg_local_send_pulse
(
uint16_t
type
,
uint32_t
code
);
/**
* @brief 接收消息,主要用于进程间消息通讯
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment