forked from dkuwahara/OmegaBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bnet.cs
executable file
·186 lines (143 loc) · 5.81 KB
/
Bnet.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BattleNet.Connections;
using System.Threading;
using BattleNet.Connections.Handlers;
using BattleNet.Connections.Readers;
using System.Net;
namespace BattleNet
{
class Bnet : IDisposable
{
#region Members
//Holds the connection and stream for the BNCS
BncsConnection m_bncsConnection;
// Reads packets from the BNCS stream and places it in a queue for the handler
BncsReader m_bncsReader;
Thread m_bncsReaderThread;
// Handles all the packets received and dispatches them appropriately
BncsHandler m_bncsHandler;
Thread m_bncsHandlerThread;
//Holds the connection and stream to the MCP
McpConnection m_mcpConnection;
// This class pulls all MCP packets from the stream and places it
// into a queue for the handler to use
McpReader m_mcpReader;
Thread m_mcpReaderThread;
//This class handles all of the received MCP Packets
McpHandler m_mcpHandler;
Thread m_mcpHandlerThread;
// Battle.Net server we are connecting to for this client
IPAddress m_server;
#endregion
#region Initializers
private void InitThreads(String account)
{
m_bncsReaderThread = new Thread(m_bncsReader.ThreadFunction);
m_bncsReaderThread.Name = account + " [BNCS]:";
m_bncsHandlerThread = new Thread(m_bncsHandler.ThreadFunction);
m_bncsHandlerThread.Name = account + " [BNCS]:";
m_mcpReaderThread = new Thread(m_mcpReader.ThreadFunction);
m_mcpReaderThread.Name = account + " [MCP]:";
m_mcpHandlerThread = new Thread(m_mcpHandler.ThreadFunction);
m_mcpHandlerThread.Name = account + " [MCP]:";
}
private void InitServers(String character, String account, String password,
Client.GameDifficulty difficulty, String classicKey,
String expansionKey, String exeInfo)
{
m_bncsConnection = new BncsConnection();
m_bncsReader = new BncsReader(ref m_bncsConnection);
m_bncsHandler = new BncsHandler(ref m_bncsConnection, account, password,
classicKey, expansionKey, exeInfo);
m_mcpConnection = new McpConnection();
m_mcpReader = new McpReader(ref m_mcpConnection);
m_mcpHandler = new McpHandler(ref m_mcpConnection, character, difficulty);
}
private void AssociateEvents()
{
m_bncsConnection.StartThread += m_bncsReaderThread.Start;
m_bncsConnection.StartThread += m_bncsHandlerThread.Start;
m_mcpConnection.StartThread += m_mcpReaderThread.Start;
m_mcpConnection.StartThread += m_mcpHandlerThread.Start;
m_bncsHandler.StartMcpThread += StartMcp;
m_bncsHandler.RealmUpdate += m_mcpHandler.UpdateRealm;
m_mcpHandler.BuildDispatchPacket += WriteBncsPacket;
}
#endregion
#region Constructors
public Bnet(IPAddress server, String character, String account, String password, Client.GameDifficulty difficulty, String classicKey, String expansionKey, String exeInfo)
{
m_server = server;
InitServers(character, account, password, difficulty, classicKey, expansionKey, password);
InitThreads(account);
AssociateEvents();
}
#endregion
#region Events
protected void WriteBncsPacket(byte command, params IEnumerable<byte>[] args)
{
byte[] packet = m_bncsConnection.BuildPacket(command, args);
m_bncsConnection.Write(packet);
}
protected void StartMcp(IPAddress server, ushort port, List<byte> data)
{
m_mcpConnection.Init(server, port, data);
}
#endregion
#region Subscribers
public void SubscribeCharacterNameUpdate(McpHandler.CharacterUpdateDel sub)
{
m_mcpHandler.UpdateCharacterName += sub;
}
public void SubscribeGameCreationThread(BncsHandler.GameCreationThreadHandler sub)
{
m_bncsHandler.StartGameCreationThread += sub;
}
public void SubscribeClassByteUpdate(McpHandler.SetByte sub)
{
m_mcpHandler.SetClassByte += sub;
}
public void SubscribeGameServerStart(McpHandler.D2gsStarter sub)
{
m_mcpHandler.StartGameServer += sub;
}
public void SubscribeStatusUpdates(GenericDispatcher.StatusUpdaterHandler sub)
{
m_bncsHandler.UpdateStatus += sub;
m_mcpHandler.UpdateStatus += sub;
}
#endregion
public void JoinGame(String gameName, String gamePass)
{
m_mcpHandler.JoinGame(gameName, gamePass);
}
public void MakeGame(Client.GameDifficulty difficulty, String gameName, String gamePass)
{
m_mcpHandler.MakeGame(difficulty, gameName, gamePass);
}
public void MakeRandomGame(Client.GameDifficulty difficulty)
{
m_mcpHandler.MakeRandomGame(difficulty);
}
// Connect to the BNCS and MCP
public bool Connect()
{
return m_bncsConnection.Init(m_server, 6112);
}
#region IDisposable Members
public void Close()
{
m_bncsConnection.Close();
m_mcpConnection.Close();
}
void IDisposable.Dispose()
{
m_bncsConnection.Close();
m_mcpConnection.Close();
}
#endregion
}
}