// This queues a command for sending public void SendCommand(RemoteCommand cmd) { lock (sendcommands) { sendcommands.Enqueue(cmd); } }
// This queues a command for sending public void SendCommand(RemoteCommand cmd) { lock(sendcommands) { sendcommands.Enqueue(cmd); } }
// This creates a reply command public RemoteCommand CreateReply(string command) { RemoteCommand newcmd = new RemoteCommand(client); newcmd.source = this.target; newcmd.target = this.source; newcmd.command = command; return newcmd; }
// When a command is received public override void ReceiveCommand(RemoteCommand cmd) { // Raise event if (ReceiveCommandEvent != null) { ReceiveCommandEvent(cmd); } }
// This creates a reply command public RemoteCommand CreateReply(string command) { RemoteCommand newcmd = new RemoteCommand(client); newcmd.source = this.target; newcmd.target = this.source; newcmd.command = command; return(newcmd); }
// Internal update on network thread private void service_UpdateEvent() { lock (sendcommands) { // Send queued commands while (sendcommands.Count > 0) { RemoteCommand cmd = sendcommands.Dequeue(); service.SendCommand(cmd); } } }
// This processes a command public void ProcessCommand(RemoteCommand cmd) { // Verify that the target service exists if (!services.ContainsKey(cmd.Target)) { throw new InvalidDataException("Target service '" + cmd.Target + "' is not known."); } RemoteService svc = services[cmd.Target]; svc.ReceiveCommand(cmd); }
// Internal receive command event private void service_ReceiveCommandEvent(RemoteCommand cmd) { if (container.InvokeRequired) { container.Invoke(new EventRemoteService.ReceiveCommandDelegate(service_ReceiveCommandEvent), cmd); } else { if (ReceiveCommand != null) { ReceiveCommand(cmd); } } }
// Receive messages from remote service private void remote_ReceiveCommand(RemoteCommand cmd) { RemoteCommand reply; // Title requested switch (cmd.Command) { case "STATUS": reply = cmd.CreateReply("STATUS"); string statusinfo; if (isplaying) { if (ispaused) { statusinfo = "PAUSED\r\n"; } else { statusinfo = "PLAYING\r\n"; } statusinfo += itemtitle.Text + "\r\n"; statusinfo += medialength + "\r\n"; statusinfo += currentmediapos + "\r\n"; } else { statusinfo = "STOPPED\r\n"; } reply.SetData(statusinfo); remote.SendCommand(reply); break; case "STOP": reply = cmd.CreateReply("OK"); remote.SendCommand(reply); stopbutton_Click(stopbutton, EventArgs.Empty); break; case "PAUSE": reply = cmd.CreateReply("OK"); remote.SendCommand(reply); pausebutton_Click(pausebutton, EventArgs.Empty); break; } }
// This processes the client. // Returns false when the connection was closed. public bool Process(RemoteManager manager) { // Send data int maxsendbytes = Math.Min(SEND_RECEIVE_SIZE, (int)sendbuffer.Length); int bytessent = socket.Send(sendbuffer.ToArray(), 0, maxsendbytes, SocketFlags.None); int bytesremaining = (int)sendbuffer.Length - bytessent; MemoryStream oldbuffer = sendbuffer; sendbuffer = new MemoryStream(Math.Max(bytesremaining, SEND_RECEIVE_SIZE)); if (bytesremaining > 0) { sendbuffer.Write(oldbuffer.ToArray(), bytessent, bytesremaining); } oldbuffer.Dispose(); // Receive data byte[] datablock = new byte[SEND_RECEIVE_SIZE]; int bytesreceived = socket.Receive(datablock); if (bytesreceived == 0) { // When Receive returns 0 bytes the connection was closed return(false); } else { // Receive new data from client NotifyResponse(); ReceiveData(datablock, bytesreceived); // Parse and process commands RemoteCommand c = TryParseCommand(); while (c != null) { manager.ProcessCommand(c); c = TryParseCommand(); } return(true); } }
// This attempts to parse a command and returns the command when complete public RemoteCommand TryParseCommand() { // If we're not working on a command yet, make one now if (receivecommand == null) { receivecommand = new RemoteCommand(this); } // Try parsing if (receivecommand.TryParse(ref receivebuffer)) { // When complete, return the command and forget about this command RemoteCommand cmd = receivecommand; receivecommand = null; return(cmd); } else { return(null); } }
// Receive messages from remote service private void remote_ReceiveCommand(RemoteCommand cmd) { RemoteCommand reply; // Title requested switch(cmd.Command) { case "STATUS": reply = cmd.CreateReply("STATUS"); string statusinfo; if(isplaying) { if(ispaused) statusinfo = "PAUSED\r\n"; else statusinfo = "PLAYING\r\n"; statusinfo += itemtitle.Text + "\r\n"; statusinfo += medialength + "\r\n"; statusinfo += currentmediapos + "\r\n"; } else { statusinfo = "STOPPED\r\n"; } reply.SetData(statusinfo); remote.SendCommand(reply); break; case "STOP": reply = cmd.CreateReply("OK"); remote.SendCommand(reply); stopbutton_Click(stopbutton, EventArgs.Empty); break; case "PAUSE": reply = cmd.CreateReply("OK"); remote.SendCommand(reply); pausebutton_Click(pausebutton, EventArgs.Empty); break; } }
// This sends a command to the client public void SendCommand(RemoteCommand cmd) { cmd.WriteTo(sendbuffer); }
// This attempts to parse a command and returns the command when complete public RemoteCommand TryParseCommand() { // If we're not working on a command yet, make one now if(receivecommand == null) receivecommand = new RemoteCommand(this); // Try parsing if(receivecommand.TryParse(ref receivebuffer)) { // When complete, return the command and forget about this command RemoteCommand cmd = receivecommand; receivecommand = null; return cmd; } else { return null; } }
// This sends a command! public void SendCommand(RemoteCommand cmd) { cmd.Client.SendCommand(cmd); }
// Receiving a command! // The service should implement this to handle the command. public virtual void ReceiveCommand(RemoteCommand cmd) { }
// This processes a command public void ProcessCommand(RemoteCommand cmd) { // Verify that the target service exists if(!services.ContainsKey(cmd.Target)) throw new InvalidDataException("Target service '" + cmd.Target + "' is not known."); RemoteService svc = services[cmd.Target]; svc.ReceiveCommand(cmd); }
// Internal receive command event private void service_ReceiveCommandEvent(RemoteCommand cmd) { if(container.InvokeRequired) { container.Invoke(new EventRemoteService.ReceiveCommandDelegate(service_ReceiveCommandEvent), cmd); } else { if(ReceiveCommand != null) ReceiveCommand(cmd); } }
// When a command is received public override void ReceiveCommand(RemoteCommand cmd) { // Raise event if(ReceiveCommandEvent != null) ReceiveCommandEvent(cmd); }