Example #1
0
        private void AcceptNewConnection(System.IAsyncResult asyncResult)
        {
            Socket servSock;
            Socket clntSock;

            byte[] recieved;
            int    length;

            GHub.client.Client client;


            try
            {
                servSock = (Socket)asyncResult.AsyncState;
                clntSock = servSock.EndAccept(asyncResult);
                newConnection.Set();
                clntSock.Blocking = true;
                clntSock.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveTimeout, 10000);             //10 seconds.
                clntSock.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendTimeout, 10000);                //10 seconds.
            }
            catch (System.InvalidOperationException)
            {
                servSock = null;
                clntSock = null;
                recieved = null;
                newConnection.Set();
                return;
            }
            catch (System.Exception e)
            {
                string a = e.Message;
                newConnection.Set();
                return;
            }

            client = new Client(clntSock, serverArray, clientArray, this);
            try
            {
                client.SendMessage("$Lock EXTENDEDPROTOCOL::This_hub_was_written_by_Gizmo} Pk=GHub|");
                client.SendMessage("$Supports NoGetINFO NoHello|");

                recieved = new byte[9];
                length   = clntSock.Receive(recieved, 0, recieved.Length, SocketFlags.Peek);
            }
            catch
            {
                //clntSock.Shutdown(SocketShutdown.Both);
                clntSock.Close();

                servSock = null;
                clntSock = null;
                recieved = null;
                return;
            }

            switch (length)
            {
            case 0:
                servSock = null;
                clntSock = null;
                recieved = null;
                return;
            }
            byte[] exactRecieved = new byte[length];
            for (int i = 0; i < length; i++)
            {
                exactRecieved[i] = recieved[i];
            }

            string textData = System.Text.ASCIIEncoding.ASCII.GetString(exactRecieved);

            //clntSock.Blocking = false;
            // if we are dealing with a server
            if (textData.IndexOf("$LogginIn") != -1)
            {
                Server newServer = new Server(clntSock, serverArray, clientArray, myPlugins, this);

                GHub.Settings.Synchronization.serverArray.WaitOne();
                serverArray.Add(newServer);
                GHub.Settings.Synchronization.serverArray.ReleaseMutex();

                newServer.log = new Log(((System.Net.IPEndPoint)client.soc.RemoteEndPoint).Address.ToString());
                newServer.log.Write("<Server to server> $Lock EXTENDEDPROTOCOL::This_hub_was_written_by_Gizmo} Pk=GHub|");
            }
            // if we are dealing with a client
            else
            {
                User newUser = new User(clntSock, serverArray, clientArray, myPlugins, this);
                newUser.ipAddress = ((System.Net.IPEndPoint)newUser.soc.RemoteEndPoint).Address.ToString();

                // if the user if banned we must deni them access to the hub.
                if (GHub.Settings.BANS.tempBans.isBanned(newUser.ipAddress))
                {
                    try
                    {
                        clntSock.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        clntSock.Close();
                    }
                    catch {}
                    finally
                    {
                        servSock      = null;
                        clntSock      = null;
                        recieved      = null;
                        textData      = null;
                        exactRecieved = null;
                    }

                    return;
                }

                GHub.Settings.Synchronization.clientArray.WaitOne();
                clientArray.Add(newUser);
                GHub.Settings.Synchronization.clientArray.ReleaseMutex();
                newUser.log = new Log(((System.Net.IPEndPoint)client.soc.RemoteEndPoint).Address.ToString());
                newUser.log.Write("<Server to client> $Lock EXTENDEDPROTOCOL::This_hub_was_written_by_Gizmo} Pk=GHub|");
            }

            servSock      = null;
            clntSock      = null;
            recieved      = null;
            textData      = null;
            exactRecieved = null;
        }