internal static void OnIncomingPacket(int client, byte[] data) { lock (myIncomingPacketLock) { dIncomingPacket handler = myIncomingPacketEvent; try { if (handler != null) { handler(client, data); } } catch (Exception e) { } } }
/// <summary> /// Process IPC message and fire corresponding event. /// </summary> /// <param name="message"> /// IPC message to process. /// </param> /// <param name="instance"> /// Instance of the IPC server that received the message. /// </param> /* Since there is one server for each client server instance = client instance */ private static void ProcessMessage(byte[] message, int instance) { Command command = (Command)message[0]; switch (command) { case Command.Ping: dPing ping = PingEvent; if (ping != null) { ThreadPool.QueueUserWorkItem(delegate { ping(instance); }); } return; case Command.PingResponse: dPingResponse pingResponse = PingResponseEvent; if (pingResponse != null) { ThreadPool.QueueUserWorkItem(delegate { pingResponse(instance); }); } return; case Command.Exception: dException exception = ExceptionEvent; if (exception != null) { string exceptionString = UnicodeEncoding.Unicode.GetString(message, 3, message.Length - 3); ThreadPool.QueueUserWorkItem(delegate { exception(instance, exceptionString); }); } return; case Command.Message: dMessage dmessage = MessageEvent; if (dmessage != null) { string messageString = UnicodeEncoding.Unicode.GetString(message, 3, message.Length - 3); ThreadPool.QueueUserWorkItem(delegate { dmessage(instance, messageString); }); } return; case Command.ClientID: dClientID clientid = ClientIDEvent; if (clientid != null) { int pid = BitConverter.ToInt32(message, 1); ThreadPool.QueueUserWorkItem(delegate { clientid(instance, pid); }); } return; case Command.ClientVersion: dClientVersion clientVersion = ClientVersionEvent; if (clientVersion != null) { int version = BitConverter.ToInt32(message, 1); ThreadPool.QueueUserWorkItem(delegate { clientVersion(instance, version); }); } return; case Command.FunctionPointer: dFunctionPointer functionPointer = FunctionPointerEvent; if (functionPointer != null) { uint address = BitConverter.ToUInt32(message, 1); ThreadPool.QueueUserWorkItem(delegate { functionPointer(instance, address, (FunctionType)message[5]); }); } return; case Command.KeyDown: dKeyDown keyDown = KeyDownEvent; if (keyDown != null) { int downCode = BitConverter.ToInt32(message, 1); ThreadPool.QueueUserWorkItem(delegate { keyDown(instance, (Keys)downCode); }); } return; case Command.KeyUp: dKeyUp keyUp = KeyUpEvent; if (keyUp != null) { int upCode = BitConverter.ToInt32(message, 1); ThreadPool.QueueUserWorkItem(delegate { keyUp(instance, (Keys)upCode); }); } return; case Command.MouseMove: dMouseMove mouseMove = MouseMoveEvent; if (mouseMove != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); ThreadPool.QueueUserWorkItem(delegate { mouseMove(instance, x, y); }); } return; case Command.MouseWheel: dMouseWheel mouseWheel = MouseWheelEvent; if (mouseWheel != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); ThreadPool.QueueUserWorkItem(delegate { mouseWheel(instance, x, y, (sbyte)message[9]); }); } return; case Command.MouseDblClick: dMouseDblClick mouseDblClick = MouseDblClickEvent; if (mouseDblClick != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); MouseButtons button; switch (message[9]) { case 1: button = MouseButtons.Left; break; case 2: button = MouseButtons.Middle; break; case 3: button = MouseButtons.Right; break; default: //should never happen button = MouseButtons.None; break; } ThreadPool.QueueUserWorkItem(delegate { mouseDblClick(instance, x, y, button); }); } return; case Command.MouseClick: dMouseClick mouseClick = MouseClickEvent; if (mouseClick != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); MouseButtons button; switch (message[9]) { case 1: button = MouseButtons.Left; break; case 2: button = MouseButtons.Middle; break; case 3: button = MouseButtons.Right; break; default: //should never happen button = MouseButtons.None; break; } ThreadPool.QueueUserWorkItem(delegate { mouseClick(instance, x, y, button); }); } return; case Command.MouseDown: dMouseDown mouseDown = MouseDownEvent; if (mouseDown != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); MouseButtons button; switch (message[9]) { case 1: button = MouseButtons.Left; break; case 2: button = MouseButtons.Middle; break; case 3: button = MouseButtons.Right; break; default: //should never happen button = MouseButtons.None; break; } ThreadPool.QueueUserWorkItem(delegate { mouseDown(instance, x, y, button); }); } return; case Command.MouseUp: dMouseUp mouseUp = MouseUpEvent; if (mouseUp != null) { int x = BitConverter.ToInt32(message, 1); int y = BitConverter.ToInt32(message, 5); MouseButtons button; switch (message[9]) { case 1: button = MouseButtons.Left; break; case 2: button = MouseButtons.Middle; break; case 3: button = MouseButtons.Right; break; default: //should never happen button = MouseButtons.None; break; } ThreadPool.QueueUserWorkItem(delegate { mouseUp(instance, x, y, button); }); } return; case Command.IncomingPacket: dIncomingPacket incomingPacket = IncomingPacketEvent; if (incomingPacket != null) { byte[] inPacket = new byte[message.Length - 3]; Buffer.BlockCopy(message, 3, inPacket, 0, message.Length - 3); ThreadPool.QueueUserWorkItem(delegate { incomingPacket(instance, inPacket); }); } return; case Command.OutgoingPacket: dOutgoingPacket outgoingPacket = OutgoingPacketEvent; if (outgoingPacket != null) { byte[] outPacket = new byte[message.Length - 3]; Buffer.BlockCopy(message, 3, outPacket, 0, message.Length - 3); ThreadPool.QueueUserWorkItem(delegate { outgoingPacket(instance, outPacket); }); } return; default: return; } }