public string GetMethodParameterDetial(MethodParameterDetails methodParameterDetails)
        {
            AsyncActions.Run(() =>
            {
                string json  = JsonConvert.SerializeObject(methodParameterDetails);
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                if (SecuritySettings != null)
                {
                    bytes = EncryptBytes(bytes);
                }
                byte[] len       = BitConverter.GetBytes(bytes.Length);
                List <byte> data = new List <byte>
                {
                    (byte)DataType.GetMethodParameterDetails,
                    (byte)CompressMode.None
                };
                data.AddRange(len);
                data.AddRange(bytes);
                if (data.Count > ProviderSetting.MaximumSendDataBlock)
                {
                    throw new Exception("SendCallbackData data length is upper than MaximumSendDataBlock");
                }

                GoStreamWriter.WriteToStream(_client.GetStream(), data.ToArray(), IsWebSocket);
            });
            getServiceDetailEvent.WaitOne();
            return(getmethodParameterDetailsResult);
        }
        /// <summary>
        /// after call method from server , client must send callback to server
        /// </summary>
        /// <param name="callback">method callback data</param>
        internal void SendCallbackData(MethodCallbackInfo callback)
        {
            string json = JsonConvert.SerializeObject(callback);

            byte[] bytes = Encoding.UTF8.GetBytes(json);
            if (SecuritySettings != null)
            {
                bytes = EncryptBytes(bytes);
            }
            byte[]      len  = BitConverter.GetBytes(bytes.Length);
            List <byte> data = new List <byte>
            {
                (byte)DataType.ResponseCallMethod,
                (byte)CompressMode.None
            };

            data.AddRange(len);
            data.AddRange(bytes);
            if (data.Count > ProviderSetting.MaximumSendDataBlock)
            {
                throw new Exception("SendCallbackData data length is upper than MaximumSendDataBlock");
            }

            GoStreamWriter.WriteToStream(_client.GetStream(), data.ToArray(), IsWebSocket);
        }
        //public object SendData(this ClientDuplex client, string className, string callerName, params object[] args)
        //{
        //    MethodCallInfo callInfo = new MethodCallInfo();
        //    callInfo.FullClassName = className;
        //    callInfo.MethodName = callerName;
        //    foreach (var item in args)
        //    {
        //        callInfo.Parameters.Add(new Shared.Models.ParameterInfo() { Type = item.GetType().FullName, Value = JsonConvert.SerializeObject(item) });
        //    }
        //    SendData(callInfo);
        //    return null;
        //}

        /// <summary>
        /// send data to call server method
        /// </summary>
        /// <param name="Data"></param>
        internal void SendData(MethodCallInfo Data)
        {
            AsyncActions.Run(() =>
            {
                try
                {
                    var json          = JsonConvert.SerializeObject(Data);
                    List <byte> bytes = new List <byte>
                    {
                        (byte)DataType.CallMethod,
                        (byte)CompressMode.None
                    };
                    var jsonBytes = Encoding.UTF8.GetBytes(json);
                    if (SecuritySettings != null)
                    {
                        jsonBytes = EncryptBytes(jsonBytes);
                    }
                    byte[] 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(_client.GetStream(), bytes.ToArray(), IsWebSocket);
                }
                catch (Exception ex)
                {
                    AutoLogger.LogError(ex, "ConnectorBase SendData");
                }
            });
        }
Exemple #4
0
        public override void WriteStreamToClient(StreamInfo streamInfo, NetworkStream toWrite, bool isWebSocket)
        {
            var readStream = streamInfo.Stream;

            while (true)
            {
                var bytes     = new byte[1024];
                var readCount = readStream.Read(bytes, 0, bytes.Length);
                if (readCount == 0)
                {
                    break;
                }
                GoStreamWriter.WriteToStream(toWrite, bytes.ToList().GetRange(0, readCount).ToArray(), isWebSocket);
            }
        }
        internal override void RegisterFileStreamToUpload(StreamInfo streamInfo, MethodCallInfo Data)
        {
            if (IsDisposed || !_client.Connected)
            {
                return;
            }
            //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
            streamInfo.Stream = downloadFileSocket.GetStream();
            downloadFileSocket.Client.Send(Encoding.UTF8.GetBytes("SignalGo/1.0"));
            //get OK SignalGo/1.0
            int o = streamInfo.Stream.ReadByte();
            int k = streamInfo.Stream.ReadByte();

            //register client
            var json = JsonConvert.SerializeObject(new List <string>()
            {
                "/UploadFile"
            });
            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(downloadFileSocket.GetStream(), bytes.ToArray(), IsWebSocket);

            ///send method data
            json  = JsonConvert.SerializeObject(Data);
            bytes = new List <byte>();
            bytes.Add((byte)DataType.RegisterFileUpload);
            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(downloadFileSocket.GetStream(), bytes.ToArray(), IsWebSocket);
        }
        /// <summary>
        /// send data to server for accept reality connection
        /// </summary>
        /// <param name="url"></param>
        void ConnectToUrl(string url)
        {
            var json = JsonConvert.SerializeObject(new List <string>()
            {
                url
            });
            List <byte> bytes     = new List <byte>();
            var         jsonBytes = Encoding.UTF8.GetBytes(json);

            byte[] dataLen = BitConverter.GetBytes(jsonBytes.Length);
            bytes.AddRange(dataLen);
            bytes.AddRange(jsonBytes);
            Console.WriteLine("write url:" + bytes.Count);
            GoStreamWriter.WriteToStream(_client.GetStream(), bytes.ToArray(), IsWebSocket);
            Console.WriteLine("write complete:" + bytes.Count);
        }
        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);
        }