Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
5c3cce7e
Commit
5c3cce7e
authored
Aug 11, 2019
by
liujiahua123123
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
network
parent
e342261e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
59 deletions
+60
-59
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
+12
-11
mirai-core/src/main/java/net/mamoe/mirai/network/MiraiNetwork.java
...e/src/main/java/net/mamoe/mirai/network/MiraiNetwork.java
+1
-48
mirai-core/src/main/java/net/mamoe/mirai/network/MiraiUDPClient.java
...src/main/java/net/mamoe/mirai/network/MiraiUDPClient.java
+47
-0
No files found.
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
View file @
5c3cce7e
...
@@ -84,16 +84,17 @@ public class MiraiServer {
...
@@ -84,16 +84,17 @@ public class MiraiServer {
this
.
setting
=
new
MiraiConfig
(
setting
);
this
.
setting
=
new
MiraiConfig
(
setting
);
}
}
int
port
=
this
.
setting
.
getMapSection
(
"network"
).
getInt
(
"port"
);
MiraiMapSection
qqs
=
this
.
setting
.
getMapSection
(
"qq"
);
MiraiNetwork
.
start
(
port
);
qqs
.
forEach
((
a
,
p
)
->
{
Thread
.
yield
();
this
.
getLogger
().
log
(
LoggerTextFormat
.
SKY_BLUE
+
"Finding available ports between "
+
"1-65536"
);
if
(
MiraiNetwork
.
getLastError
()!=
null
){
try
{
this
.
getLogger
().
log
(
LoggerTextFormat
.
RED
+
"an error occurred when staring network layer"
);
int
port
=
MiraiNetwork
.
getAvailablePort
();
this
.
shutdown
();
this
.
getLogger
().
log
(
LoggerTextFormat
.
SKY_BLUE
+
"Listening on port "
+
port
);
}
}
catch
(
IOException
e
)
{
this
.
getLogger
().
log
(
LoggerTextFormat
.
SKY_BLUE
+
"Listening on port "
+
port
);
e
.
printStackTrace
();
}
});
}
}
public
void
initSetting
(
File
setting
){
public
void
initSetting
(
File
setting
){
...
@@ -108,7 +109,7 @@ public class MiraiServer {
...
@@ -108,7 +109,7 @@ public class MiraiServer {
}
}
this
.
setting
=
new
MiraiConfig
(
setting
);
this
.
setting
=
new
MiraiConfig
(
setting
);
MiraiMapSection
network
=
this
.
setting
.
getMapSection
(
"network"
);
MiraiMapSection
network
=
this
.
setting
.
getMapSection
(
"network"
);
network
.
put
(
"port"
,
19139
);
MiraiMapSection
qqs
=
this
.
setting
.
getMapSection
(
"qq"
);
MiraiMapSection
qqs
=
this
.
setting
.
getMapSection
(
"qq"
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
this
.
getLogger
().
log
(
LoggerTextFormat
.
SKY_BLUE
+
"input one "
+
LoggerTextFormat
.
RED
+
" QQ number "
+
LoggerTextFormat
.
SKY_BLUE
+
"for default robot"
);
this
.
getLogger
().
log
(
LoggerTextFormat
.
SKY_BLUE
+
"input one "
+
LoggerTextFormat
.
RED
+
" QQ number "
+
LoggerTextFormat
.
SKY_BLUE
+
"for default robot"
);
...
...
mirai-core/src/main/java/net/mamoe/mirai/network/MiraiNetwork.java
View file @
5c3cce7e
package
net.mamoe.mirai.network
;
package
net.mamoe.mirai.network
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.bootstrap.ServerBootstrap
;
import
io.netty.channel.ChannelInitializer
;
import
io.netty.channel.ChannelPipeline
;
import
io.netty.channel.EventLoopGroup
;
import
io.netty.channel.nio.NioEventLoopGroup
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.nio.NioServerSocketChannel
;
import
io.netty.handler.codec.bytes.ByteArrayDecoder
;
import
io.netty.handler.codec.bytes.ByteArrayEncoder
;
import
lombok.Getter
;
import
lombok.Getter
;
import
net.mamoe.mirai.MiraiServer
;
import
net.mamoe.mirai.event.events.server.ServerDisableEvent
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.ServerSocket
;
import
java.net.ServerSocket
;
public
class
MiraiNetwork
{
public
class
MiraiNetwork
{
private
static
ServerBootstrap
server
;
private
static
Thread
thread
;
@Getter
@Getter
private
static
volatile
Throwable
lastError
=
null
;
private
static
volatile
Throwable
lastError
=
null
;
public
static
void
start
(
int
port
){
public
static
void
start
(
int
port
){
thread
=
new
Thread
(()
->
{
if
(
server
!=
null
)
{
throw
new
RuntimeException
(
"there is already a ServerBootstrap instance"
);
}
EventLoopGroup
bossGroup
=
new
NioEventLoopGroup
();
EventLoopGroup
workerGroup
=
new
NioEventLoopGroup
();
try
{
server
=
new
ServerBootstrap
();
server
.
group
(
bossGroup
,
workerGroup
);
server
.
channel
(
NioServerSocketChannel
.
class
);
//b.option(ChannelOption.SO_BACKLOG, 100);
//b.handler(new LoggingHandler(LogLevel.INFO));
server
.
childHandler
(
new
ChannelInitializer
<
SocketChannel
>()
{
@Override
protected
void
initChannel
(
SocketChannel
ch
)
throws
Exception
{
ChannelPipeline
pipeline
=
ch
.
pipeline
();
pipeline
.
addLast
(
"bytesDecoder"
,
new
ByteArrayDecoder
());
pipeline
.
addLast
(
"bytesEncoder"
,
new
ByteArrayEncoder
());
pipeline
.
addLast
(
"handler"
,
new
NetworkPacketHandler
());
}
});
server
.
bind
(
port
).
sync
().
channel
().
closeFuture
().
sync
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
lastError
=
e
;
}
finally
{
bossGroup
.
shutdownGracefully
();
workerGroup
.
shutdownGracefully
();
}
});
thread
.
start
();
MiraiServer
.
getInstance
().
getEventManager
().
onEvent
(
ServerDisableEvent
.
class
).
setHandler
(
a
->
{
thread
.
interrupt
();
});
}
}
...
...
mirai-core/src/main/java/net/mamoe/mirai/network/MiraiUDPClient.java
0 → 100644
View file @
5c3cce7e
package
net.mamoe.mirai.network
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.*
;
import
java.nio.charset.StandardCharsets
;
public
class
MiraiUDPClient
{
private
DatagramSocket
localUDPSocket
;
private
Thread
thread
;
public
MiraiUDPClient
(
InetAddress
target
,
int
targetPort
,
int
localPort
)
{
try
{
this
.
localUDPSocket
=
new
DatagramSocket
(
localPort
);
this
.
localUDPSocket
.
connect
(
target
,
targetPort
);
this
.
localUDPSocket
.
setReuseAddress
(
true
);
this
.
thread
=
new
Thread
(()
->
{
try
{
while
(
true
)
{
byte
[]
data
=
new
byte
[
1024
];
// 接收数据报的包
DatagramPacket
packet
=
new
DatagramPacket
(
data
,
data
.
length
);
DatagramSocket
localUDPSocket
=
MiraiUDPClient
.
this
.
localUDPSocket
;
if
((
localUDPSocket
==
null
)
||
(
localUDPSocket
.
isClosed
()))
continue
;
// 阻塞直到收到数据
localUDPSocket
.
receive
(
packet
);
// 解析服务端发过来的数据
MiraiUDPClient
.
this
.
onReceive
(
packet
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
});
this
.
thread
.
start
();
}
catch
(
SocketException
e
)
{
e
.
printStackTrace
();
}
}
public
void
onReceive
(
DatagramPacket
packet
){
System
.
out
.
println
(
new
String
(
packet
.
getData
(),
0
,
packet
.
getLength
(),
StandardCharsets
.
UTF_8
));
}
public
void
send
(
DatagramPacket
packet
)
throws
IOException
{
this
.
localUDPSocket
.
send
(
packet
);
}
}
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