Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
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
zengtianlai3
ioc_sixiang_license
Commits
905a535b
Commit
905a535b
authored
Jul 16, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化tcp客户端线程
parent
ee847bc3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
48 deletions
+76
-48
ForwardClient.java
.../main/java/iot/sixiang/license/forward/ForwardClient.java
+27
-3
TcpClient.java
license/src/main/java/iot/sixiang/license/net/TcpClient.java
+22
-42
OperateClient.java
.../main/java/iot/sixiang/license/operate/OperateClient.java
+27
-3
No files found.
license/src/main/java/iot/sixiang/license/forward/ForwardClient.java
View file @
905a535b
package
iot
.
sixiang
.
license
.
forward
;
package
iot
.
sixiang
.
license
.
forward
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
iot.sixiang.license.net.TcpClient
;
import
iot.sixiang.license.net.TcpClient
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
@Component
@Component
@Slf4j
@Slf4j
public
class
ForwardClient
{
public
class
ForwardClient
{
...
@@ -12,6 +19,7 @@ public class ForwardClient {
...
@@ -12,6 +19,7 @@ public class ForwardClient {
private
TcpClient
client
=
null
;
private
TcpClient
client
=
null
;
private
ForwardChannelInitializer
channelInitializer
;
private
ForwardChannelInitializer
channelInitializer
;
private
Bootstrap
bootstrap
;
@Autowired
@Autowired
ForwardClientHandler
handler
;
ForwardClientHandler
handler
;
...
@@ -20,15 +28,31 @@ public class ForwardClient {
...
@@ -20,15 +28,31 @@ public class ForwardClient {
}
}
@PostConstruct
public
void
init
(){
channelInitializer
=
new
ForwardChannelInitializer
(
handler
);
EventLoopGroup
eventLoopGroup
=
new
NioEventLoopGroup
();
bootstrap
=
new
Bootstrap
();
if
(
bootstrap
!=
null
)
{
try
{
bootstrap
.
channel
(
NioSocketChannel
.
class
)
.
option
(
ChannelOption
.
SO_KEEPALIVE
,
true
)
.
group
(
eventLoopGroup
)
.
handler
(
channelInitializer
);
}
catch
(
IllegalStateException
ex
)
{
log
.
error
(
ex
.
getMessage
());
}
}
}
public
void
startTcp
(
String
host
,
int
port
,
String
appId
)
{
public
void
startTcp
(
String
host
,
int
port
,
String
appId
)
{
log
.
debug
(
"桥接客户端,开始连接桥接服务:{},{},{}"
,
host
,
port
,
appId
);
log
.
debug
(
"桥接客户端,开始连接桥接服务:{},{},{}"
,
host
,
port
,
appId
);
ForwardConnectionListener
listener
=
new
ForwardConnectionListener
();
ForwardConnectionListener
listener
=
new
ForwardConnectionListener
();
listener
.
setAppId
(
appId
);
listener
.
setAppId
(
appId
);
listener
.
setHost
(
host
);
listener
.
setHost
(
host
);
listener
.
setPort
(
port
);
listener
.
setPort
(
port
);
client
=
new
TcpClient
(
host
,
port
,
channelInitializer
,
listener
,
bootstrap
);
channelInitializer
=
new
ForwardChannelInitializer
(
handler
);
client
=
new
TcpClient
(
host
,
port
,
channelInitializer
,
listener
);
client
.
start
();
client
.
start
();
}
}
...
...
license/src/main/java/iot/sixiang/license/net/TcpClient.java
View file @
905a535b
...
@@ -2,10 +2,6 @@ package iot.sixiang.license.net;
...
@@ -2,10 +2,6 @@ package iot.sixiang.license.net;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
@Slf4j
@Slf4j
...
@@ -14,38 +10,22 @@ public class TcpClient {
...
@@ -14,38 +10,22 @@ public class TcpClient {
private
String
host
;
private
String
host
;
private
BaseChannelInitializer
channelInitializer
;
private
BaseChannelInitializer
channelInitializer
;
private
BaseConnectionListener
connectionListener
;
private
BaseConnectionListener
connectionListener
;
private
Bootstrap
bootstrap
;
public
TcpClient
(
String
host
,
int
port
,
BaseChannelInitializer
channelInitializer
,
BaseConnectionListener
connectionListener
)
{
public
TcpClient
(
String
host
,
int
port
,
BaseChannelInitializer
channelInitializer
,
BaseConnectionListener
connectionListener
,
Bootstrap
bootstrap
)
{
this
.
host
=
host
;
this
.
host
=
host
;
this
.
port
=
port
;
this
.
port
=
port
;
this
.
channelInitializer
=
channelInitializer
;
this
.
channelInitializer
=
channelInitializer
;
this
.
connectionListener
=
connectionListener
;
this
.
connectionListener
=
connectionListener
;
this
.
bootstrap
=
bootstrap
;
}
}
public
void
start
()
{
public
void
start
()
{
Thread
thread
=
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
EventLoopGroup
eventLoopGroup
=
new
NioEventLoopGroup
();
Bootstrap
bootstrap
=
new
Bootstrap
();
try
{
bootstrap
.
channel
(
NioSocketChannel
.
class
)
.
option
(
ChannelOption
.
SO_KEEPALIVE
,
true
)
.
group
(
eventLoopGroup
)
.
remoteAddress
(
host
,
port
)
.
handler
(
channelInitializer
);
}
catch
(
NullPointerException
e
)
{
log
.
error
(
e
.
getMessage
());
}
catch
(
IllegalStateException
ex
)
{
log
.
error
(
ex
.
getMessage
());
}
ChannelFuture
future
=
bootstrap
.
connect
(
host
,
port
);
ChannelFuture
future
=
bootstrap
.
connect
(
host
,
port
);
future
.
addListener
(
connectionListener
);
future
.
addListener
(
connectionListener
);
}
});
thread
.
start
();
}
}
...
...
license/src/main/java/iot/sixiang/license/operate/OperateClient.java
View file @
905a535b
package
iot
.
sixiang
.
license
.
operate
;
package
iot
.
sixiang
.
license
.
operate
;
import
io.netty.bootstrap.Bootstrap
;
import
io.netty.channel.ChannelOption
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.nio.NioSocketChannel
;
import
iot.sixiang.license.net.TcpClient
;
import
iot.sixiang.license.net.TcpClient
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
@Component
@Component
@Slf4j
@Slf4j
public
class
OperateClient
{
public
class
OperateClient
{
...
@@ -12,7 +19,7 @@ public class OperateClient {
...
@@ -12,7 +19,7 @@ public class OperateClient {
private
TcpClient
client
=
null
;
private
TcpClient
client
=
null
;
private
OperateChannelInitializer
channelInitializer
;
private
OperateChannelInitializer
channelInitializer
;
private
Bootstrap
bootstrap
;
@Autowired
@Autowired
OperateClientHandler
handler
;
OperateClientHandler
handler
;
...
@@ -21,13 +28,30 @@ public class OperateClient {
...
@@ -21,13 +28,30 @@ public class OperateClient {
}
}
@PostConstruct
public
void
init
(){
channelInitializer
=
new
OperateChannelInitializer
(
handler
);
EventLoopGroup
eventLoopGroup
=
new
NioEventLoopGroup
();
bootstrap
=
new
Bootstrap
();
if
(
bootstrap
!=
null
)
{
try
{
bootstrap
.
channel
(
NioSocketChannel
.
class
)
.
option
(
ChannelOption
.
SO_KEEPALIVE
,
true
)
.
group
(
eventLoopGroup
)
.
handler
(
channelInitializer
);
}
catch
(
IllegalStateException
ex
)
{
log
.
error
(
ex
.
getMessage
());
}
}
}
public
void
startTcp
(
String
host
,
int
port
)
{
public
void
startTcp
(
String
host
,
int
port
)
{
OperateConnectionListener
listener
=
new
OperateConnectionListener
();
OperateConnectionListener
listener
=
new
OperateConnectionListener
();
listener
.
setHost
(
host
);
listener
.
setHost
(
host
);
listener
.
setPort
(
port
);
listener
.
setPort
(
port
);
channelInitializer
=
new
OperateChannelInitializer
(
handler
);
client
=
new
TcpClient
(
host
,
port
,
channelInitializer
,
listener
,
bootstrap
);
client
=
new
TcpClient
(
host
,
port
,
channelInitializer
,
listener
);
client
.
start
();
client
.
start
();
}
}
...
...
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