Don't forget to call EndSend to release cached async stuff

This commit is contained in:
UnknownShadow200 2017-05-16 00:07:33 +10:00
parent cb5310736d
commit 52dc099198
3 changed files with 23 additions and 11 deletions

View File

@ -33,12 +33,12 @@ namespace MCGalaxy.Network {
get { return ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(); } get { return ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(); }
} }
static AsyncCallback recvCallback = new AsyncCallback(Receive); static AsyncCallback recvCallback = new AsyncCallback(ReceiveCallback);
public void ReceiveNextAsync() { public void ReceiveNextAsync() {
socket.BeginReceive(tempbuffer, 0, tempbuffer.Length, SocketFlags.None, recvCallback, this); socket.BeginReceive(tempbuffer, 0, tempbuffer.Length, SocketFlags.None, recvCallback, this);
} }
static void Receive(IAsyncResult result) { static void ReceiveCallback(IAsyncResult result) {
TcpSocket s = (TcpSocket)result.AsyncState; TcpSocket s = (TcpSocket)result.AsyncState;
Player p = s.player; Player p = s.player;
if (p.disconnected) return; if (p.disconnected) return;
@ -73,15 +73,17 @@ namespace MCGalaxy.Network {
} }
} }
static AsyncCallback sendCallback = new AsyncCallback(SendCallback);
public void Send(byte[] buffer, bool sync = false) { public void Send(byte[] buffer, bool sync = false) {
// Abort if socket has been closed // Abort if socket has been closed
if (player.disconnected || !socket.Connected) return; if (player.disconnected || !socket.Connected) return;
try { try {
if (sync) if (sync)
socket.Send(buffer, 0, buffer.Length, SocketFlags.None); socket.Send(buffer, 0, buffer.Length, SocketFlags.None);
else else
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, delegate(IAsyncResult result) { }, null); socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, sendCallback, this);
buffer = null; buffer = null;
} catch (SocketException e) { } catch (SocketException e) {
buffer = null; buffer = null;
@ -95,6 +97,22 @@ namespace MCGalaxy.Network {
} }
} }
static void SendCallback(IAsyncResult result) {
TcpSocket s = (TcpSocket)result.AsyncState;
try {
int sent = s.socket.EndSend(result);
} catch (SocketException e) {
s.player.Disconnect();
#if DEBUG
Server.ErrorLog(e);
#endif
} catch (ObjectDisposedException) {
// socket was already closed by another thread.
}
}
public void Close() { public void Close() {
// Try to close the socket. // Try to close the socket.
// Sometimes its already closed so these lines will cause an error // Sometimes its already closed so these lines will cause an error

View File

@ -242,12 +242,6 @@ namespace MCGalaxy {
public static Group findPermInt(int Perm) { public static Group findPermInt(int Perm) {
return GroupList.Find(grp => (int)grp.Permission == Perm); return GroupList.Find(grp => (int)grp.Permission == Perm);
} }
/// <summary> Get the group name that player /playerName/ is in </summary>
/// <param name="name">The player Name</param>
/// <returns>The group name</returns>
public static string findPlayer(string name) { return findPlayerGroup(name).name; }
/// <summary> Find the group object that the player /playerName/ is in </summary> /// <summary> Find the group object that the player /playerName/ is in </summary>
/// <param name="name">The player name</param> /// <param name="name">The player name</param>

View File

@ -59,7 +59,7 @@ namespace MCGalaxy {
} if (perm > 119 || perm < -50) { } if (perm > 119 || perm < -50) {
Server.s.Log("Permission must be between -50 and 119 for ranks"); Server.s.Log("Permission must be between -50 and 119 for ranks");
grp = null; grp = null;
} else if (Group.GroupList.Find(g => g.Permission == (LevelPermission)perm) == null) { } else if (Group.findPermInt(perm) == null) {
grp.Permission = (LevelPermission)perm; grp.Permission = (LevelPermission)perm;
} else { } else {
Server.s.Log("Cannot have 2 ranks set at permission level " + value); Server.s.Log("Cannot have 2 ranks set at permission level " + value);