示例#1
0
    private void ReceiveServerMessages()
    {
        // show all new messages
        while (server.GetNextMessage(out Telepathy.Message msg))
        {
            switch (msg.eventType)
            {
            case Telepathy.EventType.Connected:
                Debug.Log($"client {msg.connectionId} is Connected");
                connectedClients.Add(msg.connectionId);
                SendNetworkMessageToClient(msg.connectionId, new SNetworkMessage(EMessageType.ConnectionSuccessful, msg.connectionId.ToString()));
                OnNewClient?.Invoke(msg.connectionId);
                break;

            case Telepathy.EventType.Disconnected:
                Debug.Log(msg.connectionId + " Disconnected");
                connectedClients.Remove(msg.connectionId);
                break;

            case Telepathy.EventType.Data:
                SNetworkMessage netMsg = JsonUtility.FromJson <SNetworkMessage>(Encoding.UTF8.GetString(msg.data));
                netMsg.clientID = msg.connectionId;
                //Debug.Log($"client({msg.connectionId}) has send a {netMsg.type} message.");
                SortServerMessages(netMsg);
                break;
            }
        }
    }
        private async void ListenCallback(IAsyncResult result)
        {
            // Recursive call to process another request using this same callback
            try
            {
                TcpListener.BeginAcceptTcpClient(ListenCallback, null);
            }
            catch (Exception)
            {
                if (IsStarted)
                {
                    throw;            //don't swallow too much!
                }
                return;
            }

            // Call End to complete the asynchronous operation.
            TcpClient tcpClient = TcpListener.EndAcceptTcpClient(result);

            NetworkStream stream = tcpClient.GetStream();

            using (StreamReader streamReader = new StreamReader(stream, Encoding.ASCII, false, 500, true))
                using (StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII, 5000, true))
                {
                    string inputStr = await RequestHeadingParser.RetrieveHttpRequestHeadingFromStream(streamReader, 2500);

                    LOGGER.Debug("Received request: {}", inputStr);

                    var request = RequestHeadingParser.ParseHttpRequestHeading(inputStr);

                    request.Headers.TryGetValue("Icy-Metadata", out string icyMetaData);

                    if (!string.Equals(request.Verb, "GET", StringComparison.InvariantCultureIgnoreCase))
                    {
                        await streamWriter.WriteAsync("HTTP 405 Method Not Allowed" + "\r\n");
                    }

                    bool isIcy;
                    // Checks if is a SHOUTCast client, or a normal HTTP client
                    if (!string.IsNullOrEmpty(icyMetaData) && Convert.ToInt64(icyMetaData) > 0)
                    {
                        isIcy = true;
                        await streamWriter.WriteAsync("ICY 200 OK" + "\r\n");

                        await streamWriter.WriteAsync("icy-metaint: " + Convert.ToString(RadioCaster.RadioInfo.MetadataInterval) + "\r\n");
                    }
                    else
                    {
                        isIcy = false;
                        await streamWriter.WriteAsync($"HTTP/{request.Version} 200 OK\r\n");
                    }

                    await streamWriter.WriteAsync("Content-Type: audio/mpeg" + "\r\n");

                    // Begin body
                    await streamWriter.WriteAsync("\r\n");

                    await streamWriter.FlushAsync();

                    var radioClient = isIcy ? (IRadioClient) new IcyRadioClient(stream, RadioCaster) : new RadioClient(stream);

                    OnNewClient?.Invoke(this, new NewClientEventArgs(radioClient));
                }
        }