forked from kemot90/GameClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.cs
189 lines (176 loc) · 5.39 KB
/
Commands.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
187
188
189
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace Commands
{
public class ClientCmd
{
public const string LOGIN = "LOGIN";
public const string GET_PLAYER_DATA = "GET_PLAYER_DATA";
public const string GET_CHARACTER_DATA = "GET_CHARACTER_DATA";
public const string GET_CHARACTER_EQUIPMENT = "GET_CHARACTER_EQUIPMENT";
public const string GET_SKILLS = "GET_SKILLS";
public const string GET_CITIES = "GET_CITIES";
public const string UPDATE_DATA_BASE = "UPDATE_DATA_BASE";
}
public class ServerCmd
{
public const string PLAYER_DATA = "PLAYER_DATA";
public const string CHARACTER_DATA = "CHARACTER_DATA";
public const string CHARACTER_EQUIPMENT = "CHARACTER_EQUIPMENT";
public const string CITIES = "CITIES";
public const string SKILLS = "SKILLS";
public const string DATA_BASE_UPDATED = "DATA_BASE_UPDATED";
}
public class Command
{
private byte[] cmd;
private UTF8Encoding code;
private List<string> args = new List<string>();
private string cmdString = "";
private bool isRequest;
public Command()
{
cmdString = "";
code = new UTF8Encoding();
isRequest = false;
args.Clear();
}
//dodanie żądania/odpowiedzi w konstruktorze
public Command(string request)
{
cmdString = "";
code = new UTF8Encoding();
isRequest = false;
args.Clear();
this.Request(request);
}
//pobieranie żądania/odpowiedzi
//jako stringa
public string String
{
get
{
return cmdString;
}
}
//jako tablicę bajtów
public byte[] Byte
{
get
{
return cmd;
}
}
//funkcje dodające argumenty do listy argumentów
//dodanie jednego argumentu
public void Add(string argument)
{
args.Add(argument);
}
//dodanie tablicy argumentów
public void Add(string[] arguments)
{
foreach (string arg in arguments)
{
args.Add(arg);
}
}
//funkcje dodające argumenty do listy argumentów w konkretne miejsce
//dodanie jednego argumentu
public void Insert(int index, string argument)
{
args.Insert(index, argument);
}
//dodanie tablicy argumentów
public void Insert(int index, string[] arguments)
{
foreach (string arg in arguments)
{
args.Insert(index, arg);
index++;
}
}
//metoda ustawiająca jakiego typu akcja ma zostać wykonana
public void Request(string req)
{
if (!isRequest)
{
args.Insert(0, req);
isRequest = true;
}
else
{
args.RemoveAt(0);
args.Insert(0, req);
}
}
//zatwierdzanie argumentów i wysłanie przez gniazdo podane jako arguement
public string[] Apply(Socket client, bool ExpectedResponse = false)
{
//zmienne lokalne
//rozmiar paczki danych
int packageSize = 0;
//bufor do wczytywania danych wysłanych przez serwer
byte[] buf;
if (isRequest)
{
cmdString = "";
foreach (string arg in args)
{
cmdString += ";" + arg;
}
cmdString = cmdString.Remove(0, 1);
cmd = code.GetBytes(cmdString);
try
{
client.Send(BitConverter.GetBytes(cmd.Length));
client.Send(cmd);
while (ExpectedResponse)
{
if (client.Available > 0)
{
if (packageSize == 0)
{
buf = new byte[4];
client.Receive(buf);
packageSize = BitConverter.ToInt32(buf, 0);
}
else
{
buf = new byte[packageSize];
return CommandToArguments(code.GetString(buf, 0, client.Receive(buf)));
}
}
Thread.Sleep(1);
}
return null;
}
catch
{
return null;
}
}
else
{
return null;
}
}
//czyszczenie komendy
public void Clear()
{
cmd = null;
cmdString = null;
args.Clear();
isRequest = false;
}
//zamiana stringa cmd na żądanie i ciąg argumentów
private string[] CommandToArguments(string command)
{
string[] args = command.Split(';');
return args;
}
}
}