コード例 #1
0
        public override StreamInfo RegisterFileToDownload(NetworkStream stream, CompressMode compressMode, ClientInfo client, bool isWebSocket)
        {
            var            bytes    = GoStreamReader.ReadBlockToEnd(stream, compressMode, ProviderSetting.MaximumReceiveDataBlock, isWebSocket);
            var            json     = Encoding.UTF8.GetString(bytes);
            MethodCallInfo callInfo = ServerSerializationHelper.Deserialize <MethodCallInfo>(json);

            MethodCallbackInfo callback = new MethodCallbackInfo();

            callback.Guid = callInfo.Guid;

            var serviceType = RegisteredServiceTypes[callInfo.ServiceName];
            var sessionId   = callInfo.Data.ToString();
            var clientInfo  = (from x in Services.ToArray() where x.Key.SessionId == sessionId select x.Key).FirstOrDefault();

            if (clientInfo == null)
            {
                throw new Exception("RegisterFile client not found!");
            }
            var service = FindClientServiceByType(clientInfo, serviceType);

#if (NETSTANDARD1_6 || NETCOREAPP1_1)
            var method = serviceType.GetTypeInfo().GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(serviceType, callInfo).ToArray());
#else
            var method = serviceType.GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(serviceType, callInfo).ToArray());
#endif
            List <object> parameters = new List <object>();
            int           index      = 0;
            var           prms       = method.GetParameters();
            foreach (var item in callInfo.Parameters)
            {
                parameters.Add(ServerSerializationHelper.Deserialize(item.Value, prms[index].ParameterType));
                index++;
            }
            if (method.ReturnType != typeof(StreamInfo))
            {
                throw new Exception("return type for upload must StreamInfo!");
            }
            else
            {
                StreamInfo data = null;
                data = (StreamInfo)method.Invoke(service, parameters.ToArray());
                if (data == null)
                {
                    throw new Exception($"StreamInfo cannot be null");
                }
                var streamReader = data.Stream;
                data.Stream   = null;
                callback.Data = ServerSerializationHelper.SerializeObject(data, this);
                SendCallbackData(callback, client);
                data.Stream = streamReader;
                return(data);
            }
        }
コード例 #2
0
        /// <summary>
        /// start client to reading stream and data from server
        /// </summary>
        /// <param name="client"></param>
        internal void StartToReadingClientData()
        {
            AsyncActions.Run(() =>
            {
                try
                {
                    var stream = _client.GetStream();
                    while (true)
                    {
                        //first byte is DataType
                        var dataType = (DataType)stream.ReadByte();
                        //secound byte is compress mode
                        var compresssMode = (CompressMode)stream.ReadByte();

                        // server is called client method
                        if (dataType == DataType.CallMethod)
                        {
                            var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket);
                            if (SecuritySettings != null)
                            {
                                bytes = DecryptBytes(bytes);
                            }
                            var json = Encoding.UTF8.GetString(bytes);
                            MethodCallInfo callInfo = JsonConvert.DeserializeObject <MethodCallInfo>(json);
                            if (callInfo.Type == MethodType.User)
                            {
                                CallMethod(callInfo);
                            }
                            else if (callInfo.Type == MethodType.SignalGo)
                            {
                                if (callInfo.MethodName == "/MustReconnectUdpServer")
                                {
                                    ReconnectToUdp(callInfo);
                                }
                            }
                        }
                        //after client called server method, server response to client
                        else if (dataType == DataType.ResponseCallMethod)
                        {
                            var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket);
                            if (SecuritySettings != null)
                            {
                                bytes = DecryptBytes(bytes);
                            }
                            var json = Encoding.UTF8.GetString(bytes);
                            MethodCallbackInfo callback = JsonConvert.DeserializeObject <MethodCallbackInfo>(json);

                            var geted = ConnectorExtension.WaitedMethodsForResponse.TryGetValue(callback.Guid, out KeyValue <AutoResetEvent, MethodCallbackInfo> keyValue);
                            if (geted)
                            {
                                keyValue.Value = callback;
                                keyValue.Key.Set();
                            }
                        }
                        else if (dataType == DataType.GetServiceDetails)
                        {
                            var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket);
                            if (SecuritySettings != null)
                            {
                                bytes = DecryptBytes(bytes);
                            }
                            var json = Encoding.UTF8.GetString(bytes);
                            getServiceDetialResult = JsonConvert.DeserializeObject <ProviderDetailsInfo>(json);
                            if (getServiceDetialResult == null)
                            {
                                getServiceDetialExceptionResult = JsonConvert.DeserializeObject <Exception>(json);
                            }
                            getServiceDetailEvent.Set();
                            getServiceDetailEvent.Reset();
                        }
                        else if (dataType == DataType.GetMethodParameterDetails)
                        {
                            var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket);
                            if (SecuritySettings != null)
                            {
                                bytes = DecryptBytes(bytes);
                            }
                            var json = Encoding.UTF8.GetString(bytes);
                            getmethodParameterDetailsResult = json;
                            getServiceDetailEvent.Set();
                            getServiceDetailEvent.Reset();
                        }
                        else
                        {
                            //incorrect data! :|
                            SignalGo.Shared.Log.AutoLogger.LogText("StartToReadingClientData Incorrect Data!");
                            Dispose();
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    SignalGo.Shared.Log.AutoLogger.LogError(ex, "StartToReadingClientData");
                    Dispose();
                }
            });
        }
