예제 #1
0
 public static void Invoke(string methodId, IPeer peer, ResponseCallback responseCallback, Action<ISerializable> action) {
     using (var serializable = new Serializable(null)) {
         action?.Invoke(serializable);
         var rpcInvoke = new RpcInvoke("", methodId);
         RpcProxy.Invoke(MessageType.RpcInvoke, rpcInvoke, serializable.WriteStream, peer, responseCallback);
     }
 }
예제 #2
0
        public IUser AddListener(SocketService socketService)
        {
            var peer = PoolAllocator <IPeer> .GetObject() as Peer;

            peer.SetStatus(ConnectionStatus.Connected, socketService, this);

            socketService.HandleDisconnect += () => {
                if (peer.Status != ConnectionStatus.Connected)
                {
                    return;
                }
                Disconnected?.Invoke(peer);
                peer.Recycle();
            };

            socketService.HandleRead += (readStream) => {
                var tmp = readStream.Clone();
                _messageQueue.Enqueue(() => {
                    var securityComponent = peer.GetComponent <ISecurityComponent>();
                    if (securityComponent == null)
                    {
                        Logger.Error($"{nameof(securityComponent)}组件为空!");
                        return;
                    }

                    var remoteMessageId = tmp.ShiftRight <ulong>();
                    Ssfi.Security.DecryptAES(tmp, securityComponent.AesKey);
                    RpcProxy.Invoke(remoteMessageId, tmp, peer);
                    tmp.Dispose();
                });
            };

            socketService.HandleError += reason => Logger.Error(reason);
            socketService.HandleWrite += (isSuccess) => {
                if (!isSuccess)
                {
                    Logger.Error($"消息发送失败!");
                }
            };

            socketService.HandleAcknowledge += (isSuccess, readStream) => {
                var securityComponent = peer.GetComponent <ISecurityComponent>();
                if (securityComponent == null)
                {
                    Logger.Error($"{nameof(securityComponent)}组件为空!");
                    return;
                }

                // local
                var localMessageId = readStream.ShiftRight <ulong>();
                peer.Acknowledge(isSuccess, localMessageId);
            };
            return(peer);
        }
예제 #3
0
        public ClientSocket(NetworkConfig networkConfig, UpdateRunner updateRunner)
        {
            if (networkConfig == null)
            {
                throw new ArgumentNullException(nameof(networkConfig));
            }

            if (updateRunner == null)
            {
                throw new ArgumentNullException(nameof(updateRunner));
            }

            updateRunner.Add(this);

            _socket = new UdpSocket(sizeof(ulong), networkConfig);
            _socket.HandleConnect += (socketService, rs) => {
                if (rs == null)
                {
                    Logger.Warn("连接失败!");
                    Enqueue(() => {
                        Connected?.Invoke(null, null);
                    });
                    return;
                }

                var length = rs.ShiftRight <ushort>();
                var data   = rs.ShiftRight(length);
                var bytes  = new byte[data.Count];
                Buffer.BlockCopy(data.Buffer, data.Offset, bytes, 0, bytes.Length);

                var buffer = Ssui.Security.DecryptAesKey(bytes, _keys);
                var aesKey = buffer.toString();
                Logger.Debug(aesKey);

                var peer = new Peer(_socket, aesKey);

                socketService.HandleDisconnect += () => {
                    Enqueue(() => {
                        Disconnected?.Invoke(peer);
                    });
                };

                socketService.HandleRead += (readStream) => {
                    // remote
                    var remoteMessageId = readStream.ShiftRight <ulong>();
                    var newReadStream   = readStream.Clone();
                    Ssui.Security.DecryptAES(newReadStream, aesKey);
                    Enqueue(() => {
                        RpcProxy.Invoke(remoteMessageId, newReadStream, peer);
                        newReadStream.Dispose();
                    });
                };

                socketService.HandleAcknowledge += (state, readStream) => {
                    var newReadStream = readStream.Clone();

                    var localMessageId = readStream.ShiftRight <ulong>();
                    Enqueue(() => {
                        peer.Acknowledge(state, localMessageId);
                        newReadStream.Dispose();
                    });
                };
                socketService.HandleError += error => Enqueue(() => Logger.Error(error));
                socketService.HandleWrite += (isSuccess) => {
                    Enqueue(() => {
                        if (!isSuccess)
                        {
                            Logger.Warn("发送失败!");
                        }
                    });
                };

                var newRs = rs.Clone();
                Enqueue(() => {
                    peer.SetStatus(ConnectionStatus.Connected, socketService);
                    Connected?.Invoke(peer, newRs);
                    newRs.Dispose();
                });
            };
        }