Esempio n. 1
0
        private void SendDataWait(object obj)
        {
            // Create a ticket.
            int ticket = this._ticket++;

            // Cast the argument to an array of bytes.
            byte[] array = (byte[])obj;

            // Take note of when this method was called.
            int start = Environment.TickCount;

            // Continue to loop while we're waiting to send data, or we're not first in line.
            while (this._sendFlag != SocketSendFlag.WaitCanSend || this._servicing != ticket)
            {
                ;
            }

            // Starting an asynchronous operation might cause an error. Encase it
            // in a try/catch.
            try {
                // Flag the socket as sending data, and begin to send.
                this._sendFlag = SocketSendFlag.Sending;
                this._client.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            } catch (SocketException e) {
                System.Environment.Exit(0);
            }
        }
Esempio n. 2
0
        private void ConnectCallback(IAsyncResult ar)
        {
            // Ending an asynchronous operation might cause an error. Encase it
            // in a try/catch.
            try {
                this._socket.EndConnect(ar);
            } catch (SocketException e) {
                // Figure what caused the error.
                switch (e.SocketErrorCode)
                {
                // We could not establish a connection. Destroy the client.
                case SocketError.ConnectionRefused:
                    Environment.Exit(1);
                    break;

                // An unknown error occured. Throw an exception.
                default:
                    throw new Exception("ConnectCallback: Unknown SocketException '" + e.SocketErrorCode + "'");
                }
            }

            // Initialize the arrays that will store data received from the server, and
            // flag the socket so it can send data.
            this._inBuffer = new byte[this._socket.ReceiveBufferSize];
            this._unBuffer = new byte[0];
            this._sendFlag = SocketSendFlag.CanSend;

            // Begin to receive data from the server.
            this._socket.BeginReceive(this._inBuffer, 0, this._inBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }
Esempio n. 3
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try {
                _client.EndConnect(ar);
            } catch (SocketException e) {
                Retry();
                return;
            }

            this._sendFlag = SocketSendFlag.CanSend;
        }
Esempio n. 4
0
        public Client(Socket connection, int index)
        {
            // Store the socket connection for later use.
            this._socket = connection;

            // Initialize the arrays that will store data received from the client, and
            // flag the socket so it can send data.
            this._inBuffer = new byte[_socket.ReceiveBufferSize];
            this._unBuffer = new byte[0];
            this._sendFlag = SocketSendFlag.CanSend;
            this.Connected = true;

            // Write a message to the console that we accepted a new connection.
            Server.Write("Accepted a connection at " + this.GetAddress());

            // Begin to receive data from the client.
            this._socket.BeginReceive(this._inBuffer, 0, this._inBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), index);
        }
Esempio n. 5
0
        public void SendData(byte[] array)
        {
            // Make sure we're connected.
            if (this._socket?.Connected != true)
            {
                return;
            }

            // Make the packet TCP ready.
            array = new DataBuffer(array).ToPaddedArray();

            // Make sure the data we're sending isn't over the limit.
            if (array.Length > this._socket.SendBufferSize)
            {
                Program.Write("A packet being sent was over the allowed array size: length=" + array.Length);
                return;
            }


            // Can we send data right now?
            if (this._sendFlag != SocketSendFlag.CanSend)
            {
                // Create and start a new thread that will wait to send the data.
                var thread = new Thread(new ParameterizedThreadStart(SendDataWait));
                thread.Start(array);
                return;
            }

            // Starting an asynchronous operation might cause an error. Encase it
            // in a try/catch.
            try {
                // Flag the socket as sending data, and begin sending data.
                this._sendFlag = SocketSendFlag.Sending;
                this._socket.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            } catch (SocketException e) {
                // Figure out what caused the error.
                switch (e.SocketErrorCode)
                {
                // An unknown error occured. Throw an exception.
                default:
                    throw new Exception("SendData: Unknown SocketException '" + e.SocketErrorCode + "'");
                }
            }
        }
Esempio n. 6
0
        public void SendData(byte[] array)
        {
            // Make sure that the socket is connected.
            if (this._socket?.Connected != true)
            {
                Server.Write("Tried to send data to disconnected client at " + this.GetAddress());
                return;
            }

            // Make sure the data we're sending isn't over the limit.
            if (array.Length > this._socket.SendBufferSize)
            {
                Server.Write("Tried to send data bigger than the buffer size at " + array.Length + " bytes at client " + this.GetAddress());
                return;
            }

            // Can we send data right now?
            if (this._sendFlag != SocketSendFlag.CanSend)
            {
                // Create and start a new thread that will wait to send the data.
                object packetObject = array;
                var    thread       = new Thread(new ParameterizedThreadStart(SendDataWait));
                thread.Start(packetObject);
                return;
            }

            // Starting an asynchronous operation might cause an error. Encase it
            // in a try/catch.
            try {
                // Flag the socket as sending data, and begin sending data.
                this._sendFlag = SocketSendFlag.Sending;
                this._socket.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            } catch (SocketException e) {
                // Figure out what caused the error.
                switch (e.SocketErrorCode)
                {
                // An unknown error occured. Throw an exception.
                default:
                    throw new Exception("SendData: Unknown SocketException '" + e.SocketErrorCode + "'");
                }
            }
        }
Esempio n. 7
0
        private void SendDataWait(object obj)
        {
            // Create a ticket.
            int ticket = this._ticket++;

            // Cast the argument to an array of bytes.
            byte[] array = (byte[])obj;

            // Take note of when this method was called.
            int start = Environment.TickCount;

            // Continue to loop while we're waiting to send data, or we're not first in line.
            while (this._sendFlag != SocketSendFlag.WaitCanSend || this._servicing != ticket)
            {
                ;
            }

            // Starting an asynchronous operation might cause an error. Encase it
            // in a try/catch.
            try {
                // Flag the socket as sending data, and begin to send.
                this._sendFlag = SocketSendFlag.Sending;
                this._socket.BeginSend(array, 0, array.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
            } catch (SocketException e) {
                // Figure out what caused the error.
                switch (e.SocketErrorCode)
                {
                // The connection to the server was abruptly severed.
                // Flag the client to be closed and do not continue to send.
                case SocketError.ConnectionAborted:
                    Program.ShowMessage("Connection with the server has been lost. Please save any data locally, and re-connect to sync.");
                    this.Destroy();
                    return;

                // An unknown error occured. Throw an exception.
                default:
                    throw new Exception("SendDataWait: Unknown SocketException '" + e.SocketErrorCode + "'");
                }
            }
        }
Esempio n. 8
0
        private void SendCallback(IAsyncResult ar)
        {
            // End the asynchronous operation.
            this._client.EndSend(ar);

            // If the ticket is equal to the current ticket being served, then
            // we're either not using the waiting system or we've fully caught up.
            if (this._ticket == this._servicing)
            {
                // Reset the ticketing system, and flag the socket so
                // it can begin to send data.
                this._ticket    = 0;
                this._servicing = 0;
                this._sendFlag  = SocketSendFlag.CanSend;
            }
            else
            {
                // Otherwise, increment the service number and
                // flag the socket to service another wait packet.
                this._sendFlag = SocketSendFlag.WaitCanSend;
                this._servicing++;
            }
        }