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
95594f8f
Commit
95594f8f
authored
Dec 11, 2023
by
马伊齐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Socket部分除注销注册函数的测试库完成
parent
509e1f53
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
109 deletions
+86
-109
sock_msg.c
lib/sock_msg.c
+86
-109
No files found.
lib/sock_msg.c
View file @
95594f8f
...
@@ -14,9 +14,9 @@
...
@@ -14,9 +14,9 @@
#include <mqueue.h>
#include <mqueue.h>
#include <sys/ipc.h>
#include <sys/ipc.h>
Message
messageQueue
[
MAX_MESSAGES
];
// 消息队列
Message
messageQueue
[
MAX_MESSAGES
];
// 消息队列
int
messageCount
=
0
;
// 消息队列中消息数量
int
messageCount
=
0
;
// 消息队列中消息数量
int
msgid
=
-
1
;
// 全局变量,用于存储消息队列ID
int
msgid
=
-
1
;
// 全局变量,用于存储消息队列ID
// socket信息结构体
// socket信息结构体
typedef
struct
typedef
struct
...
@@ -30,7 +30,7 @@ typedef struct
...
@@ -30,7 +30,7 @@ typedef struct
socket_info_t
sockets
[
MAX_CLIENTS
+
1
]
=
{
0
};
socket_info_t
sockets
[
MAX_CLIENTS
+
1
]
=
{
0
};
// 消息通道路径数组
// 消息通道路径数组
const
char
*
msg_channel_paths
[
MSG_CHAN_MAX
]
=
{
const
char
*
msg_channel_paths
[
MSG_CHAN_MAX
]
=
{
"/tmp/msg_channel_app"
,
"/tmp/msg_channel_app"
,
"/tmp/msg_channel_lim"
,
"/tmp/msg_channel_lim"
,
"/tmp/msg_channel_iomod"
,
"/tmp/msg_channel_iomod"
,
...
@@ -38,12 +38,12 @@ const char *msg_channel_paths [ MSG_CHAN_MAX ] = {
...
@@ -38,12 +38,12 @@ const char *msg_channel_paths [ MSG_CHAN_MAX ] = {
int
sock_init
(
int
server
,
uint32_t
ipaddr
,
uint32_t
port
)
int
sock_init
(
int
server
,
uint32_t
ipaddr
,
uint32_t
port
)
{
{
int
sockfd
;
int
sockfd
;
struct
sockaddr_in
serv_addr
;
struct
sockaddr_in
serv_addr
;
// 创建socket
// 创建socket
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
if
(
sockfd
<
0
)
{
{
// 错误处理
// 错误处理
perror
(
"socket error"
);
perror
(
"socket error"
);
...
@@ -51,15 +51,15 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
...
@@ -51,15 +51,15 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
}
}
// 如果是服务器模式
// 如果是服务器模式
if
(
server
)
if
(
server
)
{
{
// 初始化服务器地址结构,绑定IP地址和端口
// 初始化服务器地址结构,绑定IP地址和端口
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
// 清零结构体
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
// 清零结构体
serv_addr
.
sin_family
=
AF_INET
;
// 设置服务器地址结构
serv_addr
.
sin_family
=
AF_INET
;
// 设置服务器地址结构
serv_addr
.
sin_addr
.
s_addr
=
ipaddr
;
// 设置IP地址
serv_addr
.
sin_addr
.
s_addr
=
htonl
(
ipaddr
);
// 设置IP地址
serv_addr
.
sin_port
=
htons
(
port
);
// 设置端口
serv_addr
.
sin_port
=
htons
(
port
);
// 设置端口
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
{
// 错误处理
// 错误处理
perror
(
"bind error"
);
perror
(
"bind error"
);
...
@@ -67,7 +67,7 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
...
@@ -67,7 +67,7 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
}
}
// 监听
// 监听
if
(
listen
(
sockfd
,
20
)
<
0
)
if
(
listen
(
sockfd
,
20
)
<
0
)
{
{
// 错误处理
// 错误处理
perror
(
"listen error"
);
perror
(
"listen error"
);
...
@@ -78,12 +78,12 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
...
@@ -78,12 +78,12 @@ int sock_init(int server, uint32_t ipaddr, uint32_t port)
else
else
{
{
// 连接
// 连接
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
// 清零结构体
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
// 清零结构体
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
ipaddr
;
// 设置IP地址
serv_addr
.
sin_addr
.
s_addr
=
ipaddr
;
// 设置IP地址
serv_addr
.
sin_port
=
htons
(
port
);
// 设置端口
serv_addr
.
sin_port
=
htons
(
port
);
// 设置端口
if
(
connect
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
if
(
connect
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
{
{
// 错误处理
// 错误处理
perror
(
"connect error"
);
perror
(
"connect error"
);
...
@@ -101,93 +101,70 @@ void sock_exit(int fd)
...
@@ -101,93 +101,70 @@ void sock_exit(int fd)
int
sock_write
(
int
fd
,
const
uint8_t
*
buf
,
int
len
)
int
sock_write
(
int
fd
,
const
uint8_t
*
buf
,
int
len
)
{
{
int
bytes_sent
=
0
;
// 记录已发送的字节数
int
bytes_sent
;
// 记录每次发送的字节数
int
n
;
// 记录每次发送的字节数
while
(
bytes_sent
<
len
)
// 使用write函数发送数据,返回值是实际写入的字节数
bytes_sent
=
write
(
fd
,
buf
,
len
);
if
(
bytes_sent
<
0
)
{
{
// 使用write函数发送数据,返回值是实际写入的字节数
// 如果write函数返回-1,表示发送失败
n
=
write
(
fd
,
buf
+
bytes_sent
,
len
-
bytes_sent
);
if
(
errno
==
EINTR
)
if
(
n
<
0
)
{
{
// 如果write函数返回-1,表示发送失败
perror
(
"Interrupted system call"
);
if
(
errno
==
EINTR
)
{
// 如果是因为被信号中断,则继续发送剩余的数据
perror
(
"Interrupted system call, write again"
);
continue
;
}
else
{
// 其他错误,返回-1表示失败
perror
(
"Other errors, fail to send"
);
return
-
1
;
}
}
else
if
(
n
==
0
)
{
// 如果write函数返回0,表示对端已经关闭连接,发送失败
perror
(
"Peer closed connection"
);
return
-
1
;
}
}
else
else
{
{
// 更新已发送的字节数
// 其他错误,返回-1表示失败
bytes_sent
+=
n
;
perror
(
"Other errors, fail to send"
);
return
-
1
;
}
}
}
}
else
if
(
bytes_sent
==
0
)
{
// 如果write函数返回0,表示对端已经关闭连接,发送失败
perror
(
"Peer closed connection"
);
return
-
1
;
}
return
bytes_sent
;
return
bytes_sent
;
}
}
int
sock_read
(
int
fd
,
uint8_t
*
buf
,
int
len
)
int
sock_read
(
int
fd
,
uint8_t
*
buf
,
int
len
)
{
{
int
bytes_received
=
0
;
// 记录已接收的字节数
int
bytes_received
;
int
n
;
while
(
bytes_received
<
len
)
bytes_received
=
read
(
fd
,
buf
,
len
);
if
(
bytes_received
<
0
)
{
{
// 使用read函数接收数据,返回值是实际读入的字节数
// 如果read函数返回-1,表示接收失败
n
=
read
(
fd
,
buf
+
bytes_received
,
len
-
bytes_received
);
if
(
errno
==
EINTR
)
if
(
n
<
0
)
{
// 如果read函数返回-1,表示接收失败
if
(
errno
==
EINTR
)
{
// 如果是因为被信号中断,则继续接收剩余的数据
perror
(
"Interrupted system call, read again"
);
continue
;
}
else
{
// 其他错误,返回-1表示失败
perror
(
"Other errors, fail to receive"
);
return
-
1
;
}
}
else
if
(
n
==
0
)
{
{
// 如果read函数返回0,表示对端已经关闭连接,接收完成
// 如果是因为被信号中断,则继续接收剩余的数据
printf
(
"Peer closed connection
\n
"
);
perror
(
"Interrupted system call, read again"
);
break
;
}
}
else
else
{
{
// 更新已接收的字节数
// 其他错误,返回-1表示失败
bytes_received
+=
n
;
perror
(
"Other errors, fail to receive"
);
return
-
1
;
}
}
}
}
else
if
(
bytes_received
==
0
)
{
// 如果read函数返回0,表示对端已经关闭连接,接收完成
perror
(
"Peer closed connection"
);
}
return
bytes_received
;
return
bytes_received
;
}
}
int
sock_info
(
int
fd
,
uint32_t
*
ipaddr
,
uint32_t
*
port
)
int
sock_info
(
int
fd
,
uint32_t
*
ipaddr
,
uint32_t
*
port
)
{
{
struct
sockaddr_in
peer_addr
;
// 对端地址结构体
struct
sockaddr_in
peer_addr
;
// 对端地址结构体
socklen_t
peer_addr_len
=
sizeof
(
peer_addr
);
// 对端地址结构体长度
socklen_t
peer_addr_len
=
sizeof
(
peer_addr
);
// 对端地址结构体长度
// 使用getpeername函数获取对端地址信息
// 使用getpeername函数获取对端地址信息
if
(
getpeername
(
fd
,
(
struct
sockaddr
*
)
&
peer_addr
,
&
peer_addr_len
)
==
-
1
)
if
(
getpeername
(
fd
,
(
struct
sockaddr
*
)
&
peer_addr
,
&
peer_addr_len
)
==
-
1
)
{
{
// 如果getpeername函数返回-1,表示获取失败
// 如果getpeername函数返回-1,表示获取失败
perror
(
"getpeername error"
);
perror
(
"getpeername error"
);
...
@@ -196,7 +173,7 @@ int sock_info(int fd, uint32_t *ipaddr, uint32_t *port)
...
@@ -196,7 +173,7 @@ int sock_info(int fd, uint32_t *ipaddr, uint32_t *port)
// 从对端地址结构体中获取IP地址和端口号
// 从对端地址结构体中获取IP地址和端口号
*
ipaddr
=
ntohl
(
peer_addr
.
sin_addr
.
s_addr
);
*
ipaddr
=
ntohl
(
peer_addr
.
sin_addr
.
s_addr
);
*
port
=
ntohs
(
peer_addr
.
sin_port
);
*
port
=
ntohs
(
peer_addr
.
sin_port
);
return
0
;
return
0
;
}
}
...
@@ -231,18 +208,18 @@ int sock_ev_unregister(int fd)
...
@@ -231,18 +208,18 @@ int sock_ev_unregister(int fd)
return
-
1
;
// 失败,未找到对应的socket
return
-
1
;
// 失败,未找到对应的socket
}
}
int
init_msg_queue
(
)
int
init_msg_queue
()
{
{
key_t
key
;
key_t
key
;
key
=
ftok
(
"."
,
10
);
key
=
ftok
(
"."
,
10
);
if
(
key
==
-
1
)
if
(
key
==
-
1
)
{
{
perror
(
"ftok(): "
);
perror
(
"ftok(): "
);
return
-
1
;
return
-
1
;
}
}
msgid
=
msgget
(
key
,
IPC_CREAT
|
0666
);
msgid
=
msgget
(
key
,
IPC_CREAT
|
0666
);
if
(
msgid
==
-
1
)
if
(
msgid
==
-
1
)
{
{
perror
(
"msgget(): "
);
perror
(
"msgget(): "
);
return
-
1
;
return
-
1
;
...
@@ -251,11 +228,11 @@ int init_msg_queue( )
...
@@ -251,11 +228,11 @@ int init_msg_queue( )
return
0
;
return
0
;
}
}
int
destroy_msg_queue
(
)
int
destroy_msg_queue
()
{
{
if
(
msgid
!=
-
1
)
if
(
msgid
!=
-
1
)
{
{
if
(
msgctl
(
msgid
,
IPC_RMID
,
NULL
)
==
-
1
)
if
(
msgctl
(
msgid
,
IPC_RMID
,
NULL
)
==
-
1
)
{
{
perror
(
"msgctl(): "
);
perror
(
"msgctl(): "
);
return
-
1
;
return
-
1
;
...
@@ -267,26 +244,26 @@ int destroy_msg_queue( )
...
@@ -267,26 +244,26 @@ int destroy_msg_queue( )
int
msg_local_recv
(
msg_data_t
*
msg
)
int
msg_local_recv
(
msg_data_t
*
msg
)
{
{
if
(
msgid
==
-
1
)
if
(
msgid
==
-
1
)
{
{
fprintf
(
stderr
,
"Message queue not initialized.
\n
"
);
fprintf
(
stderr
,
"Message queue not initialized.
\n
"
);
return
-
1
;
return
-
1
;
}
}
printf
(
"msgid: %d
\n
"
,
msgid
);
printf
(
"msgid: %d
\n
"
,
msgid
);
ssize_t
rbytes
=
msgrcv
(
msgid
,
(
void
*
)
msg
,
sizeof
(
msg_data_t
),
0
,
0
);
ssize_t
rbytes
=
msgrcv
(
msgid
,
(
void
*
)
msg
,
sizeof
(
msg_data_t
),
0
,
0
);
if
(
rbytes
==
-
1
)
if
(
rbytes
==
-
1
)
{
{
perror
(
"[ERROR] msgrcv(): "
);
perror
(
"[ERROR] msgrcv(): "
);
return
-
1
;
return
-
1
;
}
}
return
(
int
)
rbytes
;
return
(
int
)
rbytes
;
}
}
int
msg_local_send
(
uint16_t
type
,
uint32_t
code
,
void
*
pad
,
int
len
)
int
msg_local_send
(
uint16_t
type
,
uint32_t
code
,
void
*
pad
,
int
len
)
{
{
if
(
msgid
==
-
1
)
if
(
msgid
==
-
1
)
{
{
fprintf
(
stderr
,
"Message queue not initialized.
\n
"
);
fprintf
(
stderr
,
"Message queue not initialized.
\n
"
);
return
-
1
;
return
-
1
;
...
@@ -298,7 +275,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
...
@@ -298,7 +275,7 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
memcpy
(
msg
.
pad
,
pad
,
len
);
memcpy
(
msg
.
pad
,
pad
,
len
);
msg
.
len
=
len
;
msg
.
len
=
len
;
if
(
msgsnd
(
msgid
,
(
const
void
*
)
&
msg
,
sizeof
(
msg
),
0
)
==
-
1
)
if
(
msgsnd
(
msgid
,
(
const
void
*
)
&
msg
,
sizeof
(
msg
),
0
)
==
-
1
)
{
{
perror
(
"msgsnd(): "
);
perror
(
"msgsnd(): "
);
return
-
1
;
return
-
1
;
...
@@ -309,50 +286,50 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
...
@@ -309,50 +286,50 @@ int msg_local_send(uint16_t type, uint32_t code, void *pad, int len)
int
msg_local_recv_pulse
(
uint16_t
*
type
,
uint32_t
*
code
)
int
msg_local_recv_pulse
(
uint16_t
*
type
,
uint32_t
*
code
)
{
{
if
(
messageCount
>
0
)
if
(
messageCount
>
0
)
{
{
*
type
=
messageQueue
[
0
].
type
;
*
type
=
messageQueue
[
0
].
type
;
*
code
=
messageQueue
[
0
].
code
;
*
code
=
messageQueue
[
0
].
code
;
// 移动队列
// 移动队列
for
(
int
i
=
0
;
i
<
messageCount
-
1
;
i
++
)
for
(
int
i
=
0
;
i
<
messageCount
-
1
;
i
++
)
{
{
messageQueue
[
i
]
=
messageQueue
[
i
+
1
];
messageQueue
[
i
]
=
messageQueue
[
i
+
1
];
}
}
messageCount
--
;
messageCount
--
;
return
0
;
// 成功
return
0
;
// 成功
}
}
else
else
{
{
errno
=
EAGAIN
;
// 队列空
errno
=
EAGAIN
;
// 队列空
return
-
1
;
// 失败
return
-
1
;
// 失败
}
}
}
}
int
msg_local_send_pulse
(
uint16_t
type
,
uint32_t
code
)
int
msg_local_send_pulse
(
uint16_t
type
,
uint32_t
code
)
{
{
if
(
messageCount
<
MAX_MESSAGES
)
if
(
messageCount
<
MAX_MESSAGES
)
{
{
messageQueue
[
messageCount
].
type
=
type
;
messageQueue
[
messageCount
].
type
=
type
;
messageQueue
[
messageCount
].
code
=
code
;
messageQueue
[
messageCount
].
code
=
code
;
messageCount
++
;
messageCount
++
;
return
0
;
// 成功
return
0
;
// 成功
}
}
else
else
{
{
errno
=
EAGAIN
;
// 队列满
errno
=
EAGAIN
;
// 队列满
return
-
1
;
// 失败
return
-
1
;
// 失败
}
}
}
}
int
msg_recv
(
int
channel
,
msg_data_t
*
msg
)
int
msg_recv
(
int
channel
,
msg_data_t
*
msg
)
{
{
if
(
channel
<
0
||
channel
>=
MSG_CHAN_MAX
||
msg
==
NULL
)
if
(
channel
<
0
||
channel
>=
MSG_CHAN_MAX
||
msg
==
NULL
)
{
{
return
-
1
;
return
-
1
;
}
}
int
fd
=
open
(
msg_channel_paths
[
channel
],
O_RDONLY
);
int
fd
=
open
(
msg_channel_paths
[
channel
],
O_RDONLY
);
if
(
fd
==
-
1
)
if
(
fd
==
-
1
)
{
{
perror
(
"msg_recv: open"
);
perror
(
"msg_recv: open"
);
return
-
1
;
return
-
1
;
...
@@ -366,7 +343,7 @@ int msg_recv(int channel, msg_data_t *msg)
...
@@ -366,7 +343,7 @@ int msg_recv(int channel, msg_data_t *msg)
int
msg_send
(
int
channel
,
uint16_t
type
,
uint32_t
code
,
void
*
pad
,
int
len
)
int
msg_send
(
int
channel
,
uint16_t
type
,
uint32_t
code
,
void
*
pad
,
int
len
)
{
{
if
(
channel
<
0
||
channel
>=
MSG_CHAN_MAX
||
(
len
>
0
&&
pad
==
NULL
)
)
if
(
channel
<
0
||
channel
>=
MSG_CHAN_MAX
||
(
len
>
0
&&
pad
==
NULL
)
)
{
{
return
-
1
;
return
-
1
;
}
}
...
@@ -374,11 +351,11 @@ int msg_send(int channel, uint16_t type, uint32_t code, void *pad, int len)
...
@@ -374,11 +351,11 @@ int msg_send(int channel, uint16_t type, uint32_t code, void *pad, int len)
msg_data_t
msg
=
{
msg_data_t
msg
=
{
.
type
=
type
,
.
type
=
type
,
.
code
=
code
,
.
code
=
code
,
.
len
=
len
};
.
len
=
len
};
memcpy
((
void
*
)(
msg
.
pad
),
pad
,
len
);
memcpy
((
void
*
)(
msg
.
pad
),
pad
,
len
);
int
fd
=
open
(
msg_channel_paths
[
channel
],
O_WRONLY
);
int
fd
=
open
(
msg_channel_paths
[
channel
],
O_WRONLY
);
if
(
fd
==
-
1
)
if
(
fd
==
-
1
)
{
{
perror
(
"msg_send: open"
);
perror
(
"msg_send: open"
);
return
-
1
;
return
-
1
;
...
...
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