예제 #1
0
        public int SendTo(ref PGN.Packet pck)
        {
            lock (m_oLock)
            {
                byte[] s    = pck.Buf;
                int    l    = pck.Len;
                int    nSnd = 0;

                ++m_nSqc;
                pck.Sqc = m_nSqc;


                ++m_sndN;
                m_sndN %= m_sndB.Count;


                byte[] buf = m_sndB[m_sndN];
                nSnd = PGN.Util.EnCrypt(ref buf, ref nSnd, s, l);


                m_arSnd.SetBuffer(m_sndB[m_sndN], 0, nSnd);
                m_sndC = 1;

                m_scH.SendToAsync(m_arSnd);
            }

            return(NTC.OK);
        }
예제 #2
0
        public int Send(PGN.Packet pck)
        {
            if (0 > this.IsConnected || NTC.OK != m_sndC)
            {
                return(NTC.EFAIL);
            }

            lock (m_oLock)
            {
                int    snd = 0;
                byte[] buf = null;

                ++m_nSqc;                                                       // increase packet sequence
                ++m_sndN;                                                       // increase buffer index
                m_sndN %= m_sndB.Count;

                buf = m_sndB[m_sndN];
                snd = pck.EnCrypt(ref buf);

                m_sndC = NTC.WAIT;
                m_scH.BeginSend(buf, 0, snd, SocketFlags.None, IoSend, this);
            }

            return(NTC.OK);
        }
예제 #3
0
        public int Send(ref PGN.Packet pck)
        {
            if (0 > this.IsConnected || 0 < m_sndC)
            {
                return(NTC.EFAIL);
            }

            lock (m_oLock)
            {
                byte[] s    = pck.Buf;
                int    l    = pck.Len;
                int    nSnd = 0;

                ++m_nSqc;


                ++m_sndN;
                m_sndN %= m_sndB.Count;


                byte[] buf = m_sndB[m_sndN];
                nSnd = PGN.Util.EnCrypt(ref buf, ref nSnd, s, l);


                m_arSnd.SetBuffer(m_sndB[m_sndN], 0, nSnd);
                m_sndC = 1;

                m_scH.SendAsync(m_arSnd);
            }

            return(NTC.OK);
        }
예제 #4
0
        public int SendTo(ref PGN.Packet pck)
        {
            lock (m_oLock)
            {
                byte[] s    = pck.Buf;
                int    l    = pck.Len + NTC.PCK_HEAD;
                int    nSnd = 0;

                ++m_nSqc;
                ++m_sndN;
                m_sndN %= m_sndB.Count;


                byte[] buf = m_sndB[m_sndN];
                nSnd = PGN.Packet.EnCrypt(ref buf, ref nSnd, s, l);

                m_sndC = pck.Opp;
                m_scH.SendTo(m_sndB[m_sndN], m_sdR);
            }

            return(NTC.OK);
        }
예제 #5
0
        ////////////////////////////////////////////////////////////////////////////
        // Inner Process...

        protected void IoComplete(object sender, SocketAsyncEventArgs args)
        {
            try
            {
                SocketError          err = args.SocketError;
                byte[]               rcb = args.Buffer;                                                         // buffer
                int                  rcn = args.BytesTransferred;                                               // buffer length
                SocketAsyncOperation opc = args.LastOperation;

                if (SocketAsyncOperation.ReceiveFrom == opc)
                {
                    // close message from server
                    if (err != SocketError.Success || 0 == rcn)
                    {
                        Console.WriteLine("IoComplete::Socket Error");
                        CloseSocket();
                        return;
                    }

                    lock (m_oLock)
                    {
                        // DeCryption
                        int lenD = 0;
                        PGN.Util.DeCrypt(ref m_rcvB, ref lenD, rcb, rcn);

                        // DeCoding
                        PGN.Packet pck = new PGN.Packet(ref m_rcvB, lenD);

                        // data buffer and length
                        byte[] b = pck.DataBuf;
                        int    l = pck.DataLen;


                        string s_chat = Encoding.Default
                                        .GetString(b, 0, l).Trim();

                        Console.WriteLine(s_chat);
                    }

                    m_scH.ReceiveFromAsync(m_arRcv);
                    return;
                }

                else if (SocketAsyncOperation.SendTo == opc)
                {
                    if (err != SocketError.Success)
                    {
                        Console.WriteLine("IoComplete::Cann't Send to Server");
                        CloseSocket();
                        return;
                    }

                    lock (m_oLock)
                    {
                        m_sndC = 0;
                    }

                    Console.WriteLine("IoComplete::Send compelte");
                    return;
                }
            }
            catch (SocketException)
            {
                Console.WriteLine("IoComplete::SocketException");
            }
            catch (Exception)
            {
                Console.WriteLine("IoComplete::Exception");
            }
        }
