Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
netty
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Administrator
netty
Commits
5bdce115
Commit
5bdce115
authored
Sep 02, 2017
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改为子线程处理读事件
parent
a2d1ccaa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
3 additions
and
18 deletions
+3
-18
src/main/java/com/paul0523/TcpServer.java
src/main/java/com/paul0523/TcpServer.java
+3
-18
No files found.
src/main/java/com/paul0523/TcpServer.java
View file @
5bdce115
...
...
@@ -68,24 +68,9 @@ public class TcpServer {
}
private
void
handleRead
(
SelectionKey
key
)
throws
Exception
{
SocketChannel
socketChannel
=
(
SocketChannel
)
key
.
channel
();
System
.
out
.
println
(
cache
.
containsKey
(
key
));
ByteBuffer
byteBuffer
=
(
ByteBuffer
)
key
.
attachment
();
byteBuffer
.
clear
();
int
readbytes
=
socketChannel
.
read
(
byteBuffer
);
byteBuffer
.
flip
();
if
(
readbytes
>
0
)
{
byte
[]
bytes
=
new
byte
[
readbytes
];
byteBuffer
.
get
(
bytes
,
0
,
readbytes
);
System
.
out
.
println
(
new
String
(
bytes
,
0
,
readbytes
,
Charset
.
forName
(
"utf-8"
)));
}
else
if
(
readbytes
==
-
1
)
{
socketChannel
.
close
();
return
;
}
byteBuffer
.
flip
();
key
.
attach
(
byteBuffer
);
forCast
(
byteBuffer
);
key
.
interestOps
(
SelectionKey
.
OP_WRITE
);
key
.
interestOps
(
0
);
//禁止下一次获取读事件,在子线程中重置事件, 重置完后调用wakeup重新获取感兴趣事件
new
Thread
(
new
ReadHandler
(
key
)).
start
();
}
private
void
forCast
(
ByteBuffer
byteBuffer
)
{
...
...
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