Beispiel #1
0
 public AsynchronousSocketListener(KMInterface mouseInterface)
 {
     mMouseInterface = mouseInterface;
 }
Beispiel #2
0
    public void ReadCallback(IAsyncResult ar)
    {
        try
        {
            String content = String.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            KMInterface mouseInterface = new KMInterface();

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);
            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                content = state.sb.ToString();
                if (content.Contains("<EOF>"))
                {
                    Console.WriteLine("Client disconnected");
                    handler.Shutdown(SocketShutdown.Both);
                }
                else
                {
                    if (content.Contains("LEFT_UP"))
                    {
                        Console.WriteLine("Left Up");
                        mouseInterface.DoMouseEvent(KMInterface.MouseEvent.LEFT_UP);
                        state.sb.Clear();
                    }
                    else if (content.Contains("LEFT_DOWN"))
                    {
                        Console.WriteLine("Left Down");
                        mouseInterface.DoMouseEvent(KMInterface.MouseEvent.LEFT_DOWN);
                        state.sb.Clear();
                    }
                    else if (content.Contains("RIGHT_UP"))
                    {
                        Console.WriteLine("Right Up");
                        mouseInterface.DoMouseEvent(KMInterface.MouseEvent.RIGHT_UP);
                        state.sb.Clear();
                    }
                    else if (content.Contains("RIGHT_DOWN"))
                    {
                        Console.WriteLine("Right Down");
                        mouseInterface.DoMouseEvent(KMInterface.MouseEvent.RIGHT_DOWN);
                        state.sb.Clear();
                    }
                    else if (content.Contains("VOL_UP"))
                    {
                        Console.WriteLine("Volume Up");
                        mouseInterface.DoVolumeEvent(KMInterface.VolumeEvent.VOL_UP);
                        state.sb.Clear();
                    }
                    else if (content.Contains("VOL_DOWN"))
                    {
                        Console.WriteLine("Volume Down");
                        mouseInterface.DoVolumeEvent(KMInterface.VolumeEvent.VOL_UP);
                        state.sb.Clear();
                    }
                    else if (content.Contains("VOL_MUTE"))
                    {
                        Console.WriteLine("Volume Mute");
                        mouseInterface.DoVolumeEvent(KMInterface.VolumeEvent.VOL_MUTE);
                        state.sb.Clear();
                    }
                    else if (content.Contains("<kb") && content.Contains(">"))
                    {
                        content = content.Substring(content.IndexOf("<kb")).Trim();
                        string[] parts        = content.Split(' ');
                        int      codePoint    = int.Parse(parts[1]);
                        bool     isShifted    = bool.Parse(parts[2]);
                        bool     isControlled = bool.Parse(parts[3]);
                        bool     isSupered    = bool.Parse(parts[4]);
                        bool     isAlted      = bool.Parse(parts[5]);
                        mouseInterface.KeyboardEvent(codePoint, isShifted, isControlled, isSupered, isAlted);
                        state.sb.Clear();
                    }

                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SOCKET FAILURE " + e.Message);
        }
    }