Example #1
0
        public void MessagePartSent(IAsyncResult ar)
        {
            // get the async state object which was returned by the async beginsend method
            SocketGlobals.AsyncSendState mState = (SocketGlobals.AsyncSendState)ar.AsyncState;

            try
            {
                int numBytesSent = 0;

                // call the EndSend method which will succeed or throw an error depending on if we are still connected
                numBytesSent = mState.Socket.EndSend(ar);

                // increment the total amount of bytes processed so far
                mState.Progress += numBytesSent;

                // determine if we havent' sent all the data for this Packet yet
                if (mState.NextLength() > 0)
                {
                    // we need to send more data
                    mState.Socket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
                }

                // at this point, the EndSend succeeded and we are ready to send something else!
            }
            catch (Exception ex)
            {
                MessageBox.Show("DataSent error: " + ex.Message);
            }
        }
        public void SendMessageToServer(string argCommandString)
        {
            // create a Packet object from the passed data; this packet can be any object type because we use serialization!
            //Dim mPacket As New Dictionary(Of String, String)
            //mPacket.Add("CMD", argCommandString)
            //mPacket.Add("MSG", argMessageString)
            string mPacket = argCommandString;

            // serialize the Packet into a stream of bytes which is suitable for sending with the Socket
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
            mSerializer.Serialize(mSerializerStream, mPacket);

            // get the serialized Packet bytes
            byte[] mPacketBytes = mSerializerStream.GetBuffer();

            // convert the size into a byte array
            byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);

            // create the async state object which we can pass between async methods
            SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(cClientSocket);

            // resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
            // ERROR: Not supported in C#: ReDimStatement
            Array.Resize(ref mState.BytesToSend, mPacketBytes.Length + mSizeBytes.Length);

            // copy the mSizeBytes and mPacketBytes to the BytesToSend array
            System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
            System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);

            cClientSocket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
        }
        ///' <summary>
        ///' QueueMessage prepares a Message object containing our data to send and queues this Message object in the OutboundMessageQueue.
        ///' </summary>
        ///' <param name="argCommandMessage"></param>
        ///' <param name="argCommandData"></param>
        ///' <remarks></remarks>
        //Sub QueueMessage(ByVal argCommandMessage As String, ByVal argCommandData As Object)

        //End Sub

        private void cSendQueue_MessageQueued()
        {
            // when a message is queued, we need to check whether or not we are currently processing the queue before allowing the top item in the queue to start sending
            if (cSendQueue.Processing == false)
            {
                // process the top message in the queue, which in turn will process all other messages until the queue is empty
                SocketGlobals.AsyncSendState mState = (SocketGlobals.AsyncSendState)cSendQueue.Messages.Dequeue();
                // we must send the correct number of bytes, which must not be greater than the remaining bytes
                cClientSocket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
            }
        }
