0 Replies - 2119 Views - Last Post: 23 September 2014 - 01:22 AM Rate Topic: -----

#1 Ald0s  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 14

Terrible C# Tcp Receive Code

Posted 23 September 2014 - 01:22 AM

I have to admit I wrote this... Albeit a very long time ago. I'd say a year and a half, the code is actually meant to receive data from the Tcp stream, deserialize it and then output it. Nowadays, I do this same thing in 15 lines and under. Well, laugh it up... Please don't hold me to this junk!

Also, it didn't even totally work.. Yeah.

private void InitReceive()
        {
            try
            {
                if (firstTimeExecution)
                {
                    // time to allow the client to start sending its messages.
                    this.SendACKPacket_Event();
                    firstTimeExecution = false;
                    // set this event to never fire again in the lifetime of this object. not that itd do much
                    // if it did, just meh.
                }

                // before calling our proper async method, we will make use of a simple Read() call to get the size of the
                // incoming message
                byte[] sz = new byte[4];
                int i = networkStream.Read(sz, 0, sz.Length);
                if (i == 4)
                {
                    // 4 bytes were read from the get-length, indicating that we have the size.
                    int BUFFSZ = BitConverter.ToInt32(sz, 0); DebugServices.OUT("SERVER_CLIENT: Received size integer: " + BUFFSZ.ToString());
                    MAIN_BUFFER = new byte[BUFFSZ];
                    networkStream.BeginRead(MAIN_BUFFER, 0, MAIN_BUFFER.Length, new AsyncCallback(ReceiveCallBack), BUFFSZ);
                   receiveDone.WaitOne();
                }
            }
            catch (Exception e) { Misc.ErrorHandling.OUTPUT_ERROR(e); }
        }

        private void ReceiveCallBack(IAsyncResult ar)
        {
            try
            {
                receiveDone.Set();
                int i1 = (int)ar.AsyncState;
                int i = networkStream.EndRead(ar);
               // receiveDone.Set();
                DebugServices.OUT("SERVER_CLIENT: Received MAIN_BUFFER read: " + i.ToString());

                if (!getMoreBytes)
                {
                    Array.Clear(tempBuffer, 0, tempBuffer.Length); tempBufferLength = 0;

                    if ((i > 0) && (i == i1))
                    {
                        // SUCCESS! We just read a full message - deserialize it and pass it off
                        getMoreBytes = false; // sets it to false.. >.>

                        RetreiveObjectAndContinueExecution(MAIN_BUFFER, i);
                    }
                    else if ((i > 0) && (i < i1))
                    {
                        // we havent read enough bytes yet, so lets store what we have so far and read some more
                        int DIFFERENCE = i1 - i; // find difference
                        MAIN_BUFFER.CopyTo(tempBuffer, 0); Array.Clear(MAIN_BUFFER, 0, MAIN_BUFFER.Length); // copy what we have so far and then clear the MAIN_BUFFER
                        tempBufferLength = i1;

                        getMoreBytes = true; // this is how we will let this method know we are just topping up our buffer
                        networkStream.BeginRead(MAIN_BUFFER, 0, MAIN_BUFFER.Length, new AsyncCallback(ReceiveCallBack), DIFFERENCE);
                    }
                }
                else
                {
                    // moar bytes pls
                    while(getMoreBytes)
                    {
                        if (i == i1)
                        { tempBufferLength += i; MAIN_BUFFER.CopyTo(tempBuffer, tempBuffer.Length);
                        RetreiveObjectAndContinueExecution(tempBuffer, tempBufferLength);
                        }
                        else if(i < i1) // nope. still not enough.
                        {
                            MAIN_BUFFER.CopyTo(tempBuffer, tempBuffer.Length); Array.Clear(MAIN_BUFFER, 0, MAIN_BUFFER.Length);
                            int DIFFERENCE = i1 - i;

                            getMoreBytes = true;
                            networkStream.BeginRead(MAIN_BUFFER, 0, MAIN_BUFFER.Length, new AsyncCallback(ReceiveCallBack), DIFFERENCE);
                        }
                        else if(i > i1) // this should NEVER EVER happen. (i hope)
                        {
                            // i am so convinced this will never happen, im just going to write a debug entry about it if it does, then
                            // ignore it and continue normal execution.

                            DebugServices.OUT("SERVER_CLIENT: Last message's read size was BIGGER than its assigned size... what now?");
                            // clear everything
                            Array.Clear(MAIN_BUFFER, 0, MAIN_BUFFER.Length); Array.Clear(tempBuffer, 0, tempBuffer.Length);
                            tempBufferLength = 0;
                            getMoreBytes = false;
                        }
                    }
                }
                
                InitReceive(); // re call out entry method.
            }
            catch (Exception e) { Misc.ErrorHandling.OUTPUT_ERROR(e); }
        }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1