protected virtual void SockCloseService(SockRequest request, ref SockResponse response) { // get param string & parse to dictionary string url = Encoding.UTF8.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // find session IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSess result = null; if (dc["type"] == SockType.listen.ToString()) result = sessctl.FindSession(SockType.listen, ep, null); else if (dc["type"] == SockType.connect.ToString()) result = sessctl.FindSession(SockType.connect, ep, null); else// if (dc["type"] == SockType.accept.ToString()) result = sessctl.FindSession(SockType.accept, null, ep); // close session if (result != null) sessctl.DelSession(result); // write response if (result != null) response.data = Encoding.UTF8.GetBytes("Success: shutdown " + ep.ToString() + "\r\n"); else response.data = Encoding.UTF8.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); }
public override void Handle(SockRequest request, ref SockResponse response) { if (request.content_mode == SockRequestContentMode.none) { lock (pack_queue) { if (pack_queue.Count > 2048) { log4net.ILog log = log4net.LogManager.GetLogger(typeof(Dispatcher)); log.Fatal("pack_queue's count is larger than 2048!"); pack_queue.Clear(); return; } else { pack_queue.Enqueue(new object[] { request, response }); return; } } } else { try { byte[] result = Convert.FromBase64String(Encoding.UTF8.GetString(request.data)); result = EncryptSym.AESDecrypt(result); if (result != null) request.SetData(result); } catch (Exception) { } } base.Handle(request, ref response); //response.data = EncryptSym.AESEncrypt(response.data); }
// Session Event ================================================================================== protected virtual void SessParse(object sender, SockSess sess) { // init request & response SockRequest request = new SockRequest(sess.lep, sess.rep, sess.RfifoTake()); SockResponse response = new SockResponse(); // rfifo skip sess.RfifoSkip(request.length); // dispatch dispatcher.Handle(request, ref response); if (response.data != null && response.data.Length != 0) sessctl.SendSession(sess, response.data); }
// Center Service ========================================================================= protected virtual void DefaultService(SockRequest request, ref SockResponse response) { // write response response.data = Encoding.UTF8.GetBytes("Failure: unknown request\r\n"); }
protected virtual void SockSendService(SockRequest request, ref SockResponse response) { // retrieve param_list of url string url = Encoding.UTF8.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); // retrieve param_data int index_data = param_list.IndexOf("&data="); if (index_data == -1) return; string param_data = param_list.Substring(index_data + 6); param_list = param_list.Substring(0, index_data); // retrieve param to dictionary IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // find session IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSess result = null; if (dc["type"] == SockType.listen.ToString()) result = sessctl.FindSession(SockType.listen, ep, null); else if (dc["type"] == SockType.connect.ToString()) result = sessctl.FindSession(SockType.connect, ep, null); else// if (dc["type"] == SockType.accept.ToString()) result = sessctl.FindSession(SockType.accept, null, ep); // send message if (result != null) sessctl.SendSession(result, Encoding.UTF8.GetBytes(param_data)); // write response if (result != null) response.data = Encoding.UTF8.GetBytes("Success: send to " + ep.ToString() + "\r\n"); else response.data = Encoding.UTF8.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); }
protected virtual void RecvEvent(object sender) { SockSessNew sess = sender as SockSessNew; SockRequest request = new SockRequest(sess.lep, sess.rep, sess.rfifo.Take()); SockResponse response = new SockResponse(); dispatcher.Handle(request, ref response); if (response.data != null && response.data.Length != 0) sess.wfifo.Append(response.data); }
protected virtual void SockSendService(SockRequest request, ref SockResponse response) { // retrieve param_list of url string url = Encoding.UTF8.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); // retrieve param_data int index_data = param_list.IndexOf("&data="); if (index_data == -1) return; string param_data = param_list.Substring(index_data + 6); param_list = param_list.Substring(0, index_data); // retrieve param to dictionary IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); SockType sockType = (SockType)Enum.Parse(typeof(SockType), dc["type"]); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSessNew sess = FindSockSessFromSessGroup(sockType, ep); if (sess != null) { sess.wfifo.Append(Encoding.UTF8.GetBytes(param_data)); response.data = Encoding.UTF8.GetBytes("Success: send to " + ep.ToString() + "\r\n"); } else response.data = Encoding.UTF8.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); }
protected virtual void SockOpenService(SockRequest request, ref SockResponse response) { // get param string & parse to dictionary string msg = Encoding.UTF8.GetString(request.data); if (!msg.Contains('?')) return; msg = msg.Substring(msg.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(msg); SockType sockType = (SockType)Enum.Parse(typeof(SockType), dc["type"]); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); try { SockSessNew sess = null; if (sockType == SockType.listen) sess = MakeListen(ep); else sess = MakeConnect(ep); response.data = Encoding.UTF8.GetBytes("Success: " + dc["type"] + " " + ep.ToString() + "\r\n"); } catch (Exception) { response.data = Encoding.UTF8.GetBytes("Failure: can't open " + ep.ToString() + "\r\n"); } }
protected virtual void SockCloseService(SockRequest request, ref SockResponse response) { // get param string & parse to dictionary string msg = Encoding.UTF8.GetString(request.data); if (!msg.Contains('?')) return; msg = msg.Substring(msg.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(msg); SockType sockType = (SockType)Enum.Parse(typeof(SockType), dc["type"]); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSessNew sess = FindSockSessFromSessGroup(sockType, ep); if (sess != null) { sess.Close(); response.data = Encoding.UTF8.GetBytes("Success: shutdown " + ep.ToString() + "\r\n"); } else response.data = Encoding.UTF8.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); }
private void ClientCloseService(SockRequest request, ref SockResponse response) { // check target center if (!CheckServerTargetCenter(request.lep.Port)) return; // get param string & parse to dictionary string url = Encoding.UTF8.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // find session IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSess result = sessctl.FindSession(SockType.accept, null, ep); // close session if (result != null) sessctl.DelSession(result); // write response if (result != null) response.data = Coding.GetBytes("Success: shutdown " + ep.ToString() + "\r\n"); else response.data = Coding.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); /// ** update DataUI if (result != null) DataUI.ClientDel(ep); }
// Center Service protected override void DefaultService(SockRequest request, ref SockResponse response) { string log = DateTime.Now + " (" + request.rep.ToString() + " => " + request.lep.ToString() + ")\n"; log += SockConvert.ParseBytesToString(request.data) + "\n\n"; /// ** update DataUI DataUI.Logger(log); }
private void ClientUpdateService(SockRequest request, ref SockResponse response) { // check target center if (!CheckServerTargetCenter(request.lep.Port)) return; // get param string & parse to dictionary string url = Coding.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // update sess data IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSess result = sessctl.FindSession(SockType.accept, null, ep); if (result != null) { SessData sd = result.sdata as SessData; sd.Ccid = dc["ccid"]; sd.Name = dc["name"]; } // write response if (result != null) response.data = Coding.GetBytes("Success: update " + ep.ToString() + "\r\n"); else response.data = Coding.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); /// ** update DataUI if (result != null) { DataUI.ClientUpdate(ep, "ID", dc["ccid"]); DataUI.ClientUpdate(ep, "Name", dc["name"]); } }
private void ClientSendService(SockRequest request, ref SockResponse response) { // check target center if (!CheckServerTargetCenter(request.lep.Port)) return; // get param string & parse to dictionary string url = Coding.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); // retrieve param_data int index_data = param_list.IndexOf("&data="); if (index_data == -1) return; string param_data = param_list.Substring(index_data + 6); param_list = param_list.Substring(0, index_data); // retrieve param to dictionary IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // find session IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockSess result = sessctl.FindSession(SockType.accept, null, ep); // send message if (result != null) sessctl.SendSession(result, Coding.GetBytes(param_data)); // write response if (result != null) response.data = Coding.GetBytes("Success: sendto " + ep.ToString() + "\r\n"); else response.data = Coding.GetBytes("Failure: can't find " + ep.ToString() + "\r\n"); /// ** update DataUI if (result != null) { string log = DateTime.Now + " (" + request.rep.ToString() + " => " + result.rep.ToString() + ")\n"; log += Coding.GetString(Coding.GetBytes(param_data)) + "\n\n"; DataUI.Logger(log); } }
private void ClientSendByCcidService(SockRequest request, ref SockResponse response) { // check target center if (!CheckServerTargetCenter(request.lep.Port)) return; // get param string & parse to dictionary string url = Coding.GetString(request.data); if (!url.Contains('?')) return; string param_list = url.Substring(url.IndexOf('?') + 1); // retrieve param_data int index_data = param_list.IndexOf("&data="); if (index_data == -1) return; string param_data = param_list.Substring(index_data + 6); param_list = param_list.Substring(0, index_data); // retrieve param to dictionary IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(param_list); // find session SockSess result = null; foreach (var item in sessctl.GetSessionTable()) { if (item.type != SockType.accept) continue; SessData sd = item.sdata as SessData; if (sd.Ccid == dc["ccid"]) { result = item; // take last one as result, so comment "break" at next line //break; } } // send message if (result != null) sessctl.SendSession(result, Coding.GetBytes(param_data)); // write response if (result != null) response.data = Coding.GetBytes("Success: sendto " + dc["ccid"] + "\r\n"); else response.data = Coding.GetBytes("Failure: can't find " + dc["ccid"] + "\r\n"); /// ** update DataUI if (result != null) { string log = DateTime.Now + " (" + request.rep.ToString() + " => " + result.rep.ToString() + ")\n"; log += Coding.GetString(Coding.GetBytes(param_data)) + "\n\n"; DataUI.Logger(log); } }
private void ClientListService(SockRequest request, ref SockResponse response) { if (!CheckServerTargetCenter(request.lep.Port)) return; StringBuilder sb = new StringBuilder(); foreach (var item in sessctl.GetSessionTable()) { if (item.type != SockType.accept) continue; SessData sd = item.sdata as SessData; if (String.IsNullOrEmpty(sd.Ccid)) continue; sb.Append("{" + "\"dev\":\"" + item.lep.Port + "\"," + "\"ip\":\"" + item.rep.ToString() + "\"," + "\"time\":\"" + sd.TimeConn + "\"," + "\"ccid\":\"" + sd.Ccid + "\"," + "\"name\":\"" + sd.Name + "\"" + "}"); } sb.Insert(0, '['); sb.Append(']'); sb.Replace("}{", "},{"); sb.Append("\r\n"); response.data = Coding.GetBytes(sb.ToString()); }
protected override void SockOpenService(SockRequest request, ref SockResponse response) { // get param string & parse to dictionary string msg = Encoding.UTF8.GetString(request.data); if (!msg.Contains('?')) return; msg = msg.Substring(msg.IndexOf('?') + 1); IDictionary<string, string> dc = SockConvert.ParseUrlQueryParam(msg); SockType sockType = (SockType)Enum.Parse(typeof(SockType), dc["type"]); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(dc["ip"]), int.Parse(dc["port"])); SockOpen(dc["id"], sockType, ep); }
// Center Service ========================================================================= protected override void SockSendService(SockRequest request, ref SockResponse response) { base.SockSendService(request, ref response); /// ** update DataUI if (response.data != null) { string log = DateTime.Now + " (" + request.rep.ToString() + " => " + "*.*.*.*" + ")\n"; log += "Request: " + Coding.GetString(request.data) + "\n"; log += "Respond: " + Coding.GetString(response.data) + "\n\n"; DataUI.Logger(log); } }