Exemplo n.º 1
0
        protected virtual void ReceiveCallback(IAsyncResult ar)
        {
            SocketStateObject state   = (SocketStateObject)ar.AsyncState;
            Socket            handler = state.workSocket;

            try
            {
                int byteRead = handler.EndReceive(ar);

                if (byteRead > 0)
                {
                    if (!isPackageSplit)
                    {
                        Buffer.BlockCopy(emptyArr, 0, dataArr, 0, 64 * 1024);
                        //获取数据长度;
                        byte[] datalengtharr = new byte[4];
                        Buffer.BlockCopy(state.buffer, 0, datalengtharr, 0, 4);
                        packagelen = BitConverter.ToInt32(datalengtharr, 0);
                        G6Debugger.LogFormat("receive: byteRead={0} length = {1}", byteRead, packagelen);

                        //获取数据主体;
                        Buffer.BlockCopy(state.buffer, 4, dataArr, 0, byteRead - 4);
                        byteIndex    = byteRead - 4;
                        unreceiveLen = packagelen - (byteRead - 4);
                    }
                    else
                    {
                        int enableLen = Math.Min(byteRead, unreceiveLen);
                        Buffer.BlockCopy(state.buffer, 0, dataArr, byteIndex, enableLen);
                        byteIndex    = byteIndex + enableLen;
                        unreceiveLen = unreceiveLen - enableLen;
                        G6Debugger.LogFormat("receive break: byteRead={0} length={1} byteIndex={2}", byteRead, packagelen, byteIndex);
                    }
                    G6Debugger.LogFormat("ReceiveCallback unreceiveLen:{0}", unreceiveLen);

                    //判断数据长度,是否接收完全;
                    if (unreceiveLen <= 0)
                    {
                        isPackageSplit = false;
                        AddResponse(Encoding.UTF8.GetString(dataArr, 0, packagelen));
                    }
                    else
                    {
                        isPackageSplit = true;
                    }
                }

                handler.BeginReceive(state.buffer, 0, SocketStateObject.BufferSize, 0,
                                     new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            // Get the socket that handles the client request.
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            // Create the state object.
            clientSocketState            = new SocketStateObject();
            clientSocketState.workSocket = handler;
            handler.BeginReceive(clientSocketState.buffer, 0, SocketStateObject.BufferSize, 0,
                                 new AsyncCallback(ReceiveCallback), clientSocketState);

            isConnected = true;
        }
Exemplo n.º 3
0
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket client = (Socket)ar.AsyncState;

                curSocketStateObject            = new SocketStateObject();
                curSocketStateObject.workSocket = client;

                client.BeginReceive(curSocketStateObject.buffer, 0, SocketStateObject.BufferSize, 0,
                                    new AsyncCallback(ReceiveCallback), curSocketStateObject);

                G6Debugger.Log("Hotfix server Connected!!!!!!!!");
            }
            catch (Exception e)
            {
                G6Debugger.LogError(e.ToString());
            }
        }