Esempio n. 1
0
 protected void UploadFile(string path)
 {
     bs              = new ByteSocket(uploadpath, path, "test", "test.jpg", "image/jpeg");
     bs.OnException += bs_OnException;
     bs.OnFinished  += bs_OnFinished;
     bs.OnSend      += bs_OnSend;
     bs.Upload();
 }
Esempio n. 2
0
        public void Init()
        {
            NetManager.sendFunction = SocketManager.instance.SendPackage;

            _socket = new ByteSocket(false);
            EventManager.instance.AddEventListener(_socket, SocketEvent.SOCKET_CONNECT, _SocketConnected);
            EventManager.instance.AddEventListener(_socket, SocketEvent.SOCKET_CLOSE, _SocketClose);
            EventManager.instance.AddEventListener(_socket, SocketEvent.SOCKET_DATA, _SocketData);
            EventManager.instance.AddEventListener(_socket, SocketEvent.SOCKET_ERROR, _SocketError);
        }
Esempio n. 3
0
        public PackageProcessor(ByteSocket socket, byte[] sBuffer, byte[] rBuffer)
        {
            // TODO: Complete member initialization

            m_sendBuffer    = sBuffer;
            m_receiveBuffer = rBuffer;

            Init(socket);

            AsyncReceiveImp();
        }
Esempio n. 4
0
        //protected IPackageReader pkgReader;

        public PackageProcessor(ByteSocket socket)
        {
            // TODO: Complete member initialization

            m_sendBuffer    = new byte[m_socket.SendBufferSize];
            m_receiveBuffer = new byte[m_socket.ReceiveBufferSize];

            Init(socket);

            AsyncReceiveImp();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            ResourceUtil.ExtractResource("LogConfig.xml", "LogConfig.xml", Assembly.GetAssembly(typeof(Program)));

            ByteSocket bs = new ByteSocket();

            bs.ConnectSuccessHandler += new ByteSocket.ConnectSocketDelegate(bs_ConnectSuccessHandler);

            bs.Connect("192.168.16.119", 20000);

            Console.ReadKey();
        }
Esempio n. 6
0
        private static void SendAsyncImp(object state)
        {
            PackageProcessor proc = state as PackageProcessor;
            ByteSocket       s    = proc.m_socket;

            try
            {
                AsyncSendPkgComplete(s.Sockets, proc.m_sendAsyncEvent);
            }
            catch (Exception ex)
            {
                log.Error("Async send package error.", ex);
                s.Disconnect();
            }
        }
Esempio n. 7
0
        protected virtual void Init(ByteSocket socket)
        {
            m_sendingTcp = false;
            m_pkgQueue   = new Queue(256);

            //pkgReader = new PackageReader();

            m_socket = socket;

            m_sendAsyncEvent           = new SocketAsyncEventArgs();
            m_sendAsyncEvent.UserToken = this;
            m_sendAsyncEvent.SetBuffer(m_sendBuffer, 0, 0);
            m_sendAsyncEvent.Completed += new EventHandler <SocketAsyncEventArgs>(AsyncSendPkgComplete);

            m_receiveAsyncEvent            = new SocketAsyncEventArgs();
            m_receiveAsyncEvent.Completed += new EventHandler <SocketAsyncEventArgs>(AsyncReceiveComplete);
        }
Esempio n. 8
0
 static void bs_ConnectSuccessHandler(ByteSocket bSock)
 {
     Console.WriteLine("客户端启动成功...");
     bSock.Sockets.Send(new byte[0]);
 }
Esempio n. 9
0
        private static void AsyncSendPkgComplete(object sender, SocketAsyncEventArgs e)
        {
            PackageProcessor proc = (PackageProcessor)e.UserToken;
            ByteSocket       sock = proc.m_socket;

            try
            {
                Queue q = proc.m_pkgQueue;
                if (q == null || !sock.Sockets.Connected)
                {
                    return;
                }
                int    sent  = e.BytesTransferred;
                byte[] data  = proc.m_sendBuffer;
                int    count = proc.m_sendedLength - sent;

                if (count > 0)
                {
                    Array.Copy(data, sent, data, 0, count);
                }
                else
                {
                    count = 0;
                }

                e.SetBuffer(0, 0);

                int firstOffset = proc.m_firstPkgOffset;

                lock (q.SyncRoot)
                {
                    while (q.Count > 0)
                    {
                        Package pkg = (Package)q.Peek();

                        int len = 0;

                        if (sock.Encryted)
                        {
                            len = pkg.CopyToEncryt(data, count, firstOffset, sock.PackageCrytor);
                        }
                        else
                        {
                            len = pkg.CopyTo(data, count, firstOffset);
                        }

                        firstOffset += len;
                        count       += len;

                        if (pkg.dataLength <= firstOffset)
                        {
                            q.Dequeue();
                            firstOffset = 0;
                            sock.PackageCrytor.EncrytOnceComplete();
                        }

                        if (data.Length == count)
                        {
                            break;
                        }
                    }

                    proc.m_firstPkgOffset = firstOffset;

                    if (count <= 0)
                    {
                        proc.m_sendingTcp = false;
                        return;
                    }
                }

                proc.m_sendedLength = count;
                e.SetBuffer(0, count);
                if (sock.Sockets.SendAsync(e))
                {
                    return;
                }
                AsyncSendPkgComplete(sender, e);
            }
            catch (Exception ex)
            {
                log.Error("Async sending package error.", ex);
                sock.Disconnect();
            }
        }
Esempio n. 10
0
 private void m_socket_ConnectFailedHandler(ByteSocket bSock)
 {
     OnConnected(false);
 }
Esempio n. 11
0
 private void m_socket_ConnectSuccessHandler(ByteSocket bSock)
 {
     OnConnected(true);
 }
Esempio n. 12
0
 public JSNPkgProcessor(ByteSocket socket, byte[] sBuffer, byte[] rBuffer)
     : base(socket, sBuffer, rBuffer)
 {
 }
Esempio n. 13
0
        public Package ReadPackage(ref int srcOffset, ref int pkgLength, int curBufferSize, byte[] srcBuffer, ByteSocket sock)
        {
            short header = 0;

            while (srcOffset + 4 < curBufferSize)
            {
                header = (short)((srcBuffer[srcOffset] << 8) + srcBuffer[srcOffset + 1]);
                if (header == Package.HEADER)
                {
                    pkgLength = (srcBuffer[srcOffset + 2] << 8) + srcBuffer[srcOffset + 3];
                    break;
                }
                else
                {
                    srcOffset++;
                }
            }

            int dataLeft = curBufferSize - srcOffset;

            if ((pkgLength != 0 && pkgLength < Package.HDR_SIZE) || pkgLength > sock.ReceiveBufferSize)
            {
                return(null);
            }

            if (dataLeft >= pkgLength && pkgLength != 0)
            {
                Package pkg = sock.ReceiveUsedPacket;
                pkg.CopyFrom(srcBuffer, srcOffset, 0, pkgLength);
                return(pkg);
            }

            return(null);
        }