Exemple #1
0
 public void Connect()
 {
     try
     {
         if (!Connected)
         {
             bool b = true;
             if (BeforeConnectEvent != null)
             {
                 b = BeforeConnectEvent();
             }
             if (b)
             {
                 ServerInformations si = GetServerInformations();
                 SelectedServer = si[0];
                 if (SelectServerEvent != null)
                 {
                     SelectedServer = SelectServerEvent(si);
                 }
                 if (SelectedServer != null)
                 {
                     System.Net.Sockets.Socket socket = null;
                     try
                     {
                         ManuelResetEvent.Reset();
                         socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                         System.IAsyncResult ar = socket.BeginConnect(SelectedServer.EndPoint, new System.AsyncCallback(this.ClientConnectCallback), socket);
                         ManuelResetEvent.WaitOne();
                     }
                     catch (System.Exception ex)
                     {
                         if (socket != null)
                         {
                             if (socket.Connected)
                             {
                                 socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                             }
                             socket.Close();
                         }
                         throw ex;
                     }
                 }
                 if (AfterConnectEvent != null)
                 {
                     AfterConnectEvent();
                 }
                 if (Game.UpdateStockInformationsEvent != null)
                 {
                     Game.UpdateStockInformationsEvent();
                 }
                 if (Game.UpdatePlayerInformationsEvent != null)
                 {
                     Game.UpdatePlayerInformationsEvent();
                 }
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemple #2
0
        private void ClientConnectCallback(System.IAsyncResult ar)
        {
            System.Net.Sockets.Socket clientsocket = (System.Net.Sockets.Socket)ar.AsyncState;
            bool disconnecting = false;

            try
            {
                clientsocket.EndConnect(ar);
                StartCommunication(clientsocket);
                ManuelResetEvent.Set();
                Communication(clientsocket);
            }
            catch (System.ObjectDisposedException)
            {
                // The socket has been closed.
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                switch (ex.SocketErrorCode)
                {
                case System.Net.Sockets.SocketError.ConnectionAborted:
                    // Don't throw an excetion, the connection has just aborted.
                    disconnecting = true;
                    break;

                default:
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (clientsocket.Connected)
                {
                    clientsocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                    clientsocket.Disconnect(false);
                }
                clientsocket.Close();
                if (disconnecting)
                {
                    lock (SynchronizeRoot)
                    {
                        foreach (DepositContent content in Game.CurrentPlayer.Deposit.Values)
                        {
                            if (content.OnClientBuyStocksEvent != null)
                            {
                                content.OnClientBuyStocksEvent = null;
                            }
                            if (content.OnClientSellStocksEvent != null)
                            {
                                content.OnClientSellStocksEvent = null;
                            }
                        }
                        if (Socket != null)
                        {
                            Socket = null;
                        }
                    }
                    if (SelectedServer != null)
                    {
                        SelectedServer = null;
                    }
                    if (AfterDisconnectEvent != null)
                    {
                        Synchronize.Invoke(AfterDisconnectEvent, null);
                    }
                    if (Game.UpdateStockInformationsEvent != null)
                    {
                        Synchronize.Invoke(Game.UpdateStockInformationsEvent, null);
                    }
                    if (Game.UpdatePlayerInformationsEvent != null)
                    {
                        Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                    }
                }
            }
        }
Exemple #3
0
 public void Dispose(bool disposing)
 {
     try
     {
         if (!Disposed)
         {
             if (Connected)
             {
                 if (BeforeDisconnectEvent != null)
                 {
                     BeforeDisconnectEvent = null;
                 }
                 if (AfterDisconnectEvent != null)
                 {
                     AfterDisconnectEvent = null;
                 }
                 if (OnPlayerDisconnectedEvent != null)
                 {
                     OnPlayerDisconnectedEvent = null;
                 }
                 Game.UpdateStockInformations usi = Game.UpdateStockInformationsEvent;
                 Game.UpdateStockInformationsEvent = null;
                 Game.UpdatePlayerInformations upi = Game.UpdatePlayerInformationsEvent;
                 Game.UpdatePlayerInformationsEvent = null;
                 Disconnect();
                 Game.UpdateStockInformationsEvent  = usi;
                 Game.UpdatePlayerInformationsEvent = upi;
             }
             if (ManuelResetEvent != null)
             {
                 ManuelResetEvent.Close();
             }
             if (disposing)
             {
                 if (Game != null)
                 {
                     Game = null;
                 }
                 if (Version != null)
                 {
                     Version = null;
                 }
                 if (ManuelResetEvent != null)
                 {
                     ManuelResetEvent = null;
                 }
                 if (SynchronizeRoot != null)
                 {
                     SynchronizeRoot = null;
                 }
                 if (BeforeConnectEvent != null)
                 {
                     BeforeConnectEvent = null;
                 }
                 if (AfterConnectEvent != null)
                 {
                     AfterConnectEvent = null;
                 }
                 if (BeforeDisconnectEvent != null)
                 {
                     BeforeDisconnectEvent = null;
                 }
                 if (AfterDisconnectEvent != null)
                 {
                     AfterDisconnectEvent = null;
                 }
                 if (SelectServerEvent != null)
                 {
                     SelectServerEvent = null;
                 }
                 if (OnPlayerConnectedEvent != null)
                 {
                     OnPlayerConnectedEvent = null;
                 }
                 if (OnPlayerDisconnectedEvent != null)
                 {
                     OnPlayerDisconnectedEvent = null;
                 }
             }
             Disposed = true;
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }