Inheritance Hierarchy
Un4seen.Bass.MiscStreamingServer
Un4seen.Bass.MiscICEcast
Un4seen.Bass.MiscSHOUTcast
Un4seen.Bass.MiscWMAcast
Namespace: Un4seen.Bass.Misc
Assembly: Bass.Net (in Bass.Net.dll) Version: 2.4.17.5
Syntax
The StreamingServer type exposes the following members.
Constructors
Name | Description | |
---|---|---|
StreamingServer(IBaseEncoder) |
Default constructor, creating an instance for a streaming server implementation (using BASSenc as the default processor).
| |
StreamingServer(IBaseEncoder, Boolean) |
Default constructor, creating an instance for a streaming server implementation.
|
Properties
Name | Description | |
---|---|---|
Encoder |
Returns the encoder interface which is used with this instance.
| |
ForceUTF8TitleUpdates |
Gets or Sets if title updates (via UpdateTitle(String, String)) should in any case use UTF-8 (default is ).
| |
IsConnected |
Returns , if connected to a boadcast server - else .
| |
LastError |
Gets or Sets the last streaming error.
| |
LastErrorMessage |
Gets or Sets the last streaming error message.
| |
LocalNetworkCardIPAddress |
Gets or Sets a local IP address to use for cast server connections (=any, default):
| |
SongTitle |
The current/last song title.
| |
SongUrl |
The current/last song url.
| |
UseBASS |
Gets if native BASSenc should be used to connect and stream to the server (as specified in the constructor).
|
Methods
Name | Description | |
---|---|---|
Connect |
Connects to a streaming server.
| |
Disconnect |
Disconnects from a streaming server.
| |
Dispose |
Implement IDisposable.
| |
Finalize |
Finalization code.
(Overrides ObjectFinalize.) | |
GetIPfromHost |
Resolved a given host name (DNS or IP) to its possible IP addesses.
| |
GetListeners |
Returns the number of listeners currently connected.
| |
GetStats |
Returns the XML stats of the server.
| |
Login |
Performs a login to and/or initializes the streaming server.
| |
SendData |
Sends encoded sample data (manually) to the streaming server (e.g. the data as received in an own ENCODEPROC).
| |
UpdateTitle(String, String) |
Updates the song title of the streaming server.
| |
UpdateTitle(TAG_INFO, String) |
Updates the song title of the streaming server.
|
Remarks
The properties Encoder, LastError and LastErrorMessage have been already implemented.
You might use this base class to derive your own streaming server implementations.
If an instance of a derived class is being disposed, an already created connection to a streaming server will automatically be closed (using Disconnect) and an Encoder will be stopped, if active.
Examples
public class MyOwnServer : StreamingServer { // Constructor public MyOwnServer(IBaseEncoder encoder, bool useBASS) : base(encoder, useBASS) { // validity check if (encoder.EncoderType != BASSChannelType.BASS_CTYPE_STREAM_MP3 && encoder.EncoderType != BASSChannelType.BASS_CTYPE_STREAM_AAC) throw new Exception( "Invalid EncoderType (only MP3 and AAC is supported)!" ); } private Socket _socket = null; private bool _loggedIn = false; private byte[] _data = null; private object _lock = false; public string ServerAddress = "localhost"; public int ServerPort = 8000; public override bool IsConnected { get { if (_socket != null) return _socket.Connected && _loggedIn; else return false; } } public override bool Connect() { // check the encoder if (!Encoder.IsActive) { LastError = STREAMINGERROR.Error_EncoderError; LastErrorMessage = "Encoder not active!"; return false; } // close any connections, if still open if (_socket != null && _socket.Connected) { _socket.Close(); _socket = null; } // create a connection at port+1 _socket = CreateSocket( ServerAddress, ServerPort+1 ); return (_socket != null && _socket.Connected); } public override bool Disconnect() { bool ok = false; try { _socket.Close(); } catch { } finally { if (_socket != null && _socket.Connected) { LastError = STREAMINGERROR.Error_Disconnect; LastErrorMessage = "Winsock error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } else { ok = true; _socket = null; _loggedIn = false; } } return ok; } public override bool Login() { if (_socket == null) { LastError = STREAMINGERROR.Error_NotConnected; LastErrorMessage = "Not connected to server."; return false; } bool ok = false; if ( MyLogin() ) { if ( MyInit() ) { ok = true; _loggedIn = true; LastError = STREAMINGERROR.Ok; LastErrorMessage = String.Empty; } else { LastError = STREAMINGERROR.Error_Login; LastErrorMessage = "Server could not be initialized."; } } else { LastError = STREAMINGERROR.Error_Login; LastErrorMessage = "Invalid username or password."; } return ok; } public override int SendData(IntPtr buffer, int length) { if (buffer == IntPtr.Zero || length == 0) return 0; int sendData = -1; try { lock (_lock) { // dynamic buffer allocation if (_data == null || _data.Length < length) _data = new byte[length]; Marshal.Copy(buffer, _data, 0, length); sendData = _socket.Send( _data, 0, length, SocketFlags.None ); if (sendData < 0) { LastError = STREAMINGERROR.Error_SendingData; LastErrorMessage = String.Format( "{0} bytes not send.", length); Disconnect(); } else if (sendData != length) { LastError = STREAMINGERROR.Warning_LessDataSend; LastErrorMessage = String.Format( "{0} of {1} bytes send.", sendData, length); } } } catch (Exception e) { LastError = STREAMINGERROR.Error_SendingData; LastErrorMessage = e.Message; sendData = -1; Disconnect(); } return sendData; } public override bool UpdateTitle(string song, string url) { SongTitle = song; return MyUpdateTitle(song); } private Socket CreateSocket(string serveraddress, int port) { Socket socket = null; IPHostEntry hostEntry = null; try { // Get host related information hostEntry = Dns.GetHostEntry(serveraddress); // Loop through the AddressList to obtain the supported AddressFamily. foreach(IPAddress address in hostEntry.AddressList) { try { IPEndPoint ipe = new IPEndPoint(address, port); Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(ipe); if (tempSocket.Connected) { socket = tempSocket; break; } else { continue; } } catch (Exception e) { LastError = STREAMINGERROR.Error_CreatingConnection; LastErrorMessage = e.Message; socket = null; } } } catch (Exception e) { LastError = STREAMINGERROR.Error_ResolvingServerAddress; LastErrorMessage = e.Message; socket = null; } return socket; } private bool MyLogin() { if (_socket == null) return false; // e.g. send password here using the "_socket" ... } private bool MyInit() { if (_socket == null) return false; // e.g. send some init data to the server here using the "_socket" ... } private bool MyUpdateTitle(string song) { bool ok = false; Socket socket = null; try { socket = CreateSocket( ServerAddress, ServerPort ); if (socket != null) { // send the song title using the created "socket" ... ok = true; } } catch { } finally { // disconnect if (socket != null) { socket.Close(); socket = null; } } return ok; } }
See Also