How?- receive packets -nuconnector&.Net

08/25/2011 20:50 Little Hole#1
what is the best way to receive data from nuconnector on .Net ??

byte [], List<byte[]> ,List<ArraySegment<byte>>;


or what ??

and what is the format that Nuconnector us to send data to the bot !!!
?

Thank You
08/25/2011 21:30 s2k#2
MemoryStream + BinaryReader

NuConnector Packet Structure:
DataLength Int16, Opcode Int16, SendType Int16, DATA as Byte()
08/25/2011 22:45 sarkoplata#3
Quote:
Originally Posted by s2k View Post
MemoryStream + BinaryReader

NuConnector Packet Structure:
DataLength Int16, Opcode Int16, SendType Int16, DATA as Byte()
Basicly
Code:
Dim ms as new MemoryStream(data() as byte from nuconnector)
Dim bn as new binaryreader(ms)
bn.read(w/e)
08/25/2011 22:54 DaxterSoul#4
Could look like this
Code:
public class ProxyConnection
    {

        private static Socket m_ProxySocket;
        private static byte[] m_buffer;

        private static bool m_isClosing;

        public static bool Connect(ushort ProxyPort)
        {
            if (m_ProxySocket == null)
            {
                m_ProxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            if (m_ProxySocket.Connected)
            {
                m_isClosing = true;
                m_ProxySocket.Shutdown(SocketShutdown.Both);
                m_ProxySocket.Close();
                m_ProxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            if (m_buffer == null)
            {
                m_buffer = new byte[8192];
            }
            try
            {
                m_isClosing = false;
                m_ProxySocket.Connect(IPAddress.Loopback, ProxyPort);
                m_ProxySocket.BeginReceive(m_buffer, 0, 8192, SocketFlags.None, new AsyncCallback(WaitForData), m_ProxySocket);

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        private static void WaitForData(IAsyncResult ar)
        {
            if (m_ProxySocket != null)
            {
                if (m_isClosing == false)
                {
                    try
                    {
                        Socket worker = (Socket)ar.AsyncState;
                        if (worker.EndReceive(ar) > 0)
                        {
                            var packet = new phPacket(m_buffer);
                            lock (m_packetQueue)
                            {
                               //Queue or process the packet.
                            }
                            worker.BeginReceive(m_buffer, 0, 8192, SocketFlags.None, new AsyncCallback(WaitForData), worker);
                        }
                        else //Connection Ended
                        {
                            m_isClosing = true;
                            if (m_ProxySocket.Connected)
                            {
                                m_ProxySocket.Shutdown(SocketShutdown.Both);
                                m_ProxySocket.Close();
                            }
                            m_ProxySocket = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                }
            }
        }        

    }