protected virtual void OnNetworkRequest(NetworkRequestEventArgs e) { if (NetworkRequest != null) { if (FormMain.threadTrick.InvokeRequired) { FormMain.threadTrick.Invoke((MethodInvoker)(() => NetworkRequest(this, e))); } } }
private void TcpServer_NetworkRequest(TcpServer source, NetworkRequestEventArgs e) { switch (e.EventId) { case NetworkRequestEvent.PlayerStatus: Task.Run(async () => { PlayerStatusMessage psm; if (player != null && !player.IsDisposed && player.IsLoaded) { //just cut off the position and length because they SHOULD never exceed the maximum int psm = new PlayerStatusMessage(e.MessageId, await player.GetIsPlayingAsync(), (int)await player.GetPositionAsync(), (int)await player.GetDurationAsync(), await player.GetBufferPercentAsync(), (byte)await player.GetVolumeAsync()); } else { byte volume = (byte)Settings.GetNumber(Settings.VOLUME); psm = new PlayerStatusMessage(e.MessageId, false, 0, 0, 0, volume); } if (source.IsRunning) { source.SendToClient(e.Socket, psm); } else { Logger.Log("TCP", "Trying to send PlayerStatusMessage with closed tcp server!"); } }); break; case NetworkRequestEvent.EpisodeList: if (currentProvider != null) { Series s = currentProvider.GetSeries(); foreach (List<Episode> season in s.Seasons) { foreach(Episode episode in season) { source.SendToClient(e.Socket, new EpisodeListNetworkMessage(e.MessageId, episode)); } } source.SendToClient(e.Socket, new EpisodeListNetworkMessage(e.MessageId, null)); //send last episode marker } 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; } } }