using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using StressClient.common; using SuperSocket.ClientEngine; using SuperSocket.ClientEngine.Protocol; namespace StressClient { class AsyncClient { private SuperSocket.ClientEngine.AsyncTcpSession _client = null; private int _idx = -1; private string _clientId = ""; //private string _svr_ip = "127.0.0.1"; //private int _svr_port = 8081; private string _svr_ip = AppSetting.Instance().server_address;//"120.25.176.175"; private int _svr_port = AppSetting.Instance().server_port; // 最后成功请求时间 string _last_req_time = ""; public AsyncClient(int idx, string clientId) { _idx = idx; _clientId = clientId; } public void Run() { try { string message = string.Format("{0} 客户端开始运行", _clientId); System.Console.WriteLine(message); // 创建连接 InitConnection(); System.Threading.Thread.Sleep(2000); while (true) { DateTime curTime = DateTime.Now; if (should_send_now(curTime)) { // 发送数据 SendRequest(curTime); } // 随机等待几秒 Random rdm = new Random(); int sleep_time = rdm.Next(5, 10); System.Threading.Thread.Sleep(sleep_time * 1000); } } catch (Exception e) { System.Console.WriteLine(e.Message); } } private void InitConnection() { try { if (_client != null) _client.Close(); _client = new AsyncTcpSession(); _client.Connect(new IPEndPoint(IPAddress.Parse(_svr_ip), _svr_port)); // 连接断开事件 _client.Closed += client_Closed; // 收到服务器数据事件 _client.DataReceived += client_DataReceived; // 连接到服务器事件 _client.Connected += client_Connected; // 发生错误的处理 _client.Error += client_Error; } catch (Exception e) { System.Console.WriteLine(e.Message); } } public void SendRequest(DateTime curTime) { try { if (_client.IsConnected) { string message = string.Format("[{0}] ClientId {1} 客户端发送数据", DateTime.Now, _clientId); System.Console.WriteLine(message); // 拼装请求串 //byte[] arr = Encoding.Default.GetBytes(string.Format("{0} {1}", "ADD", "DDDDDDDD\r\n")); string req_str = Helper.MakeReqStr1(_clientId, curTime); byte[] arr = Encoding.UTF8.GetBytes(req_str); _client.Send(arr, 0, arr.Length); // 发送成功,修改最新请求时间 UpdateLastReqTime(curTime); } else { InitConnection(); } } catch(Exception) { } } //判断当前时间是否在工作时间段内 protected bool CheckTimeSpan(string timeStr) { string[] workTime = AppSetting.Instance().work_day_time_span; string strWorkingTimeStart1 = workTime[0];//工作时间上午11 string strWorkingTimeEnd1 = workTime[1]; string strWorkingTimeStart2 = workTime[2];//工作时间上午11 string strWorkingTimeEnd2 = workTime[3]; TimeSpan dspWorkingTimeStart1 = DateTime.Parse(strWorkingTimeStart1).TimeOfDay; TimeSpan dspWorkingTimeEnd1 = DateTime.Parse(strWorkingTimeEnd1).TimeOfDay; TimeSpan dspWorkingTimeStart2 = DateTime.Parse(strWorkingTimeStart2).TimeOfDay; TimeSpan dspWorkingTimeEnd2 = DateTime.Parse(strWorkingTimeEnd2).TimeOfDay; //string time1 = "2017-2-17 8:10:00"; DateTime t1 = Convert.ToDateTime(timeStr); TimeSpan dspNow = t1.TimeOfDay; if ((dspNow > dspWorkingTimeStart1 && dspNow < dspWorkingTimeEnd1)|| (dspNow > dspWorkingTimeStart2 && dspNow < dspWorkingTimeEnd2)) { return true; } return false; } // 是否到了发送时间 private bool should_send_now(DateTime curTime) { //判断当前时间是否在工作时间段内 if (!CheckTimeSpan(curTime.ToString("yyyy-MM-dd HH:mm:ss"))) { return false; } // 从未发送过 if (_last_req_time == "") return true; // 分钟填5的倍数,秒填00 int minute = curTime.Minute - curTime.Minute % 5; Object[] param = new Object[] { curTime.Year, curTime.Month, curTime.Day, curTime.Hour, minute, 0 }; string cur_dt = string.Format("{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}", param); // 此时刻已经请求过 if (_last_req_time == cur_dt) return false; return true; //int t1 = (curTime.Hour * 100 + curTime.Minute) * 100 + curTime.Second; //int t2 = (curTime.Hour * 100 + minute) * 100 + 0; //// 不要偏离整5分太远 //if (t1 - t2 < 25) //{ // return true; //} //return false; } // 修改最新请求时间 private void UpdateLastReqTime(DateTime curTime) { // 分钟填5的倍数,秒填00 int minute = curTime.Minute - curTime.Minute % 5; Object[] param = new Object[] { curTime.Year, curTime.Month, curTime.Day, curTime.Hour, minute, 0 }; string cur_dt = string.Format("{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}", param); _last_req_time = cur_dt; } private void client_Error(object sender, ErrorEventArgs e) { string message = string.Format("[{0}] ClientId {1} 客户端错误, {2}", DateTime.Now, _clientId, e.Exception.Message); System.Console.WriteLine(message); this.Error(message); } private void client_Connected(object sender, EventArgs e) { string message = string.Format("[{0}] ClientId {1} 客户端连接成功", DateTime.Now, _clientId); System.Console.WriteLine(message); this.Info(message); } private void client_DataReceived(object sender, DataEventArgs e) { string msg = Encoding.Default.GetString(e.Data, 0, e.Length).ToString().Replace("\r\n", ""); string message = string.Format("[{0}][{1}] Recv {2}", DateTime.Now, _clientId, msg); Console.WriteLine(message); this.Info(message); } private void client_Closed(object sender, EventArgs e) { AsyncTcpSession client = sender as AsyncTcpSession; string message = string.Format("[{0}] ClientId {1} 客户端连接断开", DateTime.Now, _clientId); System.Console.WriteLine(message); this.Warn(message); } } }