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 ServerInformations GetServerInformations()
        {
            try
            {
                ServerInformations si = new ServerInformations();
                if (Version.Major > 0)
                {
                    System.Collections.Generic.List <System.Net.Sockets.Socket> sockets = new System.Collections.Generic.List <System.Net.Sockets.Socket>();
                    try
                    {
                        // Initialize and bind sockets.
                        bool containsloopback = false;
                        foreach (System.Net.IPAddress ipaddress in System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
                        {
                            sockets.Add(CreateBroadcastSocket(ipaddress));
                            if (ipaddress.Equals(System.Net.IPAddress.Loopback))
                            {
                                containsloopback = true;
                            }
                        }
                        if (!containsloopback)
                        {
                            sockets.Add(CreateBroadcastSocket(System.Net.IPAddress.Loopback));
                        }
                        if (sockets.Count > 0)
                        {
                            // Broadcast client information on each socket
                            bool broadcasted = false;
                            foreach (System.Net.Sockets.Socket socket in sockets)
                            {
                                try
                                {
                                    string clientinformation = Commands.GetServerInformations.ToString("d") + '|' + Version.Major.ToString() + '|' + Version.Minor.ToString() + '|';
                                    socket.SendTo(System.Text.Encoding.Unicode.GetBytes(clientinformation), System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint(GetBroadcastAddress(((System.Net.IPEndPoint)socket.LocalEndPoint).Address), Port));
                                    if (!broadcasted)
                                    {
                                        socket.SendTo(System.Text.Encoding.Unicode.GetBytes(clientinformation), System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint(System.Net.IPAddress.Broadcast, Port));
                                        broadcasted = true;
                                    }
                                }
                                catch (System.Net.Sockets.SocketException ex)
                                {
                                    switch (ex.SocketErrorCode)
                                    {
                                    case System.Net.Sockets.SocketError.HostUnreachable:
                                        // Nothing to do.
                                        break;

                                    default:
                                        throw ex;
                                    }
                                }
                                catch (System.Exception ex)
                                {
                                    throw ex;
                                }
                            }
                            // Receive server informations.
                            int timetowait = WaitForServers;
                            while (timetowait > 0)
                            {
                                System.Threading.Thread.Sleep(250);
                                timetowait -= 250;
                                System.Collections.Generic.List <System.Net.Sockets.Socket> listenersockets = new System.Collections.Generic.List <System.Net.Sockets.Socket>(sockets.Count);
                                listenersockets.AddRange(sockets);
                                System.Net.Sockets.Socket.Select(listenersockets, null, null, 1000);
                                if (listenersockets.Count > 0)
                                {
                                    foreach (System.Net.Sockets.Socket listenersocket in listenersockets)
                                    {
                                        while (listenersocket.Available > 0)
                                        {
                                            try
                                            {
                                                byte[] buffer = new byte[listenersocket.Available];
                                                System.Net.EndPoint server = (System.Net.EndPoint) new System.Net.IPEndPoint(System.Net.IPAddress.None, 0);
                                                if (listenersocket.ReceiveFrom(buffer, ref server) > 0)
                                                {
                                                    string[] serverinformations = System.Text.Encoding.Unicode.GetString(buffer).Split('|');
                                                    if (serverinformations.Length >= 3)
                                                    {
                                                        bool found = false;
                                                        for (int i = 0; i < si.Count && !found; i++)
                                                        {
                                                            found = (si[i].Information == serverinformations[0]);
                                                        }
                                                        if (!found)
                                                        {
                                                            si.Add(new ServerInformation(serverinformations[0], new Version(byte.Parse(serverinformations[1]), byte.Parse(serverinformations[2])), server));
                                                        }
                                                    }
                                                }
                                            }
                                            catch (System.Net.Sockets.SocketException ex)
                                            {
                                                switch (ex.SocketErrorCode)
                                                {
                                                case System.Net.Sockets.SocketError.ConnectionReset:
                                                    // Nothing to do.
                                                    break;

                                                default:
                                                    throw ex;
                                                }
                                            }
                                            catch (System.Exception ex)
                                            {
                                                throw ex;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (sockets.Count > 0)
                        {
                            foreach (System.Net.Sockets.Socket socket in sockets)
                            {
                                if (socket.Connected)
                                {
                                    socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                                    socket.Disconnect(false);
                                }
                                socket.Close();
                            }
                        }
                    }
                }
                if (si.Count == 0)
                {
                    throw new System.Net.Sockets.SocketException((int)System.Net.Sockets.SocketError.HostUnreachable);
                }
                return(si);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }