コード例 #1
0
        public string ExecStringJWT(CallPackage callPackage)
        {
            var request = (HttpWebRequest)WebRequest.Create(callPackage.EndPointUrl + callPackage.Parameters);

            request.Method        = callPackage.Method.ToString();
            request.ContentLength = 0;
            request.ContentType   = callPackage.ContentType;

            if (!string.IsNullOrEmpty(callPackage.PostData) && callPackage.Method == HttpVerbs.Post)
            {
                byte[] bytes = callPackage.Encoding.GetBytes(callPackage.PostData);
                request.ContentLength = bytes.Length;

                using (Stream writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseValue = response.GetResponseStream().AsString();
                return(responseValue);
            }
        }
コード例 #2
0
        public string ExecString(CallPackage callPackage)
        {
            var request = (HttpWebRequest)WebRequest.Create(callPackage.EndPointUrl + callPackage.Parameters);

            request.Method        = callPackage.Method.ToString();
            request.ContentLength = 0;
            request.ContentType   = callPackage.ContentType;

            if (!string.IsNullOrEmpty(callPackage.PostData) && callPackage.Method == HttpVerbs.Post)
            {
                byte[] bytes = callPackage.Encoding.GetBytes(callPackage.PostData);
                request.ContentLength = bytes.Length;

                using (Stream writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    return(message);

                    throw new ApplicationException(message);
                }
                // grab the response
                var responseValue = response.GetResponseStream().AsString();
                return(responseValue);
            }
        }
コード例 #3
0
        public void Execute()
        {
            CallPackage cp = new CallPackage();

            cp.Command = "register";
            cp.Data    = "testerService";

            //cp.Params.Add("commands", new[] { "time", "date" });

            _clientService.Call(cp);

            string s = "";

            do
            {
                if (s == "call")
                {
                    cp         = new CallPackage();
                    cp.Command = "call";
                    cp.Data    = "time";
                    Console.WriteLine(cp.Guid);
                    _clientService.Call(cp);
                }
                else
                {
                    _clientService.Send(s);
                }
                Console.Write("command:>");
            } while ((s = Console.ReadLine()) != "exit");

            Stop();
        }
コード例 #4
0
        public void Call(CallPackage package, TcpClient client)
        {
            string json = JsonConvert.SerializeObject(package);

            byte[] _data = Encoding.ASCII.GetBytes(json);
            SendImmediate(_data, client);
        }
コード例 #5
0
        private void Response(CallPackage cp)
        {
            var _client = awaitResponses.FirstOrDefault(x => x.Key == cp.Guid).Value;

            ServerService.Call(cp, _client);

            _logger.LogDebug("response " + cp.Data);
        }
コード例 #6
0
        public void Call(CallPackage package)
        {
            string json = JsonConvert.SerializeObject(package);

            byte[] _data = Encoding.ASCII.GetBytes(json);
            AddToPacket(_data);
            FlushData();
        }
コード例 #7
0
        public User AuthenticateUser(User authenticateUserData)
        {
            var url = GetUrl("WebAPI", "AuthenticateUser");
            var authenticateUserJson = JsonConvert.SerializeObject(authenticateUserData);
            var callPackageData      = new CallPackage(url, HttpVerbs.Post, authenticateUserJson);
            var rawValue             = ExecString(callPackageData);
            var responseValue        = JsonConvert.DeserializeObject <User>(rawValue);

            return(responseValue);
        }
コード例 #8
0
        /// <summary>
        /// This method runs on its own thread, and is responsible for
        /// receiving data from the server and raising an event when data
        /// is received
        /// </summary>
        private void ListenForPackets()
        {
            int bytesRead;

            while (started)
            {
                bytesRead = 0;

                try
                {
                    //Blocks until a message is received from the server
                    bytesRead = clientStream.Read(buffer.ReadBuffer, 0, readBufferSize);
                }
                catch
                {
                    //A socket error has occurred
                    _logger.LogDebug("A socket error has occurred with the client socket " + tcpClient.Client.LocalEndPoint.ToString());
                    break;
                }

                if (bytesRead == 0)
                {
                    //The server has disconnected
                    break;
                }

                if (OnDataReceived != null)
                {
                    //Send off the data for other classes to handle
                    var response = Encoding.ASCII.GetString(buffer.ReadBuffer, 0, bytesRead);

                    CallPackage cp = new CallPackage();
                    cp.Command = "raw";
                    cp.Data    = response;

                    try
                    {
                        cp = JsonConvert.DeserializeObject <CallPackage>(response);
                    }
                    catch { }

                    cp.Client = tcpClient;
                    //OnDataReceived(cp);

                    //Send off the data for other classes to handle

                    OnDataReceived(cp);
                }

                Thread.Sleep(15);
            }

            started = false;
            Disconnect();
        }
コード例 #9
0
        public void Handle(CallPackage response)
        {
            _logger.LogTrace("+: command: " + response.Command);

            var com = commands.FirstOrDefault(x => x.Key.Any(y => y == response.Command)).Value;

            if (com != null)
            {
                com.Execute(response);
            }
        }
コード例 #10
0
        private void Register(CallPackage cp)
        {
            var d = cp.Params.FirstOrDefault(x => x.Key == "commands").Value;

            if (d != null)
            {
                JArray jsonResponse = JArray.Parse(d.ToString());
                commands.Add(jsonResponse.ToObject <string[]>(), cp.Client);
            }

            ServerService.Send(cp.Client, "registred by server");
        }
コード例 #11
0
        private void Server_OnDataReceived(byte[] data, int bytesRead, TcpClient client)
        {
            var response_string = _crypterService.Decrypt(data, bytesRead);
            var response        = _mapperService.Map <CallPackage>(response_string);

            if (response == null)
            {
                response         = new CallPackage();
                response.Command = "raw";
                response.Data    = response_string;
            }

            response.Client = client;

            _gatewayHandler.Handle(response);
        }
コード例 #12
0
        private void Call(CallPackage cp)
        {
            var _client = commands.FirstOrDefault(x => x.Key.Any(y => y == cp.Data)).Value;

            if (_client != null)
            {
                awaitResponses.Add(cp.Guid, cp.Client);
                ServerService.Call(cp, _client);
                _logger.LogDebug("call " + cp.Data);
            }
            else
            {
                cp.Data = "no service for " + cp.Data;
                ServerService.Call(cp, cp.Client);
                _logger.LogDebug("no service for " + cp.Data);
            }
        }
コード例 #13
0
        public string ExecString(CallPackage callPackage)
        {
            var request = (HttpWebRequest)WebRequest.Create(callPackage.EndPointUrl + callPackage.Parameters);

            request.Method        = callPackage.Method.ToString();
            request.ContentLength = 0;
            request.ContentType   = callPackage.ContentType;

            if (!string.IsNullOrEmpty(callPackage.PostData) && callPackage.Method == HttpVerbs.Post)
            {
                byte[] bytes = callPackage.Encoding.GetBytes(callPackage.PostData);
                request.ContentLength = bytes.Length;

                using (Stream writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    string message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                    throw new ApplicationException(message);
                }
                // grab the response
                var responseValue = response.GetResponseStream().AsString();
                return(responseValue);

                #region COMMENTED
                // grab the response
                //StreamReader reader = new StreamReader(response.GetResponseStream());
                ////// Read the content.
                //string responseFromServer = reader.ReadToEnd();
                //responseFromServer.Replace(@"\", "").Trim();
                //var responseValue = response.GetResponseStream().ToString();
                //return responseFromServer;
                #endregion
            }
        }
コード例 #14
0
        internal override void Execute(CallPackage cp)
        {
            _logger.LogTrace(cp.Guid.ToString());

            switch (cp.Command)
            {
            case "register":
                Register(cp);
                break;

            case "call":
                Call(cp);
                break;

            case "response":
                Response(cp);
                break;
            }


            //string[] commands = (string[])cp.Params.FirstOrDefault(x => x.Key == "commands").Value;
        }
コード例 #15
0
 internal override void Execute(CallPackage cp)
 {
     _logger.LogDebug(Environment.NewLine + cp.Data);
 }
コード例 #16
0
 internal override void Execute(CallPackage cp)
 {
     ServerService.Send(cp.Client, "go exit");
     ServerService.DisconnectClient(cp.Client);
 }
コード例 #17
0
ファイル: CommandBase.cs プロジェクト: rr1980/rrServiceNetV2
 internal abstract void Execute(CallPackage cp);
コード例 #18
0
 private void Client_OnDataReceived(CallPackage response)
 {
     _logger.LogDebug("+: command: " + response.Command + "\t\t:\t" + response.Data);
 }