|
-
Dec 27th, 2008, 10:34 PM
#1
Thread Starter
New Member
[3.0/LINQ] CoD4 Rcon Class
I've searched and searched and searched. No matter what my search phrase, I run across this post. Here is my source, I don't necessarily need to be able to receive, in fact I hear you don't even need to receive...Anyway, the problem I seem to be encountering is this: I seem to be able to only send one command... :/
I've used Modern Rcon with all the *correct* information and everything works great...but my own attempt at a hand-written class fails. :/
Code:
namespace St0rmCoD4.Common.Data
{
using St0rmCoD4;
using System;
using System.Collections.Generic;
//using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
/// <summary>
/// CoD4 RCON Class
/// </summary>
public class Rcon
{
#region Singleton
private static Rcon m_Instance = null;
/// <summary>
/// Represents a Singleton instance of the RCON Object
/// </summary>
public static Rcon Instance
{
get
{
if (m_Instance == null)
{
m_Instance = new Rcon();
}
return m_Instance;
}
}
#endregion
#region Fields
private UdpClient m_Client = null;
private IPEndPoint m_EndPoint = null;
private bool m_Started = false;
#endregion
#region Properties
private string m_RconHost = string.Empty;
/// <summary>
/// Sets the Rcon Host address
/// </summary>
public string Host
{
set
{
m_RconHost = value;
}
}
private string m_RconPass = string.Empty;
/// <summary>
/// Sets the Rcon password
/// </summary>
public string Password
{
set
{
m_RconPass = value;
}
}
private int m_RconPort = 28960;
/// <summary>
/// Sets the Rcon port
/// </summary>
public int Port
{
set
{
m_RconPort = value;
}
}
#endregion
#region Events
public delegate void DataHandler(string data);
/// <summary>
/// Triggered when the Rcon server replies with data
/// </summary>
public event DataHandler OnData;
protected virtual void DataRecieveHandler(string data)
{
if (OnData != null)
{
OnData(data);
}
}
#endregion
#region Methods
/// <summary>
/// Call this before attempting to send otherwise an exception will be thrown!
/// </summary>
public void StartUp()
{
if (m_RconPass == string.Empty)
{
return;
}
if (m_Started) { return; }
m_Client = new UdpClient(m_RconPort);
m_EndPoint = new IPEndPoint(IPAddress.Parse(m_RconHost), m_RconPort);
new Thread(
() =>
{
byte[] buffer = new byte[1024 * 5];
while (true)
{
buffer = m_Client.Receive(ref m_EndPoint);
if (buffer.Length != 0)
{
DataRecieveHandler(Encoding.Default.GetString(buffer, 0, buffer.Length));
}
}
}
).Start();
m_Started = true;
}
public bool Send(string data)
{
if (m_Client == null)
{
throw new NullReferenceException("UdpClient is null; Did you forget to call StartUp()?");
}
string sendData = String.Format("\xFF\xFF\xFF\xFF rcon {0} {1}", m_RconPass, data);
byte[] dataSend = new byte[1024];
dataSend = Encoding.Default.GetBytes(sendData);
return (m_Client.Send(dataSend, dataSend.Length, m_EndPoint) > 0);
}
public bool Login()
{
if (m_Client == null)
{
throw new NullReferenceException("UdpClient is null; Did you forget to call StartUp()?");
}
string sendData = String.Format("\xFF\xFF\xFF\xFF rcon login {0}", m_RconPass);
byte[] dataSend = new byte[1024];
dataSend = Encoding.Default.GetBytes(sendData);
return (m_Client.Send(dataSend, dataSend.Length, m_EndPoint) > 0);
}
#endregion
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|