int hookKeyProc(globalKeyboardHook.keyboardPacket a_kpParam) { try { if (m_tcClient != null && m_tcClient.Connected == true) { NetworkStream ns = m_tcClient.GetStream(); byte[] arrbBuffer = StructureToByte(a_kpParam); ns.Write(arrbBuffer, 0, arrbBuffer.Length); int iVK = a_kpParam.vkCode; Invoke((MethodInvoker) delegate { addLog(globalKeyboardHook.getLogMessage(a_kpParam), LogType.Packet); }); } else { throw new IOException(); } } catch (IOException ioe) { if (m_tcClient != null) { m_tcClient.Close(); } m_tcClient = null; controlHookEvent(false); Invoke((MethodInvoker) delegate { if (m_fnotiCurrent != null) { m_fnotiCurrent.Close(); m_fnotiCurrent = null; } addLog("접속 종료 : 호스트에 의한 중단", LogType.Status); btnConnect.Text = "Connect"; m_bAllowListen = true; new Notification("알림", "호스트에 의해 접속 종료됨.", cnst_iAlarmWindow_DispalyInterval, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up).Show(); }); } return(0); }
private void frmMain_Load(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; ShowInTaskbar = false; m_gkh.DonotFireEvent = true; m_gkh.hook(); new Notification("알림", "Share Input Device 시작합니다.", cnst_iAlarmWindow_DispalyInterval, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up).Show(); m_thrLiveCheck = new Thread(delegate() { while (m_bRunning) { m_ucLiveCheck = new UdpClient(cnst_iUdpPort); IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, cnst_iUdpPort); Invoke((MethodInvoker) delegate { addLog("생존 채널 기동, 패킷 수신 대기", LogType.Status); }); try { while (m_bRunning) { byte[] bytes = m_ucLiveCheck.Receive(ref groupEP); string strMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length); switch (strMessage) { case "SHAREINPUTDEVICE_SEARCH": { Invoke((MethodInvoker) delegate { addLog(string.Format("생존 채널 수신, 탐색 패킷 {0}", groupEP.ToString()), LogType.Packet); lstTerminal.Items.Clear();; }); sendUDPPacket("SHAREINPUTDEVICE_LIVE"); } break; case "SHAREINPUTDEVICE_LIVE": if (groupEP.Address.Equals(this.GetComputer_LanIP()) == false) { Invoke((MethodInvoker) delegate { addLog(string.Format("생존 채널 수신, 생존 패킷 {0}", groupEP.ToString()), LogType.Packet); lstTerminal.Items.Add(groupEP.Address.ToString()); }); } break; case "SHAREINPUTDEVICE_DIE": if (groupEP.Address.Equals(this.GetComputer_LanIP()) == false) { Invoke((MethodInvoker) delegate { addLog(string.Format("생존 채널 수신, 사망 패킷 {0}", groupEP.ToString()), LogType.Packet); lstTerminal.Items.Remove(groupEP.Address.ToString()); }); } break; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { m_ucLiveCheck.Close(); } } }); m_thrLiveCheck.Start(); btnConnect.Enabled = true; string strIP = GetComputer_LanIP().ToString(); m_thrListening = new Thread(delegate() { try { IPAddress localAddr = IPAddress.Parse(strIP); m_tlServer = new TcpListener(localAddr, cnst_iTcpPort); m_tlServer.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); m_tlServer.Start(); Byte[] bytes = new Byte[cnst_iBufferSize]; while (m_bRunning) { try { if (m_InputSimulator.InputDeviceState.IsKeyDown(WindowsInput.Native.VirtualKeyCode.LMENU) == true) { m_InputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LMENU); } if (m_InputSimulator.InputDeviceState.IsKeyDown(WindowsInput.Native.VirtualKeyCode.LSHIFT) == true) { m_InputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LSHIFT); } if (m_InputSimulator.InputDeviceState.IsKeyDown(WindowsInput.Native.VirtualKeyCode.LCONTROL) == true) { m_InputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LCONTROL); } Invoke((MethodInvoker) delegate { addLog("통신 채널 기동, 접속 수신 대기", LogType.Status); }); m_tcClient = m_tlServer.AcceptTcpClient(); //m_tcClient.NoDelay = true; while (!m_bAllowListen) { Invoke((MethodInvoker) delegate { addLog("통신 채널, 접속 불가 상태 ", LogType.Status); }); m_tcClient.Close(); m_tcClient = null; m_tcClient = m_tlServer.AcceptTcpClient(); } Invoke((MethodInvoker) delegate { addLog("통신 채널, 접속", LogType.Status); m_fnotiCurrent = new FixedNotification("원격 접속", "게스트에 의해 연결됨", FormAnimator.AnimationMethod.Center, FormAnimator.AnimationDirection.Up); m_fnotiCurrent.Show(); btnConnect.Enabled = false; }); NetworkStream stream = m_tcClient.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { if (bytes[0] == (byte)1) { globalKeyboardHook.keyboardPacket kp = (globalKeyboardHook.keyboardPacket)ByteToStructure(bytes); m_gkh.FireKeyEvent(m_InputSimulator.Keyboard, kp); Invoke((MethodInvoker) delegate { addLog(string.Format("메시지 수신 : 키보드 {0}, {1}", kp.flags == globalKeyboardHook.FLAG_KEYDOWN ? "누름\t" : "풀림\t", kp.vkCode), LogType.Packet); }); } else { globalMouseHook.mousePacket mp = (globalMouseHook.mousePacket)ByteToStructure(bytes); m_gmh.FireMouseEvent(m_InputSimulator.Mouse, mp); Invoke((MethodInvoker) delegate { addLog(globalMouseHook.getLogMessage(mp), LogType.Packet); }); } } m_tcClient.Close(); Invoke((MethodInvoker) delegate { if (m_fnotiCurrent != null) { m_fnotiCurrent.Close(); m_fnotiCurrent = null; } addLog("통신 채널, 게스트에 의해 접속 종료", LogType.Status); new Notification("알림", "게스트에 의해 접속 종료.", cnst_iAlarmWindow_DispalyInterval, FormAnimator.AnimationMethod.Slide, FormAnimator.AnimationDirection.Up).Show(); btnConnect.Enabled = true; }); } catch (SocketException se) { try { Invoke((MethodInvoker) delegate { addLog(string.Format("SocketException: {0}", se.Message.ToString()), LogType.Error); }); } catch (Exception ode) { } } catch (ThreadAbortException tae) { } catch (Exception ex) { try { Invoke((MethodInvoker) delegate { addLog(string.Format("Exception: {0}", ex.Message.ToString()), LogType.Error); }); } catch (Exception ode) { } } } } catch (SocketException se) { try { Invoke((MethodInvoker) delegate { addLog(string.Format("SocketException: {0}", se.Message.ToString()), LogType.Error); }); } catch (Exception ode) { } } catch (ThreadAbortException tae) { } catch (Exception ex) { try { Invoke((MethodInvoker) delegate { addLog(string.Format("Exception: {0}", ex.Message.ToString()), LogType.Error); }); } catch (Exception ode) { } } finally { if (m_tlServer != null) { m_tlServer.Stop(); } } }); m_thrListening.Start(); }