Exemplo n.º 1
0
        /// <summary>
        /// 当接受到消息时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnMessage(object sender, EventArgs args)
        {
            if (packer == null)
            {
                Trigger(SocketRequestEvents.ON_MESSAGE, args);
                return;
            }

            try
            {
                byte[][] data = packer.Decode((args as SocketResponseEventArgs).Response);
                if (data != null)
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        try
                        {
                            if (render != null && render.Length > 0)
                            {
                                DecodeRenderBuffer.Byte = data[i];
                                for (int n = 0; n < render.Length; n++)
                                {
                                    render[n].Decode(DecodeRenderBuffer);
                                }
                                data[i] = DecodeRenderBuffer.Byte;
                            }

                            if (protocol == null)
                            {
                                args = new SocketResponseEventArgs(data[i]);
                            }
                            else
                            {
                                args = new PackageResponseEventArgs(protocol.Decode(data[i]));
                            }
                            Trigger(SocketRequestEvents.ON_MESSAGE, args);
                        }
                        catch (Exception ex)
                        {
                            Trigger(SocketRequestEvents.ON_ERROR, new ErrorEventArgs(ex));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trigger(SocketRequestEvents.ON_ERROR, new ErrorEventArgs(ex));
                Disconnect();
            }
        }
Exemplo n.º 2
0
        protected void OnReadCallBack(IAsyncResult result)
        {
            try
            {
                byte[] receiveBytes = socket.EndReceive(result, ref listenEndPoint);

                var args = new SocketResponseEventArgs(receiveBytes);
                OnMessage(this, args);

                socket.BeginReceive(OnReadCallBack, null);
            }catch (Exception ex)
            {
                OnError(this, new ErrorEventArgs(ex));
                Dispose();
            }
        }
Exemplo n.º 3
0
        protected void OnReadCallBack(IAsyncResult result)
        {
            var read = networkStream.EndRead(result);

            if (read <= 0)
            {
                status = Status.Closed;
                return;
            }

            var callbackBuff = new byte[read];

            System.Buffer.BlockCopy(readBuffer, 0, callbackBuff, 0, read);
            var args = new SocketResponseEventArgs(callbackBuff);

            OnMessage(this, args);

            readBuffer = new byte[readBufferLength];
            networkStream.BeginRead(readBuffer, 0, readBuffer.Length, OnReadCallBack, readBuffer);
        }
Exemplo n.º 4
0
        protected void OnReadCallBack(IAsyncResult result)
        {
            try
            {
                byte[] receiveBytes = socket.EndReceive(result, ref listenEndPoint);

                var args = new SocketResponseEventArgs(receiveBytes);
                OnMessage(this, args);

                socket.BeginReceive(OnReadCallBack, null);

            }catch(Exception ex)
            {
                OnError(this, new ErrorEventArgs(ex));
                Dispose();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 当接受到消息时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnMessage(object sender , EventArgs args)
        {
            if(packer == null)
            {
                Trigger(SocketRequestEvents.ON_MESSAGE, args);
                return;
            }

            try
            {
                byte[][] data = packer.Decode((args as SocketResponseEventArgs).Response);
                if (data != null)
                {
                    for (int i = 0; i < data.Length; i++)
                    {
                        try
                        {

                            if (render != null && render.Length > 0)
                            {
                                DecodeRenderBuffer.Byte = data[i];
                                for (int n = 0; n < render.Length; n++)
                                {
                                    render[n].Decode(DecodeRenderBuffer);
                                }
                                data[i] = DecodeRenderBuffer.Byte;
                            }

                            if (protocol == null)
                            {
                                args = new SocketResponseEventArgs(data[i]);
                            }
                            else
                            {
                                args = new PackageResponseEventArgs(protocol.Decode(data[i]));
                            }
                            Trigger(SocketRequestEvents.ON_MESSAGE, args);

                        }
                        catch (Exception ex)
                        {
                            Trigger(SocketRequestEvents.ON_ERROR, new ErrorEventArgs(ex));
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Trigger(SocketRequestEvents.ON_ERROR, new ErrorEventArgs(ex));
                Disconnect();
            }
        }
Exemplo n.º 6
0
        protected void OnReadCallBack(IAsyncResult result)
        {
            int read = networkStream.EndRead(result);
            if (read <= 0)
            {
                status = Status.CLOSED;
                return;
            }

            var callbackBuff = new byte[read];
            System.Buffer.BlockCopy(readBuffer, 0, callbackBuff, 0, read);
            var args = new SocketResponseEventArgs(callbackBuff);
            OnMessage(this, args);

            readBuffer = new byte[readBufferLength];
            networkStream.BeginRead(readBuffer, 0, readBuffer.Length, OnReadCallBack, readBuffer);
        }