connections);
41 |
42 | /**
43 | * Add a set of connections to monitor.
44 | *
45 | * The previous connections in monitor of this protocol,
46 | * will be dropped by monitor automatically.
47 | *
48 | * @param connPools connection pools
49 | */
50 | void monitor(Map> connPools);
51 | }
52 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/ConnectionSelectStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * Select strategy from connection pool
23 | *
24 | * @author xiaomin.cxm
25 | * @version $Id: ConnectionSelectStrategy.java, v 0.1 Mar 14, 2016 11:06:57 AM xiaomin.cxm Exp $
26 | */
27 | public interface ConnectionSelectStrategy {
28 | /**
29 | * select strategy
30 | *
31 | * @param connections source connections
32 | * @return selected connection
33 | */
34 | Connection select(List connections);
35 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/DefaultClientConnectionManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import com.alipay.remoting.connection.ConnectionFactory;
20 |
21 | /**
22 | * Do some preparatory work in order to refactor the ConnectionManager in the next version.
23 | *
24 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 14:27
25 | */
26 | public class DefaultClientConnectionManager extends DefaultConnectionManager implements
27 | ClientConnectionManager {
28 |
29 | public DefaultClientConnectionManager(ConnectionSelectStrategy connectionSelectStrategy,
30 | ConnectionFactory connectionFactory,
31 | ConnectionEventHandler connectionEventHandler,
32 | ConnectionEventListener connectionEventListener) {
33 | super(connectionSelectStrategy, connectionFactory, connectionEventHandler,
34 | connectionEventListener);
35 | }
36 |
37 | @Override
38 | public void startup() throws LifeCycleException {
39 | super.startup();
40 |
41 | this.connectionEventHandler.setConnectionManager(this);
42 | this.connectionEventHandler.setConnectionEventListener(connectionEventListener);
43 | this.connectionFactory.init(connectionEventHandler);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/DefaultServerConnectionManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * Do some preparatory work in order to refactor the ConnectionManager in the next version.
21 | *
22 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 14:40
23 | */
24 | public class DefaultServerConnectionManager extends DefaultConnectionManager implements
25 | ServerConnectionManager {
26 |
27 | public DefaultServerConnectionManager(ConnectionSelectStrategy connectionSelectStrategy) {
28 | super(connectionSelectStrategy);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/ExtendedNettyChannelHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import io.netty.channel.ChannelHandler;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | * Leave it to external expansion and
25 | * support the addition of extended handler in the channel pipeline.
26 | */
27 | public interface ExtendedNettyChannelHandler {
28 |
29 | /**
30 | * Netty ChannelHandlers to be added before Bolt's built-in Handler.
31 | * @return Netty ChannelHandler list
32 | */
33 | List frontChannelHandlers();
34 |
35 | /**
36 | * Netty ChannelHandlers to be added after Bolt's built-in Handler.
37 | * @return Netty ChannelHandler list
38 | */
39 | List backChannelHandlers();
40 | }
41 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/HeartbeatTrigger.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import io.netty.channel.ChannelHandlerContext;
20 |
21 | /**
22 | * Heartbeat triggers here.
23 | *
24 | * @author jiangping
25 | * @version $Id: HeartbeatTrigger.java, v 0.1 2015-12-14 PM3:40:38 tao Exp $
26 | */
27 | public interface HeartbeatTrigger {
28 | void heartbeatTriggered(final ChannelHandlerContext ctx) throws Exception;
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/InvokeCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import java.util.concurrent.Executor;
20 |
21 | /**
22 | * Invoke callback.
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeCallback.java, v 0.1 2015-9-30 AM10:24:26 tao Exp $
26 | */
27 | public interface InvokeCallback {
28 |
29 | /**
30 | * Response received.
31 | *
32 | * @param result
33 | */
34 | void onResponse(final Object result);
35 |
36 | /**
37 | * Exception caught.
38 | *
39 | * @param e
40 | */
41 | void onException(final Throwable e);
42 |
43 | /**
44 | * User defined executor.
45 | *
46 | * @return
47 | */
48 | Executor getExecutor();
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/InvokeCallbackListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * Listener to listen response and invoke callback.
21 | *
22 | * @author jiangping
23 | * @version $Id: InvokeCallbackListener.java, v 0.1 2015-9-21 PM5:17:08 tao Exp $
24 | */
25 | public interface InvokeCallbackListener {
26 | /**
27 | * Response arrived.
28 | *
29 | * @param future
30 | */
31 | void onResponse(final InvokeFuture future);
32 |
33 | /**
34 | * Get the remote address.
35 | *
36 | * @return
37 | */
38 | String getRemoteAddress();
39 | }
40 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/LifeCycle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 14:27
21 | */
22 | public interface LifeCycle {
23 |
24 | void startup() throws LifeCycleException;
25 |
26 | void shutdown() throws LifeCycleException;
27 |
28 | boolean isStarted();
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/LifeCycleException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 14:42
21 | */
22 | public class LifeCycleException extends RuntimeException {
23 |
24 | private static final long serialVersionUID = -5581833793111988391L;
25 |
26 | public LifeCycleException(String message) {
27 | super(message);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/Protocol.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * A protocol contains a group of commands.
21 | *
22 | * @author jiangping
23 | * @version $Id: Protocol.java, v 0.1 2015-12-11 PM5:02:48 tao Exp $
24 | */
25 | public interface Protocol {
26 | /**
27 | * Get the newEncoder for the protocol.
28 | *
29 | * @return
30 | */
31 | CommandEncoder getEncoder();
32 |
33 | /**
34 | * Get the decoder for the protocol.
35 | *
36 | * @return
37 | */
38 | CommandDecoder getDecoder();
39 |
40 | /**
41 | * Get the heartbeat trigger for the protocol.
42 | *
43 | * @return
44 | */
45 | HeartbeatTrigger getHeartbeatTrigger();
46 |
47 | /**
48 | * Get the command handler for the protocol.
49 | *
50 | * @return
51 | */
52 | CommandHandler getCommandHandler();
53 |
54 | /**
55 | * Get the command factory for the protocol.
56 | * @return
57 | */
58 | CommandFactory getCommandFactory();
59 | }
60 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/Reconnector.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * Reconnect manager interface.
21 | *
22 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 17:43
23 | */
24 | public interface Reconnector extends LifeCycle {
25 |
26 | /**
27 | * Do reconnecting in async mode.
28 | *
29 | * @param url target url
30 | */
31 | void reconnect(Url url);
32 |
33 | /**
34 | * Disable reconnect to the target url.
35 | *
36 | * @param url target url
37 | */
38 | void disableReconnect(Url url);
39 |
40 | /**
41 | * Enable reconnect to the target url.
42 | *
43 | * @param url target url
44 | */
45 | void enableReconnect(Url url);
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/RejectedExecutionPolicy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * RejectedExecutionPolicy determines how to deal with this situation that user executor rejected the {@link com.alipay.remoting.rpc.RpcInvokeCallbackListener.CallbackTask}.
21 | *
22 | * @author muyun
23 | * @version $Id: RejectedExecutionPolicy.java, v 0.1 2019年12月05日 7:38 PM muyun Exp $
24 | */
25 | public enum RejectedExecutionPolicy {
26 | /* discard the callback task */
27 | DISCARD,
28 | /* caller runs the callback in IO-thread */
29 | CALLER_RUNS,
30 | /* caller handle the task with exception strategy user provided */
31 | CALLER_HANDLE_EXCEPTION
32 | }
33 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/RejectionProcessableInvokeCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * InvokeCallback which support {@link RejectedExecutionPolicy} is able to process the task-rejected situation.
21 | *
22 | * @author muyun
23 | * @version $Id: RejectionProcessableInvokeCallback.java, v 0.1 2019年12月05日 9:16 PM muyun Exp $
24 | */
25 | public interface RejectionProcessableInvokeCallback extends InvokeCallback {
26 |
27 | /**
28 | * when user executor rejected the {@link com.alipay.remoting.rpc.RpcInvokeCallbackListener.CallbackTask},
29 | * bolt will handle the rejected task according to this {@link RejectedExecutionPolicy}
30 | * @return rejectedExecution Policy
31 | * @see com.alipay.remoting.rpc.RpcInvokeCallbackListener#onResponse(InvokeFuture)
32 | */
33 | RejectedExecutionPolicy rejectedExecutionPolicy();
34 | }
35 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/RemotingProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import java.util.concurrent.ExecutorService;
20 |
21 | /**
22 | * Remoting processor processes remoting commands.
23 | *
24 | * @author jiangping
25 | * @version $Id: RemotingProcessor.java, v 0.1 Dec 22, 2015 11:48:43 AM tao Exp $
26 | */
27 | public interface RemotingProcessor {
28 |
29 | /**
30 | * Process the remoting command.
31 | *
32 | * @param ctx
33 | * @param msg
34 | * @param defaultExecutor
35 | * @throws Exception
36 | */
37 | void process(RemotingContext ctx, T msg, ExecutorService defaultExecutor) throws Exception;
38 |
39 | /**
40 | * Get the executor.
41 | *
42 | * @return
43 | */
44 | ExecutorService getExecutor();
45 |
46 | /**
47 | * Set executor.
48 | *
49 | * @param executor
50 | */
51 | void setExecutor(ExecutorService executor);
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/Scannable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * Can be scanned.
21 | *
22 | * @author jiangping
23 | * @version $Id: Scannable.java, v 0.1 Mar 4, 2016 4:46:41 PM tao Exp $
24 | */
25 | public interface Scannable {
26 | /**
27 | * Scan it.
28 | */
29 | void scan();
30 | }
31 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/ServerConnectionManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | /**
20 | * Connection manager in server side.
21 | *
22 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 12:13
23 | */
24 | public interface ServerConnectionManager extends ConnectionManager, LifeCycle {
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/TimerHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting;
18 |
19 | import java.util.concurrent.TimeUnit;
20 |
21 | import io.netty.util.HashedWheelTimer;
22 | import io.netty.util.Timer;
23 |
24 | /**
25 | * A singleton holder of the timer for timeout.
26 | *
27 | * @author jiangping
28 | * @version $Id: TimerHolder.java, v 0.1 2015-09-28 2:02:20 tao Exp $
29 | */
30 | public class TimerHolder {
31 |
32 | private final static long defaultTickDuration = 10;
33 |
34 | private static class DefaultInstance {
35 | static final Timer INSTANCE = new HashedWheelTimer(new NamedThreadFactory(
36 | "DefaultTimer" + defaultTickDuration, true),
37 | defaultTickDuration, TimeUnit.MILLISECONDS);
38 | }
39 |
40 | private TimerHolder() {
41 | }
42 |
43 | /**
44 | * Get a singleton instance of {@link Timer}.
45 | * The tick duration is {@link #defaultTickDuration}.
46 | *
47 | * @return Timer
48 | */
49 | public static Timer getTimer() {
50 | return DefaultInstance.INSTANCE;
51 | }
52 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/codec/Codec.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.codec;
18 |
19 | import io.netty.channel.ChannelHandler;
20 |
21 | /**
22 | * Codec interface.
23 | *
24 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 21:07
25 | */
26 | public interface Codec {
27 |
28 | /**
29 | * Create an encoder instance.
30 | *
31 | * @return new encoder instance
32 | */
33 | ChannelHandler newEncoder();
34 |
35 | /**
36 | * Create an decoder instance.
37 | *
38 | * @return new decoder instance
39 | */
40 | ChannelHandler newDecoder();
41 | }
42 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/config/Configuration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.config;
18 |
19 | /**
20 | * Config interface.
21 | *
22 | * @author chengyi (mark.lx@antfin.com) 2018-11-06 14:46
23 | */
24 | public interface Configuration {
25 |
26 | /**
27 | * Get the option value.
28 | *
29 | * @param option target option
30 | * @return BoltOption
31 | */
32 | T option(BoltOption option);
33 |
34 | /**
35 | * Allow to specify a {@link BoltOption} which is used for the {@link Configuration} instances once they got
36 | * created. Use a value of {@code null} to remove a previous set {@link BoltOption}.
37 | *
38 | * @param option target option
39 | * @param value option value, null to remove the previous option
40 | * @return Configurable instance
41 | */
42 | Configuration option(BoltOption option, T value);
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/config/configs/ConfigItem.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.config.configs;
18 |
19 | /**
20 | * Items of config.
21 | *
22 | * Mainly used to define some config items managed by {@link ConfigContainer}.
23 | * You can define new config items based on this if need.
24 | *
25 | * @author tsui
26 | * @version $Id: ConfigItem.java, v 0.1 2018-07-28 17:43 tsui Exp $$
27 | */
28 | // fixme: remove in next version
29 | @Deprecated
30 | public enum ConfigItem {
31 | // ~~~ netty related
32 | @Deprecated
33 | NETTY_BUFFER_LOW_WATER_MARK, // netty writer buffer low water mark
34 | @Deprecated
35 | NETTY_BUFFER_HIGH_WATER_MARK // netty writer buffer high water mark
36 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/config/configs/ConfigType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.config.configs;
18 |
19 | /**
20 | * type of config
21 | *
22 | * @author tsui
23 | * @version $Id: ConfigType.java, v 0.1 2018-07-28 17:41 tsui Exp $$
24 | */
25 | @Deprecated
26 | public enum ConfigType {
27 | CLIENT_SIDE, // configs of this type can only be used in client side
28 | SERVER_SIDE // configs of this type can only be used in server side
29 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/config/switches/Switch.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.config.switches;
18 |
19 | /**
20 | * switch interface
21 | *
22 | * @author tsui
23 | * @version $Id: Switch.java, v 0.1 2018-04-08 11:26 tsui Exp $
24 | */
25 | public interface Switch {
26 | /**
27 | * api for user to turn on a feature
28 | *
29 | * @param index the switch index of feature
30 | */
31 | void turnOn(int index);
32 |
33 | /**
34 | * api for user to turn off a feature
35 | * @param index the switch index of feature
36 | */
37 | void turnOff(int index);
38 |
39 | /**
40 | * check switch whether on
41 | *
42 | * @param index the switch index of feature
43 | * @return true if either system setting is on or user setting is on
44 | */
45 | boolean isOn(int index);
46 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/connection/DefaultConnectionFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.connection;
18 |
19 | import com.alipay.remoting.codec.Codec;
20 | import com.alipay.remoting.config.Configuration;
21 |
22 | import io.netty.channel.ChannelHandler;
23 |
24 | /**
25 | * Default connection factory.
26 | *
27 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 15:18
28 | */
29 | public class DefaultConnectionFactory extends AbstractConnectionFactory {
30 |
31 | public DefaultConnectionFactory(Codec codec, ChannelHandler heartbeatHandler,
32 | ChannelHandler handler, Configuration configuration) {
33 | super(codec, heartbeatHandler, handler, configuration);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/constant/Constants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.constant;
18 |
19 | /**
20 | * Bolt Constants.
21 | *
22 | * @author chengyi (mark.lx@antfin.com) 2019-03-06 15:19
23 | */
24 | public class Constants {
25 |
26 | /**
27 | * default expire time to remove connection pool, time unit: milliseconds
28 | */
29 | public static final int DEFAULT_EXPIRE_TIME = 10 * 60 * 1000;
30 |
31 | /**
32 | * default retry times when failed to get result of FutureTask
33 | */
34 | public static final int DEFAULT_RETRY_TIMES = 2;
35 |
36 | public static final String SSL_HANDLER = "sslHandler";
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/exception/CodecException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.exception;
18 |
19 | /**
20 | * Exception when codec problems occur
21 | *
22 | * @author xiaomin.cxm
23 | * @version $Id: CodecException.java, v 0.1 2016-1-3 PM 6:26:12 xiaomin.cxm Exp $
24 | */
25 | public class CodecException extends RemotingException {
26 |
27 | /** For serialization */
28 | private static final long serialVersionUID = -7513762648815278960L;
29 |
30 | /**
31 | * Constructor.
32 | */
33 | public CodecException() {
34 | }
35 |
36 | /**
37 | * Constructor.
38 | *
39 | * @param message the detail message.
40 | */
41 | public CodecException(String message) {
42 | super(message);
43 | }
44 |
45 | /**
46 | * Constructor.
47 | *
48 | * @param message the detail message
49 | * @param cause the cause
50 | */
51 | public CodecException(String message, Throwable cause) {
52 | super(message, cause);
53 | }
54 |
55 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/exception/ConnectionClosedException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.exception;
18 |
19 | /**
20 | * Exception when connection is closed.
21 | *
22 | * @author jiangping
23 | * @version $Id: ConnectionClosedException.java, v 0.1 Jan 15, 2016 3:13:12 PM tao Exp $
24 | */
25 | public class ConnectionClosedException extends RemotingException {
26 |
27 | /** For serialization */
28 | private static final long serialVersionUID = -2595820033346329315L;
29 |
30 | /**
31 | * Default constructor.
32 | */
33 | public ConnectionClosedException() {
34 | }
35 |
36 | public ConnectionClosedException(String msg) {
37 | super(msg);
38 | }
39 |
40 | public ConnectionClosedException(String msg, Throwable cause) {
41 | super(msg, cause);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/exception/RemotingException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.exception;
18 |
19 | /**
20 | * Exception for default remoting problems
21 | *
22 | * @author jiangping
23 | * @version $Id: RemotingException.java, v 0.1 2015-9-21 PM 4:49:46 tao Exp $
24 | */
25 | public class RemotingException extends Exception {
26 |
27 | /** For serialization */
28 | private static final long serialVersionUID = 6183635628271812505L;
29 |
30 | /**
31 | * Constructor.
32 | */
33 | public RemotingException() {
34 |
35 | }
36 |
37 | /**
38 | * Constructor.
39 | */
40 | public RemotingException(String message) {
41 | super(message);
42 | }
43 |
44 | /**
45 | * Constructor.
46 | */
47 | public RemotingException(String message, Throwable cause) {
48 | super(message, cause);
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/log/BoltLoggerFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.log;
18 |
19 | import org.slf4j.Logger;
20 |
21 | import org.slf4j.LoggerFactory;
22 |
23 | /**
24 | * Customized logger factory
25 | *
26 | * This can use middleware-log in sofa-common-tools to detect specific log implementation and initialize with the given log template.
27 | *
28 | * @author tsui
29 | * @version $Id: BoltLoggerFactory.java, v 0.1 2017-09-05 16:06 tsui Exp $
30 | */
31 | public class BoltLoggerFactory {
32 | public static Logger getLogger(Class> clazz) {
33 | if (clazz == null) {
34 | return getLogger("");
35 | }
36 | return getLogger(clazz.getCanonicalName());
37 | }
38 |
39 | public static Logger getLogger(String name) {
40 | if (name == null || name.isEmpty()) {
41 | return LoggerFactory.getLogger("");
42 | }
43 | return LoggerFactory.getLogger(name);
44 | }
45 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/HeartbeatAckCommand.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | import com.alipay.remoting.CommonCommandCode;
20 | import com.alipay.remoting.ResponseStatus;
21 |
22 | /**
23 | * Heartbeat ack.
24 | *
25 | * @author jiangping
26 | * @version $Id: HeartbeatAckCommand.java, v 0.1 2015-9-29 AM11:46:11 tao Exp $
27 | */
28 | public class HeartbeatAckCommand extends ResponseCommand {
29 | /** For serialization */
30 | private static final long serialVersionUID = 2584912495844320855L;
31 |
32 | /**
33 | * Constructor.
34 | */
35 | public HeartbeatAckCommand() {
36 | super(CommonCommandCode.HEARTBEAT);
37 | this.setResponseStatus(ResponseStatus.SUCCESS);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/HeartbeatCommand.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | import com.alipay.remoting.CommonCommandCode;
20 | import com.alipay.remoting.util.IDGenerator;
21 |
22 | /**
23 | * Heart beat.
24 | *
25 | * @author jiangping
26 | * @version $Id: HeartbeatCommand.java, v 0.1 2015-9-10 AM9:46:36 tao Exp $
27 | */
28 | public class HeartbeatCommand extends RequestCommand {
29 |
30 | /** For serialization */
31 | private static final long serialVersionUID = 4949981019109517725L;
32 |
33 | /**
34 | * Construction.
35 | */
36 | public HeartbeatCommand() {
37 | super(CommonCommandCode.HEARTBEAT);
38 | this.setId(IDGenerator.nextId());
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/RpcCodec.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | import com.alipay.remoting.ProtocolCode;
20 | import com.alipay.remoting.codec.Codec;
21 | import com.alipay.remoting.codec.ProtocolCodeBasedEncoder;
22 | import com.alipay.remoting.rpc.protocol.RpcProtocolDecoder;
23 | import com.alipay.remoting.rpc.protocol.RpcProtocolManager;
24 | import com.alipay.remoting.rpc.protocol.RpcProtocolV2;
25 |
26 | import io.netty.channel.ChannelHandler;
27 |
28 | /**
29 | * @author muyun.cyt
30 | * @version 2018/6/26 下午3:51
31 | */
32 | public class RpcCodec implements Codec {
33 |
34 | @Override
35 | public ChannelHandler newEncoder() {
36 | return new ProtocolCodeBasedEncoder(ProtocolCode.fromBytes(RpcProtocolV2.PROTOCOL_CODE));
37 | }
38 |
39 | @Override
40 | public ChannelHandler newDecoder() {
41 | return new RpcProtocolDecoder(RpcProtocolManager.DEFAULT_PROTOCOL_CODE_LENGTH);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/RpcCommandType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | /**
20 | * The type of command in the request/response model.
21 | *
22 | * @author jiangping
23 | * @version $Id: RpcCommandType.java, v 0.1 2015-9-25 AM10:58:16 tao Exp $
24 | */
25 | public class RpcCommandType {
26 | /** rpc response */
27 | public static final byte RESPONSE = (byte) 0x00;
28 | /** rpc request */
29 | public static final byte REQUEST = (byte) 0x01;
30 | /** rpc oneway request */
31 | public static final byte REQUEST_ONEWAY = (byte) 0x02;
32 | }
33 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/RpcConnectionEventHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | import com.alipay.remoting.Connection;
20 | import com.alipay.remoting.ConnectionEventHandler;
21 | import com.alipay.remoting.config.Configuration;
22 |
23 | import io.netty.channel.ChannelHandlerContext;
24 |
25 | /**
26 | * ConnectionEventHandler for Rpc.
27 | *
28 | * @author jiangping
29 | * @version $Id: RpcConnectionEventHandler.java, v 0.1 2015-10-16 PM4:41:29 tao Exp $
30 | */
31 | public class RpcConnectionEventHandler extends ConnectionEventHandler {
32 |
33 | public RpcConnectionEventHandler() {
34 | super();
35 | }
36 |
37 | public RpcConnectionEventHandler(Configuration configuration) {
38 | super(configuration);
39 | }
40 |
41 | /**
42 | * @see com.alipay.remoting.ConnectionEventHandler#channelInactive(io.netty.channel.ChannelHandlerContext)
43 | */
44 | @Override
45 | public void channelInactive(ChannelHandlerContext ctx) throws Exception {
46 | Connection conn = ctx.channel().attr(Connection.CONNECTION).get();
47 | if (conn != null) {
48 | this.getConnectionManager().remove(conn);
49 | }
50 | super.channelInactive(ctx);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/RpcConnectionFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc;
18 |
19 | import java.util.concurrent.ConcurrentHashMap;
20 |
21 | import com.alipay.remoting.config.Configuration;
22 | import com.alipay.remoting.connection.DefaultConnectionFactory;
23 | import com.alipay.remoting.rpc.protocol.UserProcessor;
24 |
25 | /**
26 | * Default RPC connection factory impl.
27 | *
28 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 15:32
29 | */
30 | public class RpcConnectionFactory extends DefaultConnectionFactory {
31 |
32 | public RpcConnectionFactory(ConcurrentHashMap> userProcessors,
33 | Configuration configurations) {
34 | super(new RpcCodec(), new HeartbeatHandler(), new RpcHandler(userProcessors),
35 | configurations);
36 | }
37 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/InvokeException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Exception when invoke failed
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeException.java, v 0.1 2015-10-5 PM8:19:36 tao Exp $
26 | */
27 | public class InvokeException extends RemotingException {
28 | /** For serialization */
29 | private static final long serialVersionUID = -3974514863386363570L;
30 |
31 | /**
32 | * Default constructor.
33 | */
34 | public InvokeException() {
35 | }
36 |
37 | public InvokeException(String msg) {
38 | super(msg);
39 | }
40 |
41 | public InvokeException(String msg, Throwable cause) {
42 | super(msg, cause);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/InvokeSendFailedException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Exception when invoke send failed
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeSendFailedException.java, v 0.1 2015-10-3 PM 12:32:45 tao Exp $
26 | */
27 | public class InvokeSendFailedException extends RemotingException {
28 |
29 | /** For serialization */
30 | private static final long serialVersionUID = 4832257777758730796L;
31 |
32 | /**
33 | * Default constructor.
34 | */
35 | public InvokeSendFailedException() {
36 | }
37 |
38 | public InvokeSendFailedException(String msg) {
39 | super(msg);
40 | }
41 |
42 | public InvokeSendFailedException(String msg, Throwable cause) {
43 | super(msg, cause);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/InvokeServerBusyException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Exception when thread pool busy of process server
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeServerBusyException.java, v 0.1 2015-10-9 AM11:16:10 tao Exp $
26 | */
27 | public class InvokeServerBusyException extends RemotingException {
28 | /** For serialization */
29 | private static final long serialVersionUID = 4480283862377034355L;
30 |
31 | /**
32 | * Default constructor.
33 | */
34 | public InvokeServerBusyException() {
35 | }
36 |
37 | public InvokeServerBusyException(String msg) {
38 | super(msg);
39 | }
40 |
41 | public InvokeServerBusyException(String msg, Throwable cause) {
42 | super(msg, cause);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/InvokeServerException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Server exception caught when invoking
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeServerException.java, v 0.1 2015-10-9 AM11:16:10 tao Exp $
26 | */
27 | public class InvokeServerException extends RemotingException {
28 | /** For serialization */
29 | private static final long serialVersionUID = 4480283862377034355L;
30 |
31 | /**
32 | * Default constructor.
33 | */
34 | public InvokeServerException() {
35 | }
36 |
37 | public InvokeServerException(String msg) {
38 | super(msg);
39 | }
40 |
41 | public InvokeServerException(String msg, Throwable cause) {
42 | super(msg, cause);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/InvokeTimeoutException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Exception when invoke timeout
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeTimeoutException.java, v 0.1 2015-9-28 PM3:35:53 tao Exp $
26 | */
27 | public class InvokeTimeoutException extends RemotingException {
28 |
29 | /** For serialization */
30 | private static final long serialVersionUID = -7772633244795043476L;
31 |
32 | /**
33 | * Default constructor.
34 | */
35 | public InvokeTimeoutException() {
36 | }
37 |
38 | /**
39 | * Constructor.
40 | *
41 | * @param msg the detail message
42 | */
43 | public InvokeTimeoutException(String msg) {
44 | super(msg);
45 | }
46 |
47 | /**
48 | * Constructor.
49 | *
50 | * @param msg the detail message
51 | * @param cause the cause
52 | */
53 | public InvokeTimeoutException(String msg, Throwable cause) {
54 | super(msg, cause);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/exception/RpcServerException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.exception;
18 |
19 | import com.alipay.remoting.exception.RemotingException;
20 |
21 | /**
22 | * Rpc server exception when processing request
23 | *
24 | * @author jiangping
25 | * @version $Id: InvokeServerException.java, v 0.1 2015-10-9 PM11:16:10 tao Exp $
26 | */
27 | public class RpcServerException extends RemotingException {
28 | /** For serialization */
29 | private static final long serialVersionUID = 4480283862377034355L;
30 |
31 | /**
32 | * Default constructor.
33 | */
34 | public RpcServerException() {
35 | }
36 |
37 | public RpcServerException(String msg) {
38 | super(msg);
39 | }
40 |
41 | public RpcServerException(String msg, Throwable cause) {
42 | super(msg, cause);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/AbstractMultiInterestUserProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | /**
20 | * Implements common function and provide default value.
21 | * more details in {@link com.alipay.remoting.rpc.protocol.AbstractUserProcessor}
22 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:17 AM
23 | */
24 | public abstract class AbstractMultiInterestUserProcessor extends AbstractUserProcessor
25 | implements
26 | MultiInterestUserProcessor {
27 |
28 | /**
29 | * do not need to implement this method because of the multiple interests
30 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#interest()
31 | * */
32 | @Override
33 | public String interest() {
34 | return null;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/MultiInterestUserProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * Support multi-interests feature based on UserProcessor
23 | *
24 | * The implementations of this interface don't need to implement the {@link com.alipay.remoting.rpc.protocol.UserProcessor#interest() interest()} method;
25 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:19 AM
26 | */
27 | public interface MultiInterestUserProcessor extends UserProcessor {
28 |
29 | /**
30 | * A list of the class names of user request.
31 | * Use String type to avoid classloader problem.
32 | */
33 | List multiInterest();
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/RpcCommandCode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | import com.alipay.remoting.CommandCode;
20 |
21 | /**
22 | * Command code for rpc remoting command.
23 | * @author jiangping
24 | * @version $Id: RpcCommandCode.java, v 0.1 2015-9-21 PM5:05:59 tao Exp $
25 | */
26 | public enum RpcCommandCode implements CommandCode {
27 |
28 | RPC_REQUEST((short) 1), RPC_RESPONSE((short) 2);
29 |
30 | private short value;
31 |
32 | RpcCommandCode(short value) {
33 | this.value = value;
34 | }
35 |
36 | @Override
37 | public short value() {
38 | return this.value;
39 | }
40 |
41 | public static RpcCommandCode valueOf(short value) {
42 | switch (value) {
43 | case 1:
44 | return RPC_REQUEST;
45 | case 2:
46 | return RPC_RESPONSE;
47 | }
48 | throw new IllegalArgumentException("Unknown Rpc command code value: " + value);
49 | }
50 |
51 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/RpcDeserializeLevel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | /**
20 | * Rpc deserialize level.
21 | *
22 | * @author tsui
23 | * @version $Id: RpcDeserializeLevel.java, v 0.1 2017-04-24 15:12 tsui Exp $
24 | */
25 | public class RpcDeserializeLevel {
26 | /** deserialize clazz, header, contents all three parts of rpc command */
27 | public final static int DESERIALIZE_ALL = 0x02;
28 | /** deserialize both header and clazz parts of rpc command */
29 | public final static int DESERIALIZE_HEADER = 0x01;
30 | /** deserialize only the clazz part of rpc command */
31 | public final static int DESERIALIZE_CLAZZ = 0x00;
32 |
33 | /**
34 | * Convert to String.
35 | */
36 | public static String valueOf(int value) {
37 | switch (value) {
38 | case 0x00:
39 | return "DESERIALIZE_CLAZZ";
40 | case 0x01:
41 | return "DESERIALIZE_HEADER";
42 | case 0x02:
43 | return "DESERIALIZE_ALL";
44 | }
45 | throw new IllegalArgumentException("Unknown deserialize level value ," + value);
46 | }
47 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/RpcProtocolDecoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | import com.alipay.remoting.codec.ProtocolCodeBasedDecoder;
20 |
21 | import io.netty.buffer.ByteBuf;
22 |
23 | /**
24 | * Rpc protocol decoder.
25 | *
26 | * @author tsui
27 | * @version $Id: RpcProtocolDecoder.java, v 0.1 2018-03-27 19:28 tsui Exp $
28 | */
29 | public class RpcProtocolDecoder extends ProtocolCodeBasedDecoder {
30 | public static final int MIN_PROTOCOL_CODE_WITH_VERSION = 2;
31 |
32 | public RpcProtocolDecoder(int protocolCodeLength) {
33 | super(protocolCodeLength);
34 | }
35 |
36 | @Override
37 | protected byte decodeProtocolVersion(ByteBuf in) {
38 | in.resetReaderIndex();
39 | if (in.readableBytes() >= protocolCodeLength + DEFAULT_PROTOCOL_VERSION_LENGTH) {
40 | byte rpcProtocolCodeByte = in.readByte();
41 | if (rpcProtocolCodeByte >= MIN_PROTOCOL_CODE_WITH_VERSION) {
42 | return in.readByte();
43 | }
44 | }
45 | return DEFAULT_ILLEGAL_PROTOCOL_VERSION_LENGTH;
46 | }
47 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/rpc/protocol/RpcProtocolManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.rpc.protocol;
18 |
19 | import com.alipay.remoting.ProtocolManager;
20 |
21 | /**
22 | * Protocol manager.
23 | *
24 | * @author tsui
25 | * @version $Id: RpcProtocols.java, v 0.1 2018-03-27 19:42 tsui Exp $
26 | */
27 | public class RpcProtocolManager {
28 | public static final int DEFAULT_PROTOCOL_CODE_LENGTH = 1;
29 |
30 | public static void initProtocols() {
31 | ProtocolManager.registerProtocol(new RpcProtocol(), RpcProtocol.PROTOCOL_CODE);
32 | ProtocolManager.registerProtocol(new RpcProtocolV2(), RpcProtocolV2.PROTOCOL_CODE);
33 | }
34 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/serialization/Serializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.serialization;
18 |
19 | import com.alipay.remoting.exception.CodecException;
20 |
21 | /**
22 | * Serializer for serialize and deserialize.
23 | *
24 | * @author jiangping
25 | * @version $Id: Serializer.java, v 0.1 2015-10-4 PM9:37:57 tao Exp $
26 | */
27 | public interface Serializer {
28 | /**
29 | * Encode object into bytes.
30 | *
31 | * @param obj target object
32 | * @return serialized result
33 | */
34 | byte[] serialize(final Object obj) throws CodecException;
35 |
36 | /**
37 | * Decode bytes into Object.
38 | *
39 | * @param data serialized data
40 | * @param classOfT class of original data
41 | */
42 | T deserialize(final byte[] data, String classOfT) throws CodecException;
43 | }
44 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/FutureTaskNotCompleted.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | /**
20 | * @author chengyi (mark.lx@antfin.com) 2018-10-18 16:30
21 | */
22 | public class FutureTaskNotCompleted extends Exception {
23 |
24 | private static final long serialVersionUID = -3635466558774380138L;
25 |
26 | public FutureTaskNotCompleted() {
27 | }
28 |
29 | public FutureTaskNotCompleted(String message) {
30 | super(message);
31 | }
32 |
33 | public FutureTaskNotCompleted(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/FutureTaskNotRunYetException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | /**
20 | * Exception to represent the run method of a future task has not been called.
21 | *
22 | * @author tsui
23 | * @version $Id: FutureTaskNotRunYetException.java, v 0.1 2017-07-31 16:29 tsui Exp $
24 | */
25 | public class FutureTaskNotRunYetException extends Exception {
26 | /** For serialization */
27 | private static final long serialVersionUID = 2929126204324060632L;
28 |
29 | /**
30 | * Constructor.
31 | */
32 | public FutureTaskNotRunYetException() {
33 | }
34 |
35 | /**
36 | * Constructor.
37 | */
38 | public FutureTaskNotRunYetException(String message) {
39 | super(message);
40 | }
41 |
42 | /**
43 | * Constructor.
44 | */
45 | public FutureTaskNotRunYetException(String message, Throwable cause) {
46 | super(message, cause);
47 | }
48 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/IDGenerator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | import java.util.concurrent.atomic.AtomicInteger;
20 |
21 | /**
22 | * IDGenerator is used for generating request id in integer form.
23 | *
24 | * @author jiangping
25 | * @version $Id: IDGenerator.java, v 0.1 2015-9-23 PM5:28:58 tao Exp $
26 | */
27 | public class IDGenerator {
28 | private static final AtomicInteger id = new AtomicInteger(0);
29 |
30 | /**
31 | * generate the next id
32 | *
33 | * @return
34 | */
35 | public static int nextId() {
36 | return id.incrementAndGet();
37 | }
38 |
39 | public static void resetId() {
40 | id.set(0);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/IoUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | import java.io.Closeable;
20 | import java.io.IOException;
21 |
22 | /**
23 | * IO Utilities
24 | *
25 | * @author boyan(boyan@antfin.com)
26 | *
27 | */
28 | public class IoUtils {
29 | public static void closeQuietly(Closeable closeable) {
30 | try {
31 | if (closeable != null) {
32 | closeable.close();
33 | }
34 | } catch (IOException e) { // NOPMD
35 | // ignore
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/RunStateRecordedFutureTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | import java.util.concurrent.Callable;
20 | import java.util.concurrent.ExecutionException;
21 | import java.util.concurrent.FutureTask;
22 | import java.util.concurrent.atomic.AtomicBoolean;
23 |
24 | /**
25 | * A customized FutureTask which can record whether the run method has been called.
26 | * @author tsui
27 | * @version $Id: RunStateRecordedFutureTask.java, v 0.1 2017-07-31 16:28 tsui Exp $
28 | */
29 | public class RunStateRecordedFutureTask extends FutureTask {
30 | private AtomicBoolean hasRun = new AtomicBoolean();
31 |
32 | public RunStateRecordedFutureTask(Callable callable) {
33 | super(callable);
34 | }
35 |
36 | @Override
37 | public void run() {
38 | this.hasRun.set(true);
39 | super.run();
40 | }
41 |
42 | public V getAfterRun() throws InterruptedException, ExecutionException,
43 | FutureTaskNotRunYetException, FutureTaskNotCompleted {
44 | if (!hasRun.get()) {
45 | throw new FutureTaskNotRunYetException();
46 | }
47 |
48 | if (!isDone()) {
49 | throw new FutureTaskNotCompleted();
50 | }
51 |
52 | return super.get();
53 | }
54 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/com/alipay/remoting/util/TraceLogUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alipay.remoting.util;
18 |
19 | import org.slf4j.Logger;
20 |
21 | import com.alipay.remoting.InvokeContext;
22 |
23 | /**
24 | * Trace log util
25 | *
26 | * @author tsui
27 | * @version $Id: TraceLogUtil.java, v 0.1 2016-08-02 17:31 tsui Exp $
28 | */
29 | public class TraceLogUtil {
30 | /**
31 | * print trace log
32 | * @param traceId
33 | * @param invokeContext
34 | */
35 | public static void printConnectionTraceLog(Logger logger, String traceId,
36 | InvokeContext invokeContext) {
37 | String sourceIp = invokeContext.get(InvokeContext.CLIENT_LOCAL_IP);
38 | Integer sourcePort = invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT);
39 | String targetIp = invokeContext.get(InvokeContext.CLIENT_REMOTE_IP);
40 | Integer targetPort = invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT);
41 | StringBuilder logMsg = new StringBuilder();
42 | logMsg.append(traceId).append(",");
43 | logMsg.append(sourceIp).append(",");
44 | logMsg.append(sourcePort).append(",");
45 | logMsg.append(targetIp).append(",");
46 | logMsg.append(targetPort);
47 | if (logger.isInfoEnabled()) {
48 | logger.info(logMsg.toString());
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/BrokerClientType.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core;
2 |
3 | /**
4 | * 客户端类型
5 | *
6 | * @author Nipuru
7 | * @since 2022/7/30 16:15
8 | */
9 | public interface BrokerClientType {
10 |
11 | /** BungeeCord 客户端 */
12 | String PROXY = "proxy";
13 |
14 | /** Bukkit 客户端 */
15 | String SERVER = "server";
16 |
17 | /** 未知类型的客户端 */
18 | String UNKNOWN = "unknown";
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/BrokerGlobalConfig.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core;
2 |
3 | import lombok.experimental.UtilityClass;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2022/7/30 16:43
8 | */
9 | @UtilityClass
10 | public class BrokerGlobalConfig {
11 |
12 | /**
13 | * broker 默认地址
14 | */
15 | public final String BROKER_HOST = "localhost";
16 |
17 | /**
18 | * broker 默认端口
19 | */
20 | public final int BROKER_PORT = 11200;
21 |
22 | /**
23 | * bolt 默认消息发送超时时间
24 | */
25 | public final int DEFAULT_TIMEOUT_MILLIS = 3000;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/FileConfig.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * 抽象配置文件类
7 | *
8 | * @author Nipuru
9 | * @since 2022/7/28 17:43
10 | */
11 | public abstract class FileConfig {
12 |
13 |
14 | private final File file;
15 | private final String name;
16 |
17 | public FileConfig(File file, String name) {
18 | this.file = file;
19 | this.name = name;
20 | }
21 |
22 | /**
23 | * 获取配置文件File对象
24 | *
25 | * @return file
26 | */
27 | public File getFile() {
28 | return file;
29 | }
30 |
31 | public String getName() {
32 | return name;
33 | }
34 |
35 | /**
36 | * 保存配置文件
37 | */
38 | public abstract void save();
39 |
40 | /**
41 | * 读取配置文件
42 | */
43 | public abstract void reload();
44 |
45 | /**
46 | * 获取可使用的配置
47 | * bukkit: ConfigurationSection
48 | * bungee: Configuration
49 | *
50 | * @return config
51 | */
52 | public abstract T get();
53 | }
54 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/MetadataKeys.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core;
2 |
3 | public interface MetadataKeys {
4 | String MC_SERVER_ADDRESS = "mcServerAddress";
5 | }
6 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/BroadcastChatMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2022/8/10 11:23
15 | */
16 | @Getter
17 | @Setter
18 | @ToString
19 | @Accessors(chain = true)
20 | @FieldDefaults(level = AccessLevel.PRIVATE)
21 | public class BroadcastChatMessage implements Serializable {
22 | private static final long serialVersionUID = -4901406795508836396L;
23 |
24 | /**
25 | * 消息
26 | */
27 | String message;
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/BrokerClientInfoMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 | import net.afyer.afybroker.core.BrokerClientInfo;
10 |
11 | import java.io.Serializable;
12 | import java.util.Map;
13 | import java.util.Set;
14 |
15 | /**
16 | * @author Nipuru
17 | * @since 2022/7/30 16:25
18 | */
19 | @Getter
20 | @Setter
21 | @ToString
22 | @Accessors(chain = true)
23 | @FieldDefaults(level = AccessLevel.PRIVATE)
24 | public class BrokerClientInfoMessage implements Serializable {
25 | private static final long serialVersionUID = 5964124139341528361L;
26 |
27 | /** 客户端名称(唯一标识) */
28 | String name;
29 | /** 客户端标签 */
30 | Set tags;
31 | /** 客户端类型 */
32 | String type;
33 | /** 服务器/客户端 地址 */
34 | String address;
35 | /** 客户端元数据 */
36 | Map metadata;
37 |
38 | public BrokerClientInfo build() {
39 | return new BrokerClientInfo(name, tags, type, address, metadata);
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/ConnectToServerMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * 让玩家连接到某个 minecraft 服务器
15 | *
16 | * @author Nipuru
17 | * @since 2022/9/6 17:33
18 | */
19 | @Getter
20 | @Setter
21 | @ToString
22 | @Accessors(chain = true)
23 | @FieldDefaults(level = AccessLevel.PRIVATE)
24 | public class ConnectToServerMessage implements Serializable {
25 | private static final long serialVersionUID = -2031147618861482881L;
26 |
27 | /** 玩家uuid */
28 | UUID uniqueId;
29 |
30 | /** minecraft 服务器名(在 proxy 中的名字) */
31 | String serverName;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/ForwardingMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 |
12 | /**
13 | * 用于封装消息 由client发送 然后由server转发给指定名称的client
14 | * @author Nipuru
15 | * @since 2022/9/4 18:15
16 | */
17 | @Getter
18 | @Setter
19 | @ToString
20 | @Accessors(chain = true)
21 | @FieldDefaults(level = AccessLevel.PRIVATE)
22 | public abstract class ForwardingMessage implements Serializable {
23 | private static final long serialVersionUID = 7011303487498190975L;
24 |
25 | /** 目标broker client 名称 */
26 | String clientName;
27 |
28 | /** 封装的消息 */
29 | Serializable message;
30 | }
31 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/KickPlayerMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * 踢出玩家消息
15 | *
16 | * @author Nipuru
17 | * @since 2022/10/10 10:30
18 | */
19 | @Getter
20 | @Setter
21 | @ToString
22 | @Accessors(chain = true)
23 | @FieldDefaults(level = AccessLevel.PRIVATE)
24 | public class KickPlayerMessage implements Serializable {
25 | private static final long serialVersionUID = 225514412094976346L;
26 |
27 | /** 玩家名 */
28 | UUID uniqueId;
29 |
30 | /** 踢出消息 */
31 | String message;
32 | }
33 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerHeartbeatValidateMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.List;
12 | import java.util.UUID;
13 |
14 | /**
15 | * 玩家心跳验证
16 | *
17 | * @author Nipuru
18 | * @since 2023/11/25 12:31
19 | */
20 | @Getter
21 | @Setter
22 | @ToString
23 | @Accessors(chain = true)
24 | @FieldDefaults(level = AccessLevel.PRIVATE)
25 | public class PlayerHeartbeatValidateMessage implements Serializable {
26 | private static final long serialVersionUID = -3464928414600404140L;
27 |
28 | /** 需要验证的玩家集合 */
29 | List uniqueIdList;
30 | }
31 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerProfilePropertyMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 |
4 | import lombok.AccessLevel;
5 | import lombok.Getter;
6 | import lombok.Setter;
7 | import lombok.ToString;
8 | import lombok.experimental.Accessors;
9 | import lombok.experimental.FieldDefaults;
10 |
11 | import java.io.Serializable;
12 | import java.util.*;
13 |
14 | /**
15 | * @author Nipuru
16 | * @since 2024/12/03 10:14
17 | */
18 | @Getter
19 | @Setter
20 | @ToString
21 | @Accessors(chain = true)
22 | @FieldDefaults(level = AccessLevel.PRIVATE)
23 | public class PlayerProfilePropertyMessage implements Serializable {
24 | /** 玩家uuid */
25 | UUID uniqueId;
26 | /** 需要移除的 Property */
27 | List removeList;
28 | /** 需要更新的 Property */
29 | Map updateMap;
30 |
31 | public PlayerProfilePropertyMessage remove(String name) {
32 | if (removeList == null) {
33 | removeList = new ArrayList<>();
34 | }
35 | removeList.add(name);
36 | return this;
37 | }
38 |
39 | public PlayerProfilePropertyMessage update(String name, String value, String signature) {
40 | if (updateMap == null) {
41 | updateMap = new HashMap<>();
42 | }
43 | updateMap.put(name, new String[]{value, signature});
44 | return this;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerProxyConnectMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * 玩家连接至 proxy 的消息
15 | *
16 | * @author Nipuru
17 | * @since 2022/8/1 11:32
18 | */
19 | @Getter
20 | @Setter
21 | @ToString
22 | @Accessors(chain = true)
23 | @FieldDefaults(level = AccessLevel.PRIVATE)
24 | public class PlayerProxyConnectMessage implements Serializable {
25 | private static final long serialVersionUID = 1791475059445212432L;
26 |
27 | /** 玩家uuid */
28 | UUID uniqueId;
29 |
30 | /** 玩家名 */
31 | String name;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerProxyDisconnectMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * 玩家从 proxy 断开连接的消息
15 | * @author Nipuru
16 | * @since 2022/11/21 17:30
17 | */
18 | @Getter
19 | @Setter
20 | @ToString
21 | @Accessors(chain = true)
22 | @FieldDefaults(level = AccessLevel.PRIVATE)
23 | public class PlayerProxyDisconnectMessage implements Serializable {
24 | private static final long serialVersionUID = -5160344925177364814L;
25 |
26 | /** 玩家uuid */
27 | UUID uniqueId;
28 |
29 | /** 玩家名 */
30 | String name;
31 | }
32 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerServerConnectedMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * 玩家与 minecraft 服务器连接状态变化的消息
15 | * @author Nipuru
16 | * @since 2022/9/12 12:21
17 | */
18 | @Getter
19 | @Setter
20 | @ToString
21 | @Accessors(chain = true)
22 | @FieldDefaults(level = AccessLevel.PRIVATE)
23 | public class PlayerServerConnectedMessage implements Serializable {
24 | private static final long serialVersionUID = 5436035428469761938L;
25 |
26 | /** 玩家uuid */
27 | UUID uniqueId;
28 |
29 | /** 玩家名 */
30 | String name;
31 |
32 | /** 服务器名 */
33 | String serverName;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/PlayerServerJoinMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2023/09/29 12:04
16 | */
17 | @Getter
18 | @Setter
19 | @ToString
20 | @Accessors(chain = true)
21 | @FieldDefaults(level = AccessLevel.PRIVATE)
22 | public class PlayerServerJoinMessage implements Serializable {
23 | private static final long serialVersionUID = -1132388839270494188L;
24 |
25 | /** 玩家uuid */
26 | UUID uniqueId;
27 |
28 | /** 玩家名 */
29 | String name;
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/RequestBrokerClientInfoMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2022/7/30 17:17
8 | */
9 | public class RequestBrokerClientInfoMessage implements Serializable {
10 | private static final long serialVersionUID = -2061260713419444821L;
11 | }
12 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/RequestPlayerInfoMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2024/7/24 00:58
8 | */
9 | public class RequestPlayerInfoMessage implements Serializable {
10 | private static final long serialVersionUID = -227215016466238678L;
11 | }
12 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/SendPlayerMessageMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.UUID;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2022/8/5 10:04
16 | */
17 | @Getter
18 | @Setter
19 | @ToString
20 | @Accessors(chain = true)
21 | @FieldDefaults(level = AccessLevel.PRIVATE)
22 | public class SendPlayerMessageMessage implements Serializable {
23 | private static final long serialVersionUID = -4207075992906096144L;
24 |
25 | /**
26 | * 玩家 uniqueId
27 | */
28 | UUID uniqueId;
29 |
30 | /**
31 | * 消息
32 | */
33 | String message;
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/SendPlayerTitleMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2022/8/11 8:55
15 | */
16 | @Getter
17 | @Setter
18 | @ToString
19 | @Accessors(chain = true)
20 | @FieldDefaults(level = AccessLevel.PRIVATE)
21 | public class SendPlayerTitleMessage implements Serializable {
22 | private static final long serialVersionUID = 2892294285923120071L;
23 |
24 | /**
25 | * 玩家名
26 | */
27 | String name;
28 |
29 | /**
30 | * title
31 | */
32 | String title;
33 |
34 | /**
35 | * subtitle
36 | */
37 | String subtitle;
38 |
39 | /**
40 | * 淡入
41 | */
42 | int fadein;
43 |
44 | /** 停留时间 */
45 | int stay;
46 |
47 | /** 淡出 */
48 | int fadeout;
49 | }
50 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/message/SyncServerMessage.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.message;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.Setter;
6 | import lombok.ToString;
7 | import lombok.experimental.Accessors;
8 | import lombok.experimental.FieldDefaults;
9 |
10 | import java.io.Serializable;
11 | import java.util.Map;
12 |
13 | /**
14 | * broker-server 发出,由 proxy 类型的服务器处理
15 | */
16 | @Getter
17 | @Setter
18 | @ToString
19 | @Accessors(chain = true)
20 | @FieldDefaults(level = AccessLevel.PRIVATE)
21 | public class SyncServerMessage implements Serializable {
22 |
23 | /** 在线的 mc 服务器列表, key:name, value:address */
24 | Map servers;
25 | }
26 |
--------------------------------------------------------------------------------
/afybroker-core/src/main/java/net/afyer/afybroker/core/util/AbstractInvokeCallback.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.core.util;
2 |
3 | import com.alipay.remoting.InvokeCallback;
4 |
5 | import java.util.concurrent.Executor;
6 |
7 | /**
8 | * @author Nipuru
9 | * @since 2022/12/18 17:32
10 | */
11 | public abstract class AbstractInvokeCallback implements InvokeCallback {
12 |
13 | @Override
14 | public void onResponse(Object result) { }
15 |
16 | @Override
17 | public void onException(Throwable e) { }
18 |
19 | @Override
20 | public Executor getExecutor() {
21 | return null;
22 | }
23 |
24 | @SuppressWarnings("unchecked")
25 | public static T cast(Object object) {
26 | return (T) object;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-server-bootstrap/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | application
3 | alias(libs.plugins.shadow)
4 | }
5 |
6 | dependencies {
7 | implementation(project(":afybroker-server"))
8 | }
9 |
10 | val mainClazz = "net.afyer.afybroker.server.BootStrap"
11 |
12 | application {
13 | mainClass.set(mainClazz)
14 | }
15 |
16 | tasks.jar {
17 | manifest {
18 | attributes(
19 | "Main-Class" to mainClazz,
20 | )
21 | }
22 | }
23 |
24 | tasks.assemble {
25 | dependsOn(tasks.shadowJar)
26 | }
--------------------------------------------------------------------------------
/afybroker-server-bootstrap/src/main/java/net/afyer/afybroker/server/BootStrap.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 |
5 | import java.io.IOException;
6 |
7 | /**
8 | * @author Nipuru
9 | * @since 2022/7/29 20:19
10 | */
11 | @Slf4j
12 | public class BootStrap {
13 |
14 | public static void main(String[] args) throws IOException {
15 | BrokerServer brokerServer = BrokerServer.builder().build();
16 |
17 | Broker.setServer(brokerServer);
18 |
19 | brokerServer.startup();
20 |
21 | String line;
22 | while (brokerServer.isStart() && (line = brokerServer.getConsoleReader().readLine()) != null) {
23 | if (!brokerServer.getPluginManager().dispatchCommand(line)) {
24 | log.warn("Command not found.");
25 | }
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/afybroker-server/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | `java-library`
3 | `maven-publish`
4 | id("afybroker-publish")
5 | }
6 | dependencies {
7 | api(project(":afybroker-core"))
8 | api(libs.guava)
9 | api(libs.slf4j.api)
10 | api(libs.netty)
11 | api(libs.gson)
12 | api(libs.snakeyaml)
13 | api(libs.trove4j)
14 | api(libs.jline)
15 | api(libs.logback.classic)
16 | }
17 |
18 | java {
19 | withSourcesJar()
20 | }
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/aware/BrokerServerAware.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.aware;
2 |
3 | import net.afyer.afybroker.server.BrokerServer;
4 |
5 | /**
6 | * BrokerServerAware
7 | *
8 | * 设置 BrokerServer 实例
9 | * 实现了该接口的对象通过{@link BrokerServer#aware(Object)} 即可设置实例
10 | *
11 | * @author Nipuru
12 | * @since 2022/7/31 19:53
13 | */
14 | public interface BrokerServerAware {
15 |
16 | /**
17 | * set brokerServer
18 | * @param brokerServer brokerServer
19 | */
20 | void setBrokerServer(BrokerServer brokerServer);
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/command/CommandKick.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.command;
2 |
3 | import com.alipay.remoting.exception.RemotingException;
4 | import lombok.extern.slf4j.Slf4j;
5 | import net.afyer.afybroker.core.message.KickPlayerMessage;
6 | import net.afyer.afybroker.server.BrokerServer;
7 | import net.afyer.afybroker.server.plugin.Command;
8 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
9 |
10 | /**
11 | * @author Nipuru
12 | * @since 2022/10/10 10:41
13 | */
14 | @Slf4j
15 | public class CommandKick extends Command {
16 |
17 | private final BrokerServer server;
18 | public CommandKick(BrokerServer server) {
19 | super("kick");
20 | this.server = server;
21 | }
22 |
23 | @Override
24 | public void execute(String[] args) {
25 | if (args.length == 0) {
26 | log.info("kick [message...]");
27 | return;
28 | }
29 |
30 | String playerName = args[0];
31 |
32 | BrokerPlayer player = server.getPlayer(playerName);
33 | if (player == null) {
34 | log.info("player not online!");
35 | return;
36 | }
37 |
38 | StringBuilder messageBuilder = new StringBuilder();
39 | for (int i = 1; i < args.length; i++) {
40 | messageBuilder.append(args[i]);
41 | }
42 |
43 | KickPlayerMessage message = new KickPlayerMessage()
44 | .setUniqueId(player.getUniqueId())
45 | .setMessage(messageBuilder.toString());
46 |
47 | try {
48 | player.getProxy().oneway(message);
49 | } catch (RemotingException | InterruptedException e) {
50 | log.error(e.getMessage(), e);
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/command/CommandList.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.command;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import net.afyer.afybroker.server.BrokerServer;
5 | import net.afyer.afybroker.server.plugin.Command;
6 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
7 |
8 | import java.util.ArrayList;
9 | import java.util.List;
10 |
11 | /**
12 | * @author Nipuru
13 | * @since 2022/7/31 14:17
14 | */
15 | @Slf4j
16 | public class CommandList extends Command {
17 |
18 | private final BrokerServer server;
19 |
20 | public CommandList(BrokerServer server) {
21 | super("list", "ls");
22 | this.server = server;
23 | }
24 |
25 | @Override
26 | public void execute(String[] args) {
27 | List clients = new ArrayList<>(server.getClientManager().list());
28 |
29 | for (BrokerClientItem client : clients) {
30 | log.info("BrokerClient(type={}, address={}, name={}, tags={})",
31 | client.getType(), client.getAddress(), client.getName(), client.getTags());
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/command/CommandStop.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.command;
2 |
3 | import net.afyer.afybroker.server.BrokerServer;
4 | import net.afyer.afybroker.server.plugin.Command;
5 |
6 | /**
7 | * @author Nipuru
8 | * @since 2022/7/31 13:34
9 | */
10 | public class CommandStop extends Command {
11 |
12 | private final BrokerServer server;
13 |
14 | public CommandStop(BrokerServer server) {
15 | super("stop");
16 | this.server = server;
17 | }
18 |
19 | @Override
20 | public void execute(String[] args) {
21 | server.shutdown();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/command/ConsoleCommandCompleter.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.command;
2 |
3 | import jline.console.completer.Completer;
4 | import lombok.AccessLevel;
5 | import lombok.RequiredArgsConstructor;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.BrokerServer;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 | import java.util.Locale;
12 | import java.util.Map;
13 | import java.util.stream.Collectors;
14 |
15 | /**
16 | * @author Nipuru
17 | * @since 2022/8/6 8:24
18 | */
19 | @RequiredArgsConstructor
20 | @FieldDefaults(level = AccessLevel.PRIVATE)
21 | public class ConsoleCommandCompleter implements Completer {
22 |
23 | final BrokerServer server;
24 |
25 | @Override
26 | public int complete(String buffer, int cursor, List candidates) {
27 | int lastSpace = buffer.lastIndexOf(' ');
28 | if (lastSpace == -1) {
29 | String lowerCase = buffer.toLowerCase(Locale.ROOT);
30 | candidates.addAll(server.getPluginManager().getCommands().stream()
31 | .map(Map.Entry::getKey)
32 | .filter((name) -> name.toLowerCase(Locale.ROOT).startsWith(lowerCase))
33 | .collect(Collectors.toList()));
34 | } else {
35 | List suggestions = new ArrayList<>();
36 | server.getPluginManager().dispatchCommand(buffer, suggestions);
37 | candidates.addAll(suggestions);
38 | }
39 |
40 | return (lastSpace == -1) ? cursor - buffer.length() : cursor - (buffer.length() - lastSpace - 1);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/config/BrokerFileConfig.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.config;
2 |
3 | import net.afyer.afybroker.core.FileConfig;
4 | import net.afyer.afybroker.server.plugin.Plugin;
5 |
6 | import java.io.File;
7 | import java.io.IOException;
8 | import java.nio.file.Files;
9 |
10 | /**
11 | * @author Nipuru
12 | * @since 2022/7/31 12:16
13 | */
14 | public class BrokerFileConfig extends FileConfig {
15 |
16 | private final Plugin plugin;
17 | private Configuration configuration;
18 | private final Class extends ConfigurationProvider> provider;
19 |
20 | public BrokerFileConfig(String path, Plugin plugin, Class extends ConfigurationProvider> provider) {
21 | super(new File(plugin.getDataFolder(), path), path);
22 | this.plugin = plugin;
23 | this.provider = provider;
24 | init();
25 | reload();
26 | }
27 |
28 | private void init() {
29 | File configFile = getFile();
30 | if (!configFile.exists()) {
31 | configFile.getParentFile().mkdirs();
32 | try {
33 | Files.copy(plugin.getResourceAsStream(getName()), configFile.toPath());
34 | } catch (IOException e) {
35 | throw new IllegalStateException(e.getMessage(), e.getCause());
36 | }
37 | }
38 | }
39 |
40 | @Override
41 | public void save() {
42 | try {
43 | ConfigurationProvider.getProvider(provider).save(configuration, getFile());
44 | } catch (IOException e) {
45 | throw new IllegalStateException(e.getMessage(), e.getCause());
46 | }
47 | }
48 |
49 | @Override
50 | public void reload() {
51 | try {
52 | this.configuration = ConfigurationProvider.getProvider(provider).load(getFile());
53 | } catch (IOException e) {
54 | throw new IllegalStateException(e.getMessage(), e.getCause());
55 | }
56 | }
57 |
58 | @Override
59 | public Configuration get() {
60 | return configuration;
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/config/ConfigurationProvider.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.config;
2 |
3 | import java.io.*;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 |
7 | /**
8 | * @author Nipuru
9 | * @since 2022/7/31 12:09
10 | */
11 | public abstract class ConfigurationProvider {
12 |
13 | private static final Map, ConfigurationProvider> providers = new HashMap<>();
14 |
15 | static {
16 | try {
17 | providers.put(YamlConfiguration.class, new YamlConfiguration());
18 | } catch (NoClassDefFoundError ex) {
19 | // Ignore, no SnakeYAML
20 | }
21 |
22 | try {
23 | providers.put(JsonConfiguration.class, new JsonConfiguration());
24 | } catch (NoClassDefFoundError ex) {
25 | // Ignore, no Gson
26 | }
27 | }
28 |
29 | public static ConfigurationProvider getProvider(Class extends ConfigurationProvider> provider) {
30 | return providers.get(provider);
31 | }
32 |
33 | public abstract void save(Configuration config, File file) throws IOException;
34 |
35 | public abstract void save(Configuration config, Writer writer);
36 |
37 | public abstract Configuration load(File file) throws IOException;
38 |
39 | public abstract Configuration load(File file, Configuration defaults) throws IOException;
40 |
41 | public abstract Configuration load(Reader reader);
42 |
43 | public abstract Configuration load(Reader reader, Configuration defaults);
44 |
45 | public abstract Configuration load(InputStream is);
46 |
47 | public abstract Configuration load(InputStream is, Configuration defaults);
48 |
49 | public abstract Configuration load(String string);
50 |
51 | public abstract Configuration load(String string, Configuration defaults);
52 | }
53 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/ClientCloseEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.plugin.Event;
8 |
9 | import java.util.Set;
10 |
11 | /**
12 | * 当BrokerClient与BrokerServer断开连接时触发此事件
13 | * @author Nipuru
14 | * @since 2022/9/10 17:55
15 | */
16 | @Getter
17 | @AllArgsConstructor
18 | @FieldDefaults(level = AccessLevel.PRIVATE)
19 | public class ClientCloseEvent extends Event {
20 |
21 | /** 客户端地址 */
22 | final String remoteAddress;
23 |
24 | /** 客户端名称 */
25 | final String name;
26 |
27 | /** 客户端标签 */
28 | final Set tags;
29 |
30 | /** 客户端类型 */
31 | final String type;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/ClientConnectEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import com.alipay.remoting.Connection;
4 | import lombok.AccessLevel;
5 | import lombok.AllArgsConstructor;
6 | import lombok.Getter;
7 | import lombok.experimental.FieldDefaults;
8 | import net.afyer.afybroker.server.plugin.Event;
9 |
10 | /**
11 | * 当 BrokerClient 初次连接至 BrokerServer 时触发此事件
12 | *
13 | * @author Nipuru
14 | * @since 2022/9/10 17:54
15 | */
16 | @Getter
17 | @AllArgsConstructor
18 | @FieldDefaults(level = AccessLevel.PRIVATE)
19 | public class ClientConnectEvent extends Event {
20 | /** 客户端地址 */
21 | final String remoteAddress;
22 |
23 | /** 客户端连接 */
24 | final Connection connection;
25 | }
26 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/ClientRegisterEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.core.message.BrokerClientInfoMessage;
8 | import net.afyer.afybroker.server.plugin.Event;
9 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
10 |
11 | /**
12 | * 当 BrokerClient 注册到 BrokerServer之后触发此事件
13 | *
14 | * @author Nipuru
15 | * @since 2022/9/10 17:56
16 | */
17 | @Getter
18 | @AllArgsConstructor
19 | @FieldDefaults(level = AccessLevel.PRIVATE)
20 | public class ClientRegisterEvent extends Event {
21 |
22 | /** 客户端信息 */
23 | final BrokerClientInfoMessage brokerClientInfo;
24 |
25 | /** 客户端代理 */
26 | final BrokerClientItem brokerClientItem;
27 | }
28 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/PlayerProxyLoginEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.plugin.Event;
8 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
9 |
10 | /**
11 | * @author Nipuru
12 | * @since 2022/8/13 9:20
13 | */
14 | @Getter
15 | @AllArgsConstructor
16 | @FieldDefaults(level = AccessLevel.PRIVATE)
17 | public class PlayerProxyLoginEvent extends Event {
18 |
19 | /** 玩家代理 */
20 | final BrokerPlayer player;
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/PlayerProxyLogoutEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.plugin.Event;
8 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
9 |
10 | /**
11 | * @author Nipuru
12 | * @since 2022/8/13 9:20
13 | */
14 | @Getter
15 | @AllArgsConstructor
16 | @FieldDefaults(level = AccessLevel.PRIVATE)
17 | public class PlayerProxyLogoutEvent extends Event {
18 |
19 | /** 玩家代理 */
20 | final BrokerPlayer player;
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/PlayerServerConnectedEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.plugin.Event;
8 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
9 |
10 | /**
11 | * @author Nipuru
12 | * @since 2022/12/4 21:15
13 | */
14 | @Getter
15 | @AllArgsConstructor
16 | @FieldDefaults(level = AccessLevel.PRIVATE)
17 | public class PlayerServerConnectedEvent extends Event {
18 |
19 | /** 玩家代理 */
20 | final BrokerPlayer player;
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/event/PlayerServerJoinEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.event;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.plugin.Event;
8 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
9 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
10 | import org.jetbrains.annotations.Nullable;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2023/09/29 12:18
15 | */
16 | @Getter
17 | @AllArgsConstructor
18 | @FieldDefaults(level = AccessLevel.PRIVATE)
19 | public class PlayerServerJoinEvent extends Event {
20 |
21 | /** 玩家代理 */
22 | final BrokerPlayer player;
23 |
24 | /** 玩家之前所在的 server 代理 */
25 | @Nullable
26 | final BrokerClientItem previous;
27 |
28 | /** 玩家当前所在的 server 代理 */
29 | final BrokerClientItem current;
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/AsyncEvent.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import com.google.common.base.Preconditions;
4 | import lombok.*;
5 |
6 | import java.util.Map;
7 | import java.util.concurrent.ConcurrentHashMap;
8 | import java.util.concurrent.atomic.AtomicBoolean;
9 | import java.util.concurrent.atomic.AtomicInteger;
10 |
11 | /**
12 | * @author Nipuru
13 | * @since 2022/11/21 16:38
14 | */
15 | @Data
16 | @Getter(AccessLevel.NONE)
17 | @ToString(callSuper = true)
18 | @EqualsAndHashCode(callSuper = true)
19 | public class AsyncEvent extends Event {
20 |
21 | private final Callback done;
22 | private final Map intents = new ConcurrentHashMap<>();
23 | private final AtomicBoolean fired = new AtomicBoolean();
24 | private final AtomicInteger latch = new AtomicInteger();
25 |
26 | @Override
27 | @SuppressWarnings("unchecked")
28 | public void postCall() {
29 | if (latch.get() == 0) {
30 | done.done((T) this, null);
31 | }
32 | fired.set(true);
33 | }
34 |
35 | public void registerIntent(Plugin plugin) {
36 | Preconditions.checkState(!fired.get(), "Event %s has already been fired", this);
37 |
38 | AtomicInteger intentCount = intents.get(plugin);
39 | if (intentCount == null) {
40 | intents.put(plugin, new AtomicInteger(1));
41 | } else {
42 | intentCount.incrementAndGet();
43 | }
44 | latch.incrementAndGet();
45 | }
46 |
47 | @SuppressWarnings("unchecked")
48 | public void completeIntent(Plugin plugin) {
49 | AtomicInteger intentCount = intents.get(plugin);
50 | Preconditions.checkState(intentCount != null && intentCount.get() > 0, "Plugin %s has not registered intents for event %s", plugin, this);
51 |
52 | intentCount.decrementAndGet();
53 | if (fired.get()) {
54 | if (latch.decrementAndGet() == 0) {
55 | done.done((T) this, null);
56 | }
57 | } else {
58 | latch.decrementAndGet();
59 | }
60 | }
61 | }
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/BrokerClassLoader.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | /**
4 | * @author Nipuru
5 | * @since 2022/8/13 18:17
6 | */
7 | public class BrokerClassLoader extends ClassLoader {
8 |
9 | @Override
10 | protected Class> loadClass(String name, boolean resolve) throws ClassNotFoundException {
11 | try {
12 | return super.loadClass(name, resolve);
13 | } catch (ClassNotFoundException ignored) {
14 | }
15 |
16 | for (PluginClassloader loader : PluginClassloader.allLoaders) {
17 | try {
18 | return loader.loadClass0(name, resolve, false);
19 | } catch (ClassNotFoundException ignored) {
20 | }
21 | }
22 |
23 | throw new ClassNotFoundException(name);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Callback.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | /**
4 | * @author Nipuru
5 | * @since 2022/11/21 16:39
6 | */
7 | public interface Callback {
8 | void done(V result, Throwable error);
9 | }
10 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Cancellable.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | /**
4 | * @author Nipuru
5 | * @since 2022/12/4 21:35
6 | */
7 | public interface Cancellable {
8 |
9 | void setCancelled(boolean cancel);
10 |
11 | boolean isCancelled();
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Command.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.Getter;
5 | import lombok.experimental.FieldDefaults;
6 |
7 | /**
8 | * @author Nipuru
9 | * @since 2022/7/31 10:49
10 | */
11 | @Getter
12 | @FieldDefaults(level = AccessLevel.PRIVATE)
13 | public abstract class Command {
14 |
15 | /** 命令名称 */
16 | final String name;
17 | /** 命令别名 */
18 | final String[] aliases;
19 |
20 | public Command(String name, String... aliases)
21 | {
22 | this.name = name;
23 | this.aliases = aliases;
24 | }
25 |
26 | public abstract void execute(String[] args);
27 | }
28 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Event.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import net.afyer.afybroker.server.Broker;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2022/7/31 11:36
8 | */
9 | public class Event {
10 | public void postCall() {}
11 |
12 | public boolean call() {
13 | Broker.getPluginManager().callEvent(this);
14 | if (this instanceof Cancellable) {
15 | return !((Cancellable) this).isCancelled();
16 | }
17 | return true;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/EventHandler.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * @author Nipuru
10 | * @since 2022/7/31 11:44
11 | */
12 | @Retention(RetentionPolicy.RUNTIME)
13 | @Target(ElementType.METHOD)
14 | public @interface EventHandler {
15 |
16 | byte priority() default EventPriority.NORMAL;
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/EventHandlerMethod.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 |
8 | import java.lang.reflect.InvocationTargetException;
9 | import java.lang.reflect.Method;
10 |
11 | /**
12 | * @author Nipuru
13 | * @since 2022/7/31 11:42
14 | */
15 | @Getter
16 | @AllArgsConstructor
17 | @FieldDefaults(level = AccessLevel.PRIVATE)
18 | public class EventHandlerMethod {
19 | final Object listener;
20 | final Method method;
21 |
22 | public void invoke(Object event) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
23 | method.invoke(listener, event);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/EventPriority.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import lombok.experimental.UtilityClass;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2022/7/31 11:45
8 | */
9 | @UtilityClass
10 | public class EventPriority {
11 | public final byte LOWEST = -64;
12 | public final byte LOW = -32;
13 | public final byte NORMAL = 0;
14 | public final byte HIGH = 32;
15 | public final byte HIGHEST = 64;
16 | }
17 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Listener.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | /**
4 | * @author Nipuru
5 | * @since 2022/7/31 11:28
6 | */
7 | public interface Listener {
8 | }
9 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/Plugin.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import com.google.common.base.Preconditions;
4 | import lombok.AccessLevel;
5 | import lombok.Getter;
6 | import lombok.experimental.FieldDefaults;
7 | import net.afyer.afybroker.server.BrokerServer;
8 |
9 | import java.io.File;
10 | import java.io.InputStream;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2022/7/31 10:42
15 | */
16 | @Getter
17 | @FieldDefaults(level = AccessLevel.PRIVATE)
18 | public class Plugin {
19 |
20 | PluginDescription description;
21 | BrokerServer server;
22 | File file;
23 |
24 | public Plugin() {
25 | ClassLoader classLoader = getClass().getClassLoader();
26 | Preconditions.checkState( classLoader instanceof PluginClassloader, "Plugin requires " + PluginClassloader.class.getName() );
27 |
28 | ((PluginClassloader)classLoader).init(this);
29 | }
30 |
31 | public void onLoad() {
32 | }
33 |
34 |
35 | public void onEnable() {
36 | }
37 |
38 |
39 | public void onDisable() {
40 | }
41 |
42 | public final File getDataFolder()
43 | {
44 | return new File( getServer().getPluginsFolder(), getDescription().getName() );
45 | }
46 |
47 | public final InputStream getResourceAsStream(String name) {
48 | return getClass().getClassLoader().getResourceAsStream( name );
49 | }
50 |
51 | final void init(BrokerServer server, PluginDescription description)
52 | {
53 | this.server = server;
54 | this.description = description;
55 | this.file = description.getFile();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/PluginDescription.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 | import lombok.experimental.FieldDefaults;
8 |
9 | import java.io.File;
10 | import java.util.HashSet;
11 | import java.util.Set;
12 |
13 | /**
14 | * 代表 broker.yml
15 | *
16 | * @author Nipuru
17 | * @since 2022/7/31 10:40
18 | */
19 | @Data
20 | @NoArgsConstructor
21 | @AllArgsConstructor
22 | @FieldDefaults(level = AccessLevel.PRIVATE)
23 | public class PluginDescription {
24 |
25 | /**
26 | * 插件名称.
27 | */
28 | String name;
29 | /**
30 | * 插件主类 继承自{@link Plugin}.
31 | */
32 | String main;
33 | /**
34 | * 插件版本.
35 | */
36 | String version;
37 | /**
38 | * 插件作者.
39 | */
40 | String author;
41 | /**
42 | * 插件硬依赖.
43 | */
44 | Set depends = new HashSet<>();
45 | /**
46 | * 插件软依赖.
47 | */
48 | Set softDepends = new HashSet<>();
49 | /**
50 | * 插件源文件.
51 | */
52 | File file = null;
53 | /**
54 | * 可选 插件简介.
55 | */
56 | String description = null;
57 | }
58 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/plugin/TabExecutor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.plugin;
2 |
3 | /**
4 | * @author Nipuru
5 | * @since 2022/8/6 8:29
6 | */
7 | public interface TabExecutor {
8 |
9 | Iterable onTabComplete(String[] args);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/BroadcastChatBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.exception.RemotingException;
6 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
7 | import lombok.Setter;
8 | import lombok.extern.slf4j.Slf4j;
9 | import net.afyer.afybroker.core.BrokerClientType;
10 | import net.afyer.afybroker.core.message.BroadcastChatMessage;
11 | import net.afyer.afybroker.server.BrokerServer;
12 | import net.afyer.afybroker.server.aware.BrokerServerAware;
13 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
14 | import net.afyer.afybroker.server.proxy.BrokerClientManager;
15 |
16 | import java.util.List;
17 |
18 | /**
19 | * @author Nipuru
20 | * @since 2022/8/10 11:27
21 | */
22 | @Slf4j
23 | public class BroadcastChatBrokerProcessor extends AsyncUserProcessor implements BrokerServerAware {
24 |
25 | @Setter
26 | BrokerServer brokerServer;
27 |
28 | @Override
29 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, BroadcastChatMessage request) {
30 | BrokerClientManager clientProxyManager = brokerServer.getClientManager();
31 | List brokerClients = clientProxyManager.getByType(BrokerClientType.SERVER);
32 |
33 | for (BrokerClientItem brokerClient : brokerClients) {
34 | try {
35 | brokerClient.oneway(request);
36 | } catch (RemotingException | InterruptedException e) {
37 | log.error(e.getMessage(), e);
38 | }
39 | }
40 | }
41 |
42 | @Override
43 | public String interest() {
44 | return BroadcastChatMessage.class.getName();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/ConnectToServerBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
6 | import lombok.Setter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import net.afyer.afybroker.core.message.ConnectToServerMessage;
9 | import net.afyer.afybroker.server.BrokerServer;
10 | import net.afyer.afybroker.server.aware.BrokerServerAware;
11 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2022/9/6 17:35
16 | */
17 | @Slf4j
18 | public class ConnectToServerBrokerProcessor extends AsyncUserProcessor implements BrokerServerAware {
19 |
20 | @Setter
21 | BrokerServer brokerServer;
22 |
23 | @Override
24 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, ConnectToServerMessage message) throws Exception {
25 | BrokerPlayer brokerPlayer = brokerServer.getPlayerManager().getPlayer(message.getUniqueId());
26 | if (brokerPlayer == null) return;
27 |
28 | brokerPlayer.connectToServer(message.getServerName());
29 | }
30 |
31 | @Override
32 | public String interest() {
33 | return ConnectToServerMessage.class.getName();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/KickPlayerBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
6 | import lombok.Setter;
7 | import lombok.extern.slf4j.Slf4j;
8 | import net.afyer.afybroker.core.message.KickPlayerMessage;
9 | import net.afyer.afybroker.server.BrokerServer;
10 | import net.afyer.afybroker.server.aware.BrokerServerAware;
11 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2024/02/03 12:42
16 | */
17 | @Slf4j
18 | public class KickPlayerBrokerProcessor extends AsyncUserProcessor implements BrokerServerAware {
19 |
20 | @Setter
21 | BrokerServer brokerServer;
22 |
23 | @Override
24 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, KickPlayerMessage request) throws Exception {
25 | BrokerPlayer player = brokerServer.getPlayer(request.getUniqueId());
26 | if (player == null) {
27 | return;
28 | }
29 | player.kick(request.getMessage());
30 | }
31 |
32 | @Override
33 | public String interest() {
34 | return KickPlayerMessage.class.getName();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/PlayerProfilePropertyBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 |
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
6 | import lombok.Setter;
7 | import net.afyer.afybroker.core.message.PlayerProfilePropertyMessage;
8 | import net.afyer.afybroker.server.BrokerServer;
9 | import net.afyer.afybroker.server.aware.BrokerServerAware;
10 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2024/12/03 10:20
15 | */
16 | @Setter
17 | public class PlayerProfilePropertyBrokerProcessor extends SyncUserProcessor implements BrokerServerAware {
18 |
19 | BrokerServer brokerServer;
20 |
21 | @Override
22 | public Object handleRequest(BizContext bizCtx, PlayerProfilePropertyMessage request) throws Exception {
23 | BrokerPlayer player = brokerServer.getPlayer(request.getUniqueId());
24 | if (player == null) {
25 | return false;
26 | }
27 | return player.getProxy().invokeSync(request);
28 | }
29 |
30 | @Override
31 | public String interest() {
32 | return PlayerProfilePropertyMessage.class.getName();
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/SendPlayerChatBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.exception.RemotingException;
6 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
7 | import lombok.Setter;
8 | import lombok.extern.slf4j.Slf4j;
9 | import net.afyer.afybroker.core.message.SendPlayerMessageMessage;
10 | import net.afyer.afybroker.server.BrokerServer;
11 | import net.afyer.afybroker.server.aware.BrokerServerAware;
12 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
13 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
14 |
15 | /**
16 | * @author Nipuru
17 | * @since 2022/8/5 10:07
18 | */
19 | @Slf4j
20 | public class SendPlayerChatBrokerProcessor extends AsyncUserProcessor implements BrokerServerAware {
21 |
22 | @Setter
23 | BrokerServer brokerServer;
24 |
25 | @Override
26 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SendPlayerMessageMessage request) {
27 | BrokerPlayer brokerPlayer = brokerServer.getPlayer(request.getUniqueId());
28 | if (brokerPlayer == null) return;
29 | BrokerClientItem clientProxy = brokerPlayer.getServer();
30 | if (clientProxy == null) return;
31 | try {
32 | clientProxy.oneway(request);
33 | } catch (RemotingException | InterruptedException e) {
34 | log.error(e.getMessage(), e);
35 | }
36 | }
37 |
38 | @Override
39 | public String interest() {
40 | return SendPlayerMessageMessage.class.getName();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/SendPlayerTitleBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.exception.RemotingException;
6 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
7 | import lombok.Setter;
8 | import lombok.extern.slf4j.Slf4j;
9 | import net.afyer.afybroker.core.message.SendPlayerTitleMessage;
10 | import net.afyer.afybroker.server.BrokerServer;
11 | import net.afyer.afybroker.server.aware.BrokerServerAware;
12 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
13 | import net.afyer.afybroker.server.proxy.BrokerPlayer;
14 |
15 | /**
16 | * @author Nipuru
17 | * @since 2022/8/11 9:02
18 | */
19 | @Slf4j
20 | public class SendPlayerTitleBrokerProcessor extends AsyncUserProcessor implements BrokerServerAware {
21 |
22 | @Setter
23 | BrokerServer brokerServer;
24 |
25 | @Override
26 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SendPlayerTitleMessage request) {
27 | BrokerPlayer brokerPlayer = brokerServer.getPlayer(request.getName());
28 | if (brokerPlayer == null) {
29 | return;
30 | }
31 |
32 | BrokerClientItem clientProxy = brokerPlayer.getServer();
33 |
34 | if (clientProxy == null) {
35 | return;
36 | }
37 |
38 | try {
39 | clientProxy.oneway(request);
40 | } catch (RemotingException | InterruptedException e) {
41 | log.error(e.getMessage(), e);
42 | }
43 | }
44 |
45 | @Override
46 | public String interest() {
47 | return SendPlayerTitleMessage.class.getName();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/processor/connection/CloseEventBrokerProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.processor.connection;
2 |
3 | import com.alipay.remoting.Connection;
4 | import com.alipay.remoting.ConnectionEventProcessor;
5 | import lombok.AccessLevel;
6 | import lombok.Setter;
7 | import lombok.experimental.FieldDefaults;
8 | import lombok.extern.slf4j.Slf4j;
9 | import net.afyer.afybroker.server.BrokerServer;
10 | import net.afyer.afybroker.server.aware.BrokerServerAware;
11 | import net.afyer.afybroker.server.event.ClientCloseEvent;
12 | import net.afyer.afybroker.server.proxy.BrokerClientItem;
13 | import net.afyer.afybroker.server.proxy.BrokerClientManager;
14 |
15 | /**
16 | * @author Nipuru
17 | * @since 2022/7/30 11:42
18 | */
19 | @Slf4j
20 | @FieldDefaults(level = AccessLevel.PRIVATE)
21 | public class CloseEventBrokerProcessor implements ConnectionEventProcessor, BrokerServerAware {
22 |
23 | @Setter
24 | BrokerServer brokerServer;
25 |
26 | @Override
27 | public void onEvent(String remoteAddress, Connection connection) {
28 |
29 | BrokerClientManager clientProxyManager = brokerServer.getClientManager();
30 | BrokerClientItem brokerClientItem = clientProxyManager.getByAddress(remoteAddress);
31 | clientProxyManager.remove(remoteAddress);
32 |
33 | if (brokerClientItem != null) {
34 | ClientCloseEvent event = new ClientCloseEvent(remoteAddress, brokerClientItem.getName(), brokerClientItem.getTags(), brokerClientItem.getType());
35 | brokerServer.getPluginManager().callEvent(event);
36 | }
37 |
38 | log.info("BrokerClient[{}] disconnect", remoteAddress);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/proxy/BrokerPlayerManager.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.proxy;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.experimental.FieldDefaults;
5 | import org.jetbrains.annotations.Nullable;
6 |
7 | import java.util.Collection;
8 | import java.util.Collections;
9 | import java.util.Map;
10 | import java.util.UUID;
11 | import java.util.concurrent.ConcurrentHashMap;
12 |
13 | /**
14 | * 玩家代理 管理器
15 | *
16 | * @author Nipuru
17 | * @since 2022/7/30 20:36
18 | */
19 | @FieldDefaults(level = AccessLevel.PRIVATE)
20 | public class BrokerPlayerManager {
21 |
22 | final Map byUid = new ConcurrentHashMap<>();
23 | final Map byName = new ConcurrentHashMap<>();
24 | final Map view = Collections.unmodifiableMap(byUid);
25 |
26 | public Collection getPlayers() {
27 | return view.values();
28 | }
29 |
30 | @Nullable
31 | public BrokerPlayer addPlayer(BrokerPlayer player) {
32 | UUID uid = player.getUniqueId();
33 | BrokerPlayer absent = byUid.putIfAbsent(uid, player);
34 | if (absent == null) {
35 | byName.put(player.getName(), player);
36 | }
37 | return absent;
38 | }
39 |
40 | public void removePlayer(UUID uid) {
41 | BrokerPlayer player = byUid.remove(uid);
42 | if (player != null) {
43 | byName.remove(player.getName());
44 | }
45 | }
46 |
47 | @Nullable
48 | public BrokerPlayer getPlayer(UUID uid) {
49 | return byUid.get(uid);
50 | }
51 |
52 | @Nullable
53 | public BrokerPlayer getPlayer(String name) {
54 | return byName.get(name);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/scheduler/ScheduledTask.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.scheduler;
2 |
3 | import net.afyer.afybroker.server.plugin.Plugin;
4 |
5 | /**
6 | * @author Nipuru
7 | * @since 2022/7/31 12:21
8 | */
9 | public interface ScheduledTask {
10 |
11 | int getId();
12 |
13 | Plugin getOwner();
14 |
15 | Runnable getTask();
16 |
17 | void cancel();
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/afybroker-server/src/main/java/net/afyer/afybroker/server/scheduler/TaskScheduler.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.server.scheduler;
2 |
3 | import net.afyer.afybroker.server.plugin.Plugin;
4 |
5 | import java.util.concurrent.TimeUnit;
6 |
7 | /**
8 | * @author Nipuru
9 | * @since 2022/7/31 12:22
10 | */
11 | public interface TaskScheduler {
12 |
13 | void cancel(int id);
14 |
15 | void cancel(ScheduledTask task);
16 |
17 | int cancel(Plugin plugin);
18 |
19 | ScheduledTask runAsync(Plugin owner, Runnable task);
20 |
21 | ScheduledTask schedule(Plugin owner, Runnable task, long delay, TimeUnit unit);
22 |
23 | ScheduledTask schedule(Plugin owner, Runnable task, long delay, long period, TimeUnit unit);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/afybroker-velocity/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | alias(libs.plugins.shadow)
3 | }
4 |
5 | dependencies {
6 | compileOnly(libs.velocity.api)
7 | implementation(project(":afybroker-client"))
8 | }
9 |
10 | tasks.build {
11 | dependsOn(tasks.shadowJar)
12 | }
13 |
14 | tasks.processResources {
15 | val props = mapOf(
16 | "version" to project.version
17 | )
18 | inputs.properties(props)
19 | filesMatching("velocity-plugin.json") {
20 | expand(props)
21 | }
22 | }
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/ConnectToServerVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
6 | import com.velocitypowered.api.proxy.Player;
7 | import com.velocitypowered.api.proxy.ProxyServer;
8 | import com.velocitypowered.api.proxy.server.RegisteredServer;
9 | import lombok.AllArgsConstructor;
10 | import net.afyer.afybroker.core.message.ConnectToServerMessage;
11 | import net.afyer.afybroker.velocity.AfyBroker;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2022/9/6 17:35
16 | */
17 | @AllArgsConstructor
18 | public class ConnectToServerVelocityProcessor extends AsyncUserProcessor {
19 |
20 | private final AfyBroker plugin;
21 |
22 | @Override
23 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, ConnectToServerMessage message) {
24 | ProxyServer server = plugin.getServer();
25 |
26 | RegisteredServer target = server.getServer(message.getServerName()).orElse(null);
27 | if (target == null) return;
28 |
29 | Player player = server.getPlayer(message.getUniqueId()).orElse(null);
30 | if (player == null) return;
31 |
32 | player.createConnectionRequest(target).connect();
33 | }
34 |
35 | @Override
36 | public String interest() {
37 | return ConnectToServerMessage.class.getName();
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/KickPlayerVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
6 | import lombok.AllArgsConstructor;
7 | import net.afyer.afybroker.core.message.KickPlayerMessage;
8 | import net.afyer.afybroker.velocity.AfyBroker;
9 | import net.kyori.adventure.text.Component;
10 |
11 | /**
12 | * @author Nipuru
13 | * @since 2022/10/10 10:34
14 | */
15 | @AllArgsConstructor
16 | public class KickPlayerVelocityProcessor extends AsyncUserProcessor {
17 |
18 | private final AfyBroker plugin;
19 |
20 | @Override
21 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, KickPlayerMessage request) {
22 | plugin.getServer().getPlayer(request.getUniqueId())
23 | .ifPresent(player -> player.disconnect(Component.text(request.getMessage())));
24 | }
25 |
26 | @Override
27 | public String interest() {
28 | return KickPlayerMessage.class.getName();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/PlayerHeartbeatValidateVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor;
2 |
3 | import com.alipay.remoting.BizContext;
4 | import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
5 | import lombok.AllArgsConstructor;
6 | import net.afyer.afybroker.core.message.PlayerHeartbeatValidateMessage;
7 | import net.afyer.afybroker.velocity.AfyBroker;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 | import java.util.UUID;
12 |
13 | /**
14 | * @author Nipuru
15 | * @since 2023/11/25 12:32
16 | */
17 | @AllArgsConstructor
18 | public class PlayerHeartbeatValidateVelocityProcessor extends SyncUserProcessor {
19 |
20 | private final AfyBroker plugin;
21 |
22 | @Override
23 | public Object handleRequest(BizContext bizCtx, PlayerHeartbeatValidateMessage request) {
24 | // 包含验证失败(已离线)的玩家
25 | List response = new ArrayList<>();
26 | for (UUID uniqueId : request.getUniqueIdList()) {
27 | if (!plugin.getServer().getPlayer(uniqueId).isPresent()) {
28 | response.add(uniqueId);
29 | }
30 | }
31 | return response;
32 | }
33 |
34 | @Override
35 | public String interest() {
36 | return PlayerHeartbeatValidateMessage.class.getName();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/RequestPlayerInfoVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor;
2 |
3 | import com.alipay.remoting.BizContext;
4 | import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
5 | import com.velocitypowered.api.proxy.Player;
6 | import lombok.AllArgsConstructor;
7 | import net.afyer.afybroker.core.message.RequestPlayerInfoMessage;
8 | import net.afyer.afybroker.velocity.AfyBroker;
9 |
10 | import java.util.HashMap;
11 | import java.util.Map;
12 | import java.util.UUID;
13 |
14 | @AllArgsConstructor
15 | public class RequestPlayerInfoVelocityProcessor extends SyncUserProcessor {
16 |
17 | private final AfyBroker plugin;
18 |
19 | @Override
20 | public Object handleRequest(BizContext bizCtx, RequestPlayerInfoMessage request) throws Exception {
21 | Map map = new HashMap<>();
22 | for (Player player : plugin.getServer().getAllPlayers()) {
23 | map.put(player.getUniqueId(), player.getUsername());
24 | }
25 | return map;
26 | }
27 |
28 | @Override
29 | public String interest() {
30 | return RequestPlayerInfoMessage.class.getName();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/SyncServerVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor;
2 |
3 | import com.alipay.remoting.AsyncContext;
4 | import com.alipay.remoting.BizContext;
5 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
6 | import com.velocitypowered.api.proxy.server.RegisteredServer;
7 | import com.velocitypowered.api.proxy.server.ServerInfo;
8 | import lombok.AllArgsConstructor;
9 | import net.afyer.afybroker.core.message.SyncServerMessage;
10 | import net.afyer.afybroker.velocity.AfyBroker;
11 |
12 | import java.net.InetSocketAddress;
13 | import java.util.Optional;
14 |
15 | @AllArgsConstructor
16 | public class SyncServerVelocityProcessor extends AsyncUserProcessor {
17 |
18 | private final AfyBroker plugin;
19 |
20 | @Override
21 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SyncServerMessage request) throws Exception {
22 | if (!plugin.isSyncEnable()) {
23 | return;
24 | }
25 | request.getServers().forEach((name, address) -> {
26 | Optional server = plugin.getServer().getServer(name);
27 | String host = address.split(":")[0];
28 | int port = Integer.parseInt(address.split(":")[1]);
29 | InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(host, port);
30 | if (server.isPresent()) {
31 | ServerInfo serverInfo = server.get().getServerInfo();
32 | if (serverInfo.getAddress().equals(socketAddress)) {
33 | return;
34 | } else {
35 | plugin.getServer().unregisterServer(serverInfo);
36 | }
37 | }
38 | plugin.getServer().registerServer(new ServerInfo(name, socketAddress));
39 | });
40 | }
41 |
42 | @Override
43 | public String interest() {
44 | return SyncServerMessage.class.getName();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/java/net/afyer/afybroker/velocity/processor/connection/CloseEventVelocityProcessor.java:
--------------------------------------------------------------------------------
1 | package net.afyer.afybroker.velocity.processor.connection;
2 |
3 | import com.alipay.remoting.Connection;
4 | import com.alipay.remoting.ConnectionEventProcessor;
5 | import com.velocitypowered.api.proxy.ProxyServer;
6 | import lombok.AllArgsConstructor;
7 | import net.afyer.afybroker.velocity.AfyBroker;
8 | import net.kyori.adventure.text.Component;
9 | import net.kyori.adventure.text.TextComponent;
10 | import net.kyori.adventure.text.format.NamedTextColor;
11 |
12 | /**
13 | * @author Nipuru
14 | * @since 2023/08/11 08:58
15 | */
16 | @AllArgsConstructor
17 | public class CloseEventVelocityProcessor implements ConnectionEventProcessor {
18 |
19 | private final AfyBroker plugin;
20 |
21 | @Override
22 | public void onEvent(String remoteAddress, Connection connection) {
23 | ProxyServer server = plugin.getServer();
24 |
25 | if (plugin.getConfig().getNode("player", "kick-on-close").getBoolean(false)) {
26 |
27 | TextComponent msg = Component
28 | .text("服务器网关已关闭")
29 | .color(NamedTextColor.RED);
30 | server.getAllPlayers().forEach(player -> player.disconnect(msg));
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/resources/config.yml:
--------------------------------------------------------------------------------
1 | broker:
2 | # broker 网关服务器地址
3 | host: localhost
4 | # broker 网关服务器端口
5 | port: 11200
6 | # 客户端名称 每个客户端应该唯一
7 | name: 'velocity-%unique_id%'
8 | # 客户端默认标签 可以删除
9 | tags: [ 'velocity', 'proxy' ]
10 | # 元数据信息 字符串键值对 自行拓展
11 | metadata:
12 | #key1: 'value1'
13 | #key2: 'value2'
14 |
15 | player:
16 | # 在服务器网关断联后是否踢出全部玩家
17 | kick-on-close: false
18 |
19 | server:
20 | # 是否开启 mc 游戏服务器自动注册到 proxy 的功能
21 | # 若要开启则先确认 broker-bukkit 插件的 config.yml
22 | sync-enable: false
--------------------------------------------------------------------------------
/afybroker-velocity/src/main/resources/velocity-plugin.json:
--------------------------------------------------------------------------------
1 | {
2 | "id": "afybroker",
3 | "name": "AfyBroker",
4 | "version": "$version",
5 | "authors": [
6 | "Nipuru"
7 | ],
8 | "main": "net.afyer.afybroker.velocity.AfyBroker"
9 | }
--------------------------------------------------------------------------------
/build-logic/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | `kotlin-dsl`
3 | }
--------------------------------------------------------------------------------
/build-logic/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | @file:Suppress("UnstableApiUsage")
2 |
3 | dependencyResolutionManagement {
4 | repositories {
5 | mavenCentral()
6 | gradlePluginPortal()
7 | }
8 | }
9 |
10 | rootProject.name = "build-logic"
11 |
--------------------------------------------------------------------------------
/build-logic/src/main/kotlin/afybroker-publish.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | java
3 | `maven-publish`
4 | }
5 |
6 | extensions.configure {
7 | repositories {
8 | maven("http://repo.afyer.net/repository/maven-releases/") {
9 | credentials(PasswordCredentials::class.java)
10 | isAllowInsecureProtocol = true
11 | }
12 | }
13 | publications {
14 | create("maven") {
15 | from(components["java"])
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | java
3 | id("com.github.johnrengelman.shadow") version "7.1.2" apply false
4 | }
5 |
6 | subprojects {
7 |
8 | apply(plugin = "java")
9 |
10 | group = "net.afyer.afybroker"
11 | version = "2.4"
12 |
13 | repositories {
14 | mavenCentral()
15 | mavenLocal()
16 | maven("https://hub.spigotmc.org/nexus/repository/public/")
17 | maven("https://oss.sonatype.org/content/groups/public/")
18 | maven("https://repo.papermc.io/repository/maven-public/")
19 | maven("https://repo.minebench.de/")
20 | }
21 |
22 | dependencies {
23 | testImplementation ("org.junit.jupiter:junit-jupiter-api:5.8.2")
24 | testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.8.2")
25 |
26 | compileOnly ("org.projectlombok:lombok:1.18.36")
27 | annotationProcessor ("org.projectlombok:lombok:1.18.36")
28 |
29 | testCompileOnly ("org.projectlombok:lombok:1.18.36")
30 | testAnnotationProcessor ("org.projectlombok:lombok:1.18.36")
31 | }
32 |
33 | java {
34 | sourceCompatibility = JavaVersion.VERSION_1_8
35 | targetCompatibility = JavaVersion.VERSION_1_8
36 | }
37 |
38 | tasks.withType {
39 | options.encoding = Charsets.UTF_8.name()
40 | }
41 | }
42 |
43 |
44 |
45 | tasks.withType {
46 | enabled = false
47 | }
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/gradle/libs.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | velocity = "3.1.0"
3 | bungeecord = "1.19-R0.1-SNAPSHOT"
4 | spigot = "1.12.2-R0.1-SNAPSHOT"
5 |
6 |
7 | [plugins]
8 | shadow = "com.github.johnrengelman.shadow:7.1.2"
9 |
10 | [libraries]
11 | gson = "com.google.code.gson:gson:2.8.9"
12 | snakeyaml = "org.yaml:snakeyaml:1.30"
13 | logback-classic = "ch.qos.logback:logback-classic:1.5.16"
14 | guava = "com.google.guava:guava:33.0.0-jre"
15 | slf4j-api = "org.slf4j:slf4j-api:1.7.21"
16 | netty = "io.netty:netty-all:4.1.42.Final"
17 | trove4j = "net.sf.trove4j:core:3.1.0"
18 | jline = "jline:jline:2.14.6"
19 | hessian = "com.caucho:hessian:4.0.66"
20 | annotations = "org.jetbrains:annotations:22.0.0"
21 |
22 | spigot-api = { module = "org.spigotmc:spigot-api", version.ref = "spigot"}
23 | velocity-api = { module = "com.velocitypowered:velocity-api", version.ref = "velocity" }
24 | bungeecord-api = { module = "net.md-5:bungeecord-api", version.ref = "bungeecord" }
25 | bungeecord-proxy = { module = "net.md-5:bungeecord-proxy", version.ref = "bungeecord"}
26 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AfyerDev/AfyBroker/f4ee903c362ad635d0389f0530d0283f576522b8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | @file:Suppress("UnstableApiUsage")
2 |
3 | rootProject.name = "afybroker"
4 |
5 | pluginManagement {
6 | includeBuild("build-logic")
7 | repositories {
8 | mavenCentral()
9 | gradlePluginPortal()
10 | }
11 | }
12 |
13 | sequenceOf(
14 | "core",
15 | "server",
16 | "server-bootstrap",
17 | "client",
18 | "bukkit",
19 | "bungee",
20 | "velocity"
21 | ).forEach {
22 | val project = ":afybroker-$it"
23 | include(project)
24 | }
25 |
--------------------------------------------------------------------------------