public DbCommand CreateCommand(DbConnection conn) { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("CreateCommand", CodeTrackLevel.Function, null, "EFCore")) { #endif DbCommand cmd = conn.CreateCommand(); cmd.CommandText = Text.ToString(); cmd.CommandType = CommandType; DbCommand = cmd; for (int i = 0; i < Parameters.Count; i++) { Parameter p = Parameters[i]; var cmdp = cmd.CreateParameter(); cmdp.ParameterName = p.Name; cmdp.Value = p.Value; cmdp.Direction = p.Direction; cmd.Parameters.Add(cmdp); } return(cmd); #if NETCOREAPP2_1 } #endif }
public async Task <JToken> ReceiveFrom(object data) { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Request", CodeTrackLevel.Function, null, "Websocket", "JsonClient")) { #endif Send(data); return(await Receive()); #if NETCOREAPP2_1 } #endif }
public async Task <string> ReceiveFrom(string text) { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Request", CodeTrackLevel.Function, null, "Websocket", "TextClient")) { #endif Send(text); return(await Receive()); #if NETCOREAPP2_1 } #endif }
public async Task <Result> Execute(Command cmd, params Type[] types) { var host = cmd.Read ? Host.GetReadHost() : Host.GetWriteHost(); if (host == null) { return(new Result() { ResultType = ResultType.NetError, Messge = "redis server is not available" }); } var client = await host.Pop(); if (client == null) { return new Result() { ResultType = ResultType.NetError, Messge = "exceeding maximum number of connections" } } ; try { var result = host.Connect(client); if (result.IsError) { return(result); } using (var tarck = CodeTrackFactory.Track(cmd.Name, CodeTrackLevel.Module, null, "Redis", client.Host)) { if (tarck.Enabled) { tarck.Activity?.AddTag("tag", "BeetleX Redis"); } cmd.Activity = tarck.Activity; RedisRequest request = new RedisRequest(host, client, cmd, types); request.Activity = tarck.Activity; result = await request.Execute(); return(result); } } finally { if (client != null) { host.Push(client); } } }
public virtual void Send(string text) { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Send", CodeTrackLevel.Function, null, "Websocket", "TextClient")) { #endif DataFrame dataFrame = new DataFrame(); byte[] data = Encoding.UTF8.GetBytes(text); dataFrame.Body = new ArraySegment <byte>(data, 0, data.Length); SendFrame(dataFrame); #if NETCOREAPP2_1 } #endif }
public virtual void Send(object data) { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Send", CodeTrackLevel.Function, null, "Websocket", "JsonClient")) { #endif string text = Newtonsoft.Json.JsonConvert.SerializeObject(data); DataFrame df = new DataFrame(); var buffer = Encoding.UTF8.GetBytes(text); df.Body = new ArraySegment <byte>(buffer, 0, buffer.Length); base.SendFrame(df); #if NETCOREAPP2_1 } #endif }
public virtual async Task <JToken> Receive() { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Receive", CodeTrackLevel.Function, null, "Websocket", "JsonClient")) { #endif var data = await ReceiveFrame(); if (data.Type != DataPacketType.text) { throw new BXException("Data type is not json text"); } if (data.Body == null) { return(new JObject()); } var body = data.Body.Value; string result = Encoding.UTF8.GetString(body.Array, body.Offset, body.Count); return((JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(result)); #if NETCOREAPP2_1 } #endif }
public virtual async Task <string> Receive() { #if NETCOREAPP2_1 using (CodeTrackFactory.Track("Receive", CodeTrackLevel.Function, null, "Websocket", "TextClient")) { #endif var data = await ReceiveFrame(); if (data.Type != DataPacketType.text) { throw new BXException("Data type is not text"); } if (data.Body == null) { return(null); } var body = data.Body.Value; string result = Encoding.UTF8.GetString(body.Array, body.Offset, body.Count); return(result); #if NETCOREAPP2_1 } #endif }
public void Connect() { if (IsConnected) { return; } lock (mLockConnect) { if (IsConnected) { return; } #if NETCOREAPP2_1 using (CodeTrackFactory.Track($"Connect {Host}", CodeTrackLevel.Function, null, "Websocket", "Client")) { #endif mWScompletionSource = new TaskCompletionSource <bool>(); if (mNetClient == null) { string protocol = Host.Scheme.ToLower(); if (!(protocol == "ws" || protocol == "wss")) { OnConnectResponse(new BXException("protocol error! host must [ws|wss]//host:port"), null); mWScompletionSource.Task.Wait(); } WSPacket wSPacket = new WSPacket { WSClient = this }; if (Host.Scheme.ToLower() == "wss") { mNetClient = SocketFactory.CreateSslClient <AsyncTcpClient>(wSPacket, Host.Host, Host.Port, SSLAuthenticateName); mNetClient.CertificateValidationCallback = CertificateValidationCallback; } else { mNetClient = SocketFactory.CreateClient <AsyncTcpClient>(wSPacket, Host.Host, Host.Port); } mNetClient.LocalEndPoint = this.LocalEndPoint; mNetClient.LittleEndian = false; mNetClient.PacketReceive = OnPacketCompleted; mNetClient.ClientError = OnClientError; } mDataFrames = new System.Collections.Concurrent.ConcurrentQueue <object>(); bool isNew; if (mNetClient.Connect(out isNew)) { OnWriteConnect(); } else { OnConnectResponse(mNetClient.LastError, null); } mWScompletionSource.Task.Wait(10000); if (!OnWSConnected) { throw new TimeoutException($"Connect {Host} websocket server timeout!"); } #if NETCOREAPP2_1 } #endif } }
private async void OnExecute() { HttpClientHandler client = null; Response response; bool closeClient = false; try { object result = null; requestResult = new TaskCompletionSource <object>(); client = await HttpHost.Pool.Pop(); Client = client.Client; AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client; asyncClient.ClientError = onEventClientError; asyncClient.PacketReceive = OnEventClientPacketCompleted; GetConnection?.Invoke(asyncClient); #if NETCOREAPP2_1 using (CodeTrackFactory.Track(Url, CodeTrackLevel.Function, mRequestTrack?.Activity?.Id, "HTTPClient", "Protocol", "Write")) { asyncClient.Send(this); Status = RequestStatus.SendCompleted; } #else asyncClient.Send(this); Status = RequestStatus.SendCompleted; #endif #if NETCOREAPP2_1 using (CodeTrackFactory.Track(Url, CodeTrackLevel.Function, mRequestTrack?.Activity?.Id, "HTTPClient", "Protocol", "Read")) { var a = requestResult.Task; result = await a; } #else var a = requestResult.Task; result = await a; #endif if (result is Exception error) { response = new Response(); response.Exception = new HttpClientException(this, HttpHost.Uri, error.Message, error); Status = RequestStatus.Error; closeClient = true; } else { response = (Response)result; Status = RequestStatus.Received; } if (response.Exception == null) { int code = int.Parse(response.Code); if (response.Length > 0) { try { if (code >= 200 && code < 300) { response.Body = this.Formater.Deserialization(response, response.Stream, this.BodyType, response.Length); } else { response.Body = response.Stream.ReadString(response.Length); } } finally { response.Stream.ReadFree(response.Length); if (response.Chunked) { response.Stream.Dispose(); } response.Stream = null; } } if (!response.KeepAlive) { client.Client.DisConnect(); } if (code >= 400) { response.Exception = new HttpClientException(this, HttpHost.Uri, $"{Url}({response.Code}) [{response.Body}]"); response.Exception.Code = code; } Status = RequestStatus.Completed; } } catch (Exception e_) { HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_); response = new Response { Exception = clientException }; Status = RequestStatus.Error; closeClient = true; } if (response.Exception != null) { HttpHost.AddError(response.Exception.SocketError); } else { HttpHost.AddSuccess(); } Response.Current = response; this.Response = response; if (client != null) { if (client.Client is AsyncTcpClient asclient) { asclient.ClientError = null; asclient.PacketReceive = null; } if (closeClient) { await DisConnect(client.Client); } HttpHost.Pool.Push(client); client = null; } await Task.Run(() => mTaskCompletionSource.Success(response)); }
private void OnReceive(IClient c, ClientReceiveArgs reader) { mReceiveTrack = CodeTrackFactory.Track("Read", CodeTrackLevel.Function, Activity?.Id, "Redis", "Protocol"); if (Command.NetworkReceive != null) { var result = Command.NetworkReceive(this, reader.Stream.ToPipeStream()); if (result != null) { Result = result; OnCompleted(ResultType.Object, null); } } else { ResultType resultType; string msg; PipeStream pipeStream = reader.Stream.ToPipeStream(); if (mFreeLength > 0) { pipeStream.ReadFree(mFreeLength); mFreeLength = 0; } if (Result.Status == ResultStatus.None) { if (pipeStream.TryReadLine(out string line)) { char type = line[0]; switch (type) { case '+': resultType = ResultType.Simple; msg = line.Substring(1, line.Length - 1); OnCompleted(resultType, msg); return; case '-': resultType = ResultType.Error; msg = line.Substring(1, line.Length - 1); OnCompleted(resultType, msg); return; case ':': Result.Data.Add(new ResultItem { Type = ResultType.Integers, Data = long.Parse(line.Substring(1, line.Length - 1)) }); Result.Status = ResultStatus.Completed; OnCompleted(ResultType.Integers, null); return; case '$': Result.ResultType = ResultType.Bulck; Result.ArrayCount = 1; Result.BodyLength = int.Parse(line.Substring(1, line.Length - 1)); Result.Status = ResultStatus.Loading; break; case '*': Result.ResultType = ResultType.Arrays; Result.ArrayCount = int.Parse(line.Substring(1, line.Length - 1)); Result.Status = ResultStatus.Loading; break; } } } if (Result.Status == ResultStatus.Loading) { if (Result.ResultType == ResultType.Arrays) { LoadArray(pipeStream); } else if (Result.ResultType == ResultType.Bulck) { LoadBulck(pipeStream); } } } }