// Set the receive buffer and post a receive op. private void StartReceive(GameServer re) { // Post async receive operation on the socket. bool willRaiseEvent = false; try { willRaiseEvent = re.connection.RecvSocket.AcceptSocket.ReceiveAsync(re.connection.RecvSocket); } catch (ObjectDisposedException) { HandleBadConnection(re.connection); InGameUsers.RemoveGameServer(re.connection.TokenID); willRaiseEvent = true; } //catch (NullReferenceException) //{ // HandleBadConnection(re.connection); // InGameUsers.RemoveGameServer(re.connection.TokenID); // willRaiseEvent = true; //} if (!willRaiseEvent) { //If the op completed synchronously, we need to call ProcessReceive method directly. ProcessReceive(re.connection.RecvSocket); } }
public void CleanUpOnExit() { for (int i = gServerList.Count - 1; i >= 0; i--) { GameServer gs = gServerList[i]; gs.connection.Close(); InGameUsers.RemoveGameServer(gs.connection.TokenID); } }
private void ProcessDisconnectAndCloseSocket(SocketAsyncEventArgs receiveSendEventArgs) { GameServer receiveSendToken = (GameServer)receiveSendEventArgs.UserToken; Output.WriteLine("WorldConnectionListener::ProcessDisconnect(), ID = " + receiveSendToken.connection.TokenID.ToString()); if (receiveSendEventArgs.SocketError != SocketError.Success) { Output.WriteLine("WorldConnectionListener::ProcessDisconnect ERROR, id " + receiveSendToken.connection.TokenID.ToString()); } //This method closes the socket and releases all resources, both //managed and unmanaged. It internally calls Dispose. receiveSendToken.connection.Close(); InGameUsers.RemoveGameServer(receiveSendToken.connection.TokenID); }
// This method is invoked by the IO_Completed method when an asynchronous receive operation completes. private void ProcessReceive(SocketAsyncEventArgs re) { // If there was a socket error, close the connection. This is NOT a normal situation, if you get an error here. if (re.SocketError != SocketError.Success) { if (Program.DEBUG_Game_Recv) { Output.WriteLine(ConsoleColor.Red, "WorldConnectionListener::ProcessReceive " + "Receive ERROR"); } HandleBadConnection(((GameServer)re.UserToken).connection); InGameUsers.RemoveGameServer(((GameServer)re.UserToken).connection.TokenID); return; } // If no data was received, close the connection. This is a NORMAL situation that shows when the client has finished sending data = closed connection if (re.BytesTransferred == 0) { if (Program.DEBUG_Game_Recv) { Output.WriteLine("WorldConnectionListener::ProcessReceive " + "Receive NO DATA"); } HandleBadConnection(((GameServer)re.UserToken).connection); InGameUsers.RemoveGameServer(((GameServer)re.UserToken).connection.TokenID); return; } //The BytesTransferred property tells us how many bytes we need to process. Int32 remainingBytesToProcess = re.BytesTransferred; //if (Program.DEBUG_Game_Recv) Output.WriteLine("RECV packet length: " + remainingBytesToProcess.ToString()); int status = ((GameServer)re.UserToken).connection.Recv(remainingBytesToProcess); if (status == -1)// ther was criticall error in recv packet { if (Program.DEBUG_Game_Recv) { Output.WriteLine("WorldConnectionListener::ProcessReceive Error in packet processing - close connection"); } HandleBadConnection(((GameServer)re.UserToken).connection); InGameUsers.RemoveGameServer(((GameServer)re.UserToken).connection.TokenID); return; } //wait for next packet StartReceive((GameServer)re.UserToken); }
private void ProcessSend(SocketAsyncEventArgs receiveSendEventArgs) { GameServer receiveSendToken = (GameServer)receiveSendEventArgs.UserToken; if (receiveSendEventArgs.SocketError == SocketError.Success) { if (Program.DEBUG_Game_Send) { Output.WriteLine("WorldConnectionListener::ProcessSend Send Success, id " + receiveSendToken.connection.TokenID.ToString()); } } else { //If we are in this else-statement, there was a socket error. if (Program.DEBUG_Game_Send) { Output.WriteLine("WorldConnectionListener::ProcessSend ERROR, id " + receiveSendToken.connection.TokenID.ToString()); } receiveSendToken.connection.Close(); InGameUsers.RemoveGameServer(receiveSendToken.connection.TokenID); } }