Skip to content

Commit dd5352b

Browse files
committed
add rpc-java server
1 parent 0da663e commit dd5352b

21 files changed

+4021
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.swp
2+
*.swo
3+
target
4+
.classpath
5+
.project
6+
.settings
7+
.svn
8+
output
9+
.idea
10+
*.iml
11+
*.pyc
12+
logs/
13+
*MANIFEST
14+
*artifacts
15+
.DS_Store

pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.wenweihu86.rpc</groupId>
6+
<artifactId>rpc-java</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>rpc-java</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>3.6.1</version>
23+
<configuration>
24+
<source>1.7</source>
25+
<target>1.7</target>
26+
<encoding>UTF-8</encoding>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>io.netty</groupId>
35+
<artifactId>netty-all</artifactId>
36+
<version>4.1.9.Final</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.protobuf</groupId>
40+
<artifactId>protobuf-java</artifactId>
41+
<version>3.2.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.slf4j</groupId>
45+
<artifactId>slf4j-api</artifactId>
46+
<version>1.7.25</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.logging.log4j</groupId>
50+
<artifactId>log4j-slf4j-impl</artifactId>
51+
<version>2.8.1</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.apache.logging.log4j</groupId>
55+
<artifactId>log4j-core</artifactId>
56+
<version>2.8.1</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.google.protobuf</groupId>
60+
<artifactId>protobuf-java</artifactId>
61+
<version>3.2.0</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.12</version>
67+
<scope>test</scope>
68+
</dependency>
69+
</dependencies>
70+
71+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.wenweihu86.rpc.codec.proto3;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.ByteToMessageDecoder;
6+
7+
import java.util.List;
8+
9+
/**
10+
* Created by wenweihu86 on 2017/4/25.
11+
*/
12+
public class ProtoV3Decoder extends ByteToMessageDecoder {
13+
14+
public static final int FIXED_LEN = 8;
15+
16+
@Override
17+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
18+
// 解决半包问题,此时头部8字节长度还没有接收全,channel中留存的字节流不做处理
19+
if (in.readableBytes() < FIXED_LEN) {
20+
return;
21+
}
22+
in.markReaderIndex();
23+
int headerLen = in.readInt();
24+
int bodyLen = in.readInt();
25+
// 解决半包问题,此时header和body还没有接收全,channel中留存的字节流不做处理,重置readerIndex
26+
if (in.readableBytes() < headerLen + bodyLen) {
27+
in.resetReaderIndex();
28+
return;
29+
}
30+
in.markReaderIndex();
31+
byte[] headBytes = new byte[headerLen];
32+
in.readBytes(headBytes, 0, headerLen);
33+
byte[] bodyBytes = new byte[bodyLen];
34+
in.readBytes(bodyBytes, 0, bodyLen);
35+
36+
ProtoV3Request request = new ProtoV3Request();
37+
ProtoV3Header.RequestHeader header = ProtoV3Header.RequestHeader.parseFrom(headBytes);
38+
request.setHeader(header);
39+
request.setBody(bodyBytes);
40+
41+
out.add(request);
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.wenweihu86.rpc.codec.proto3;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.ByteBufAllocator;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.handler.codec.MessageToMessageEncoder;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Created by wenweihu86 on 2017/4/25.
12+
*/
13+
public class ProtoV3Encoder extends MessageToMessageEncoder<ProtoV3Response> {
14+
15+
@Override
16+
protected void encode(ChannelHandlerContext ctx, ProtoV3Response response, List<Object> out)
17+
throws Exception {
18+
byte[] headerBytes = response.getHeader().toByteArray();
19+
int totalLength = 4 + 4 + headerBytes.length + response.getBody().length;
20+
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(totalLength);
21+
byteBuf.writeInt(headerBytes.length);
22+
byteBuf.writeInt(response.getBody().length);
23+
byteBuf.writeBytes(headerBytes);
24+
byteBuf.writeBytes(response.getBody());
25+
out.add(byteBuf);
26+
}
27+
}

0 commit comments

Comments
 (0)