protected virtual void OnNetworkControl(NetworkControlEventArgs e) { if (NetworkControl != null) { if (FormMain.threadTrick.InvokeRequired) { FormMain.threadTrick.Invoke((MethodInvoker)(() => NetworkControl(this, e))); } } }
private void TcpServer_NetworkControl(TcpServer source, NetworkControlEventArgs e) { switch (e.EventId) //happens always { case NetworkControlEvent.SkipStart: int newSkipStartValue = e.Data.ReadInt(0); numericUpDownSkipStart.Value = newSkipStartValue; break; case NetworkControlEvent.SkipEnd: int newSkipEndValue = e.Data.ReadInt(0); numericUpDownSkipEnd.Value = newSkipEndValue; break; } if (player != null && !player.IsDisposed && player.IsLoaded) //player available { switch (e.EventId) { case NetworkControlEvent.PlayPause: Task.Run(async () => { if (await player.GetIsPlayingAsync()) { player.Pause(); } else { player.Play(); } }); break; case NetworkControlEvent.Next: player.Next(); break; case NetworkControlEvent.Previous: player.Previous(); break; case NetworkControlEvent.PlayEpisode: player.Play(selectedSeason, e.Data.ReadInt(0)); break; case NetworkControlEvent.ClosePlayer: player.Close(); break; case NetworkControlEvent.SeekTo: int pos = e.Data.ReadInt(0); player.SetPosition(pos); break; case NetworkControlEvent.ToggleFullscreen: player.Maximized = !player.Maximized; break; case NetworkControlEvent.Volume: player.SetVolume(e.Data[0]); break; } } else //player not available { switch (e.EventId) { case NetworkControlEvent.Volume: Settings.WriteValue(Settings.VOLUME, e.Data[0]); break; case NetworkControlEvent.PlayEpisode: player = new FormJwPlayer(); player.Open(currentProvider); player.EpisodeChange += Player_EpisodeChange; player.Play(selectedSeason, e.Data.ReadInt(0)); break; } } }
private void ClientReceived(IAsyncResult ar) { BufferedSocket bufSoc = (BufferedSocket)ar.AsyncState; int bytesReceived = bufSoc.EndReceive(ar); if (bytesReceived == 0) //client wants to disconnect { Logger.Log("TcpListener", "Client " + bufSoc.Socket.RemoteEndPoint + " disconnected."); bufSoc.Close(); clients.Remove(bufSoc); } else if (bytesReceived == -1) { Logger.Log("TcpListener", "Some client was disposed before disconnect ... probably my mistake, have to take a look at it."); clients.Remove(bufSoc); } else { byte[] buffer = bufSoc.ReceiveBuffer; NetworkEventType type = (NetworkEventType)buffer[0]; int specType = buffer[1]; byte messageId = buffer[2]; byte[] data = null; if (buffer.Length > 2) { data = new byte[bytesReceived - 3]; Array.Copy(buffer, 3, data, 0, data.Length); //Logger.Log("TcpListener", "Got packet from " + bufSoc.Socket.RemoteEndPoint.ToString() + " with following data: " + buffer.ToReadableString() + "."); } bufSoc.BeginReceive(bufSoc.ReceiveBuffer, 0, bufSoc.ReceiveBuffer.Length, SocketFlags.None, ClientReceived); switch (type) { case NetworkEventType.Control: var controlEvent = new NetworkControlEventArgs(bufSoc, (NetworkControlEvent)specType, messageId, data); OnNetworkControl(controlEvent); break; case NetworkEventType.Request: var requestEvent = new NetworkRequestEventArgs(bufSoc, (NetworkRequestEvent)specType, messageId, data); OnNetworkRequest(requestEvent); break; } } }