コード例 #3
0
        internal override StreamInfo RegisterFileStreamToDownload(MethodCallInfo Data)
        {
            if (IsDisposed || !_client.Connected)
            {
                return(null);
            }
            //connect to tcp
#if (NETSTANDARD1_6 || NETCOREAPP1_1)
            var downloadFileSocket = new TcpClient();
            var waitable           = downloadFileSocket.ConnectAsync(_address, _port);
            waitable.Wait();
#else
            var downloadFileSocket = new TcpClient(_address, _port);
#endif
            var socketStream = downloadFileSocket.GetStream();

            downloadFileSocket.Client.Send(Encoding.UTF8.GetBytes("SignalGo/1.0"));
            //get OK SignalGo/1.0
            int o = socketStream.ReadByte();
            int k = socketStream.ReadByte();

            //register client
            var json = JsonConvert.SerializeObject(new List <string>()
            {
                "/DownloadFile"
            });
            List <byte> bytes     = new List <byte>();
            var         jsonBytes = Encoding.UTF8.GetBytes(json);
            byte[]      dataLen   = BitConverter.GetBytes(jsonBytes.Length);
            bytes.AddRange(dataLen);
            bytes.AddRange(jsonBytes);
            GoStreamWriter.WriteToStream(socketStream, bytes.ToArray(), IsWebSocket);

            ///send method data
            json  = JsonConvert.SerializeObject(Data);
            bytes = new List <byte>();
            bytes.Add((byte)DataType.RegisterFileDownload);
            bytes.Add((byte)CompressMode.None);
            jsonBytes = Encoding.UTF8.GetBytes(json);
            dataLen   = BitConverter.GetBytes(jsonBytes.Length);
            bytes.AddRange(dataLen);
            bytes.AddRange(jsonBytes);
            if (bytes.Count > ProviderSetting.MaximumSendDataBlock)
            {
                throw new Exception("SendData data length is upper than MaximumSendDataBlock");
            }

            GoStreamWriter.WriteToStream(socketStream, bytes.ToArray(), IsWebSocket);

            //get OK SignalGo/1.0
            //int o = socketStream.ReadByte();
            //int k = socketStream.ReadByte();

            //get DataType
            var dataType = (DataType)socketStream.ReadByte();
            //secound byte is compress mode
            var compresssMode = (CompressMode)socketStream.ReadByte();

            // server is called client method
            if (dataType == DataType.ResponseCallMethod)
            {
                var bytesArray = GoStreamReader.ReadBlockToEnd(socketStream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket);
                json = Encoding.UTF8.GetString(bytesArray);
                MethodCallInfo callInfo = JsonConvert.DeserializeObject <MethodCallInfo>(json);
                var            data     = JsonConvert.DeserializeObject <StreamInfo>(callInfo.Data.ToString());
                data.Stream = socketStream;
                return(data);
            }
            return(null);
        }