예제 #6
0
        ////////////////////////////////////////////////////////////////////////////
        // Inner Process...

        protected void IoComplete(object sender, SocketAsyncEventArgs args)
        {
            try
            {
                SocketError          err = args.SocketError;
                byte[]               rcb = args.Buffer;                                                         // buffer
                int                  rcn = args.BytesTransferred;                                               // buffer length
                SocketAsyncOperation opc = args.LastOperation;

                if (SocketAsyncOperation.Receive == opc)
                {
                    // close message from server
                    if (err != SocketError.Success || 0 == rcn)
                    {
                        Console.WriteLine("IoComplete::Disconnected");
                        CloseSocket();
                        return;
                    }

                    lock (m_oLock)
                    {
                        // DeCryption
                        int lenD = 0;
                        PGN.Util.DeCrypt(ref m_rcvB, ref lenD, rcb, rcn);

                        // DeCoding
                        PGN.Packet pck = new PGN.Packet(ref m_rcvB, lenD);

                        // data buffer and length
                        byte[] b = pck.DataBuf;
                        int    l = pck.DataLen;


                        // read the key and id from string
                        if (0 > m_aId)
                        {
                            int n   = 0;
                            int cnt = 0;

                            n   = Array.FindIndex(b, 0, _s => _s == (byte)' ');
                            cnt = l - (n + 1);

                            string s_uid = Encoding.Default
                                           .GetString(b, 0, n)
                                           .Trim();

                            m_aId = System.Convert.ToInt32(s_uid);


                            string s_key = null;
                            Array.Copy(b, n + 1, m_vCrp, 0, cnt);
                            s_key = Encoding.Default.GetString(m_vCrp, 0, cnt).Trim();

                            Console.WriteLine("Auth Uid::{0} , Key:{1}", m_aId, s_key);
                        }

                        // the transformed stream exists
                        else
                        {
                            string s_chat = Encoding.Default
                                            .GetString(b, 0, l).Trim();

                            Console.WriteLine(s_chat);

                            int op = PGN.NTC.OP_CHAT;
                            this.Send("server send::" + s_chat, op);
                        }
                    }

                    m_scH.ReceiveAsync(m_arRcv);

                    return;
                }

                else if (SocketAsyncOperation.Connect == opc)
                {
                    if (err != SocketError.Success)
                    {
                        Console.WriteLine("IoComplete::Cann't Connect to Server");
                        CloseSocket();
                    }
                    else
                    {
                        Console.WriteLine("IoComplete::Connection Success");

                        m_scH.ReceiveAsync(m_arRcv);                            // enable the receive event
                    }

                    m_arCon = null;                                                             // release the connection event instance
                    return;
                }

                else if (SocketAsyncOperation.Send == opc)
                {
                    if (err != SocketError.Success)
                    {
                        Console.WriteLine("IoComplete::Cann't Send to Server");
                        CloseSocket();
                        return;
                    }

                    lock (m_oLock)
                    {
                        m_sndC = 0;
                    }

                    Console.WriteLine("IoComplete::Send complete");
                    return;
                }
            }
            catch (SocketException)
            {
                Console.WriteLine("IoComplete::SocketException");
            }
            catch (Exception)
            {
                Console.WriteLine("IoComplete::Exception");
            }
        }