File: CubeMS.cs

package info (click to toggle)
assaultcube-data 1.0.4%2Brepack1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 60,024 kB
  • ctags: 15,165
  • sloc: cpp: 34,920; ansic: 20,515; xml: 3,864; sh: 3,408; objc: 975; cs: 350; makefile: 146
file content (147 lines) | stat: -rw-r--r-- 3,887 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Net;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Text;

using AHSoftware.CubeMon;
using AHSoftware.CubeMon.Games.ActionCube.v094;

/// <summary>
/// Summary description for CubeMS
/// </summary>
public class CubeMS
{
    string connectionString;

    public string ConnectionString
    {
        get { return connectionString; }
        set { connectionString = value; }
    }

    private bool AddServer(IPEndPoint address, string name, string description)
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("AddServer", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("IP", IPTools.IPToInt(address.Address));
        cmd.Parameters.AddWithValue("Port", address.Port);
        cmd.Parameters.AddWithValue("Name", name);
        cmd.Parameters.AddWithValue("Description", description);
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            con.Close();
        }
    }

    public bool Register(IPEndPoint address)
    {
        Client c = new Client();
        c.Socket = new IPEndPoint(address.Address, address.Port+1);
        ServerInfo info = null;
        try
        {
            info = (ServerInfo) c.RequestInfo();
        }
        catch
        {
            return false;
        }
        
        string name = string.Empty;
        try 
        { 
            name = Dns.GetHostEntry(info.Socket.Address).HostName;
        }
        catch { };

        return AddServer(address, name, info.Description);
    }

    public string GetServers(bool xml)
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(xml ? "GetServersXML" : "GetServers", con);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            
            if(xml)
            {
                // sql server handles xml conversion for us, just embed in a new xmldoc
                sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                sb.AppendLine("<Servers>");
                if(r.Read())
                {
                    sb.AppendLine(r[0] as string);
                }
                sb.AppendLine("</Servers>");
            }
            else
            {
                // construct server list as cubescript
                while(r.Read())
                {
                    sb.AppendFormat("addserver {0} {1};{2}", IPTools.IntToIp((int)r["IP"]).ToString(), r["Port"], Environment.NewLine);
                }
            }

            return sb.ToString();
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
        }
    }

    public string GetClientScript(string HTTPAgent)
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("GetClientScript", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("HTTPAgent", HTTPAgent);
        try
        {
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            r.Read();
            return r["Script"] as string;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
        }
    }
}