Example #4
0
        /// <summary>
        /// QueueMessage prepares a Message object containing our data to send and queues this Message object in the OutboundMessageQueue.
        /// </summary>
        /// <remarks></remarks>
        public void SendMessageToClient(string argCommandString, Socket argClient)
        {
            // parse the command string
            string argCommand = null;
            string argText    = null;

            argCommand = argCommandString.Substring(0, argCommandString.IndexOf(" "));
            argText    = argCommandString.Remove(0, argCommand.Length);

            //' create a Packet object from the passed data
            //Dim mPacket As New Dictionary(Of String, String)
            //mPacket.Add("CMD", argCommandMessage)
            //mPacket.Add("MSG", argCommandData)

            string mPacket = argCommandString;

            // serialize the Packet into a stream of bytes which is suitable for sending with the Socket
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
            mSerializer.Serialize(mSerializerStream, mPacket);

            // get the serialized Packet bytes
            byte[] mPacketBytes = mSerializerStream.GetBuffer();

            // convert the size into a byte array
            byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);

            // create the async state object which we can pass between async methods
            SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(argClient);

            // resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
            // TODO: ReDim mState.BytesToSend(mPacketBytes.Length + mSizeBytes.Length - 1)

            // copy the mSizeBytes and mPacketBytes to the BytesToSend array
            System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
            System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);

            // queue the Message
            argClient.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
        }
        /// <summary>
        /// Fires right when a client is connected to the server.
        /// </summary>
        /// <param name="ar"></param>
        /// <remarks></remarks>
        public void ConnectToServerCompleted(IAsyncResult ar)
        {
            // get the async state object which was returned by the async beginconnect method
            SocketGlobals.AsyncSendState mState = (SocketGlobals.AsyncSendState)ar.AsyncState;

            // end the async connection request so we can check if we are connected to the server
            try
            {
                // call the EndConnect method which will succeed or throw an error depending on the result of the connection
                mState.Socket.EndConnect(ar);
                // at this point, the EndConnect succeeded and we are connected to the server!
                // send a welcome message
                SendMessageToServer("/say What? My name is...");
                // start waiting for messages from the server
                SocketGlobals.AsyncReceiveState mReceiveState = new SocketGlobals.AsyncReceiveState();
                mReceiveState.Socket = mState.Socket;
                mReceiveState.Socket.BeginReceive(mReceiveState.Buffer, 0, SocketGlobals.gBufferSize, SocketFlags.None, new AsyncCallback(ServerMessageReceived), mReceiveState);
            }
            catch (Exception ex)
            {
                // at this point, the EndConnect failed and we are NOT connected to the server!
                MessageBox.Show("Connect error: " + ex.Message);
            }
        }
Example #6
0
        public void SendMessageToServer(string argCommandString)
        {
            // create a Packet object from the passed data; this packet can be any object type because we use serialization!
            //Dim mPacket As New Dictionary(Of String, String)
            //mPacket.Add("CMD", argCommandString)
            //mPacket.Add("MSG", argMessageString)
            string mPacket = argCommandString;

            // serialize the Packet into a stream of bytes which is suitable for sending with the Socket
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
            mSerializer.Serialize(mSerializerStream, mPacket);

            // get the serialized Packet bytes
            byte[] mPacketBytes = mSerializerStream.GetBuffer();

            // convert the size into a byte array
            byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);

            // create the async state object which we can pass between async methods
            SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(cClientSocket);

            // resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
            // ERROR: Not supported in C#: ReDimStatement
            Array.Resize(ref mState.BytesToSend, mPacketBytes.Length + mSizeBytes.Length);

            // copy the mSizeBytes and mPacketBytes to the BytesToSend array
            System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
            System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);

            cClientSocket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
        }
Example #7
0
        /// <summary>
        /// QueueMessage prepares a Message object containing our data to send and queues this Message object in the OutboundMessageQueue.
        /// </summary>
        /// <remarks></remarks>
        public void SendMessageToClient(string argCommandString, Socket argClient)
        {
            // parse the command string
            string argCommand = null;
            string argText = null;
            argCommand = argCommandString.Substring(0, argCommandString.IndexOf(" "));
            argText = argCommandString.Remove(0, argCommand.Length);

            //' create a Packet object from the passed data
            //Dim mPacket As New Dictionary(Of String, String)
            //mPacket.Add("CMD", argCommandMessage)
            //mPacket.Add("MSG", argCommandData)

            string mPacket = argCommandString;

            // serialize the Packet into a stream of bytes which is suitable for sending with the Socket
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
            mSerializer.Serialize(mSerializerStream, mPacket);

            // get the serialized Packet bytes
            byte[] mPacketBytes = mSerializerStream.GetBuffer();

            // convert the size into a byte array
            byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);

            // create the async state object which we can pass between async methods
            SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(argClient);

            // resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
            // TODO: ReDim mState.BytesToSend(mPacketBytes.Length + mSizeBytes.Length - 1)

            // copy the mSizeBytes and mPacketBytes to the BytesToSend array
            System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
            System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);

            // queue the Message
            argClient.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
        }