-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalSocket.cpp
123 lines (96 loc) · 2.74 KB
/
LocalSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* =====================================================================================
*
* Filename: LocalSocket.cpp
*
* Description:
*
* Version: 1.0
* Created: 2017年08月26日 11时08分28秒
* Last Modified: 2017年08月26日 11时08分28秒
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
#include "LocalSocket.h"
LocalSocket::LocalSocket ( std::string lsSelf, std::string lsRemote )
: m_lsSelf ( lsSelf ), m_lsRemote ( lsRemote ),
m_sockfd_self ( -1 ), m_sockfd_remote ( -1 )
{
Init();
}
LocalSocket::~LocalSocket()
{
DeInit();
}
int LocalSocket::Init()
{
m_sockfd_self = socket ( AF_UNIX, SOCK_DGRAM, 0 );
int loop = 1;
setsockopt ( m_sockfd_self, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*> ( &loop ), sizeof ( loop ) );
m_self_addr.sun_family = AF_UNIX;
strcpy ( m_self_addr.sun_path, m_lsSelf.c_str() );
m_self_addr.sun_path[0] = 0;
m_selfaddrlen = m_lsSelf.size() + offsetof ( sockaddr_un, sun_path );
bind ( m_sockfd_self, reinterpret_cast<sockaddr*> ( &m_self_addr ), m_selfaddrlen );
m_sockfd_remote = socket ( AF_UNIX, SOCK_DGRAM, 0 );
m_remote_addr.sun_family = AF_UNIX;
strcpy ( m_remote_addr.sun_path, m_lsRemote.c_str() );
m_remote_addr.sun_path[0] = 0;
m_remoteaddrlen = m_lsRemote.size() + offsetof ( sockaddr_un, sun_path );
return 0;
}
int LocalSocket::DeInit()
{
if ( m_sockfd_self > 0 )
close ( m_sockfd_self );
if ( m_sockfd_remote > 0 )
close ( m_sockfd_remote );
return 0;
}
int LocalSocket::Send ( const uint8_t* sendbuf, int size )
{
if ( m_sockfd_remote < 0 )
return -1;
return sendto ( m_sockfd_remote, reinterpret_cast<const char*> ( sendbuf ), size,
MSG_NOSIGNAL,
reinterpret_cast<sockaddr*> ( &m_remote_addr ), m_remoteaddrlen );
}
int LocalSocket::Recv ( uint8_t* recvbuf, int size )
{
if ( m_sockfd_self < 0 )
return -1;
sockaddr addr;
socklen_t len = sizeof ( addr );
return recvfrom ( m_sockfd_self, reinterpret_cast<char*> ( recvbuf ),
size, 0, &addr, &len );
}
#ifdef TEST
int main ( int argc, char* argv[] )
{
LocalSocket ls1 ( "@aaa", "@bbb" );
LocalSocket ls2 ( "@bbb", "@aaa" );
uint8_t a[] = "test aaaaaa";
uint8_t b[] = "test bbbbbb";
uint8_t buf1[1024];
uint8_t buf2[1024];
while ( 1 )
{
ls1.Send ( a, 10 );
memset ( buf1, 0, 1024 );
ls2.Recv ( buf1, 10 );
printf ( "ls2 recv ls1 %s\n", buf1 );
ls2.Send ( b, 10 );
memset ( buf2, 0, 1024 );
ls1.Recv ( buf2, 10 );
printf ( "ls1 recv ls2 %s\n", buf2 );
sleep ( 1 );
}
return 0;
}
#endif