public int readMore()
        {
            int size = 0;


            try
            {
                int    available = _uSocket.Available;
                Byte[] buff;
                if (available > 0)
                {
                    buff = new Byte[available];
                    size = _uSocket.ReceiveFrom(buff, 0, available, SocketFlags.None, ref _remoteEP);
                }
                else
                {
                    buff = _tempBuff; //available이 0일때는 다음에 도착할 header를 기다린다.
                    _uSocket.Blocking = true;
                    size = _uSocket.ReceiveFrom(buff, ref _remoteEP);
                }
                if (size > 0)
                {
                    if (size == buff.Length)
                    {
                        _queue.Enqueue(buff);
                    }
                    else
                    {
                        _queue.enqueueFrom(buff, 0, size);
                    }
                }
                return(size);
            }
            catch (Exception e)
            {
                Console.WriteLine("socket exception" + e.ToString());
                return(-1);
            }
        }
Beispiel #2
0
        private void recvLoop()
        {
            Console.WriteLine("server ready...");


            _isEndConnection = false;
            Console.WriteLine("accept client!");
            //_sh.setStream(_client.GetStream(),_client.Client);
            BeginAClient();

            while (!_isEndConnection)
            {
                funcRunningInServerLoopForClient();
                if (_callBackFunc != null)
                {
                    _callBackFunc();
                }

                int    available = base.Available;
                Byte[] buff;
                int    size;
                if (available > 0)
                {
                    buff = new Byte[available];
                    size = this.Client.Receive(buff, 0, available, SocketFlags.None);
                }
                else
                {
                    buff = new byte[_headerSize]; //available이 0일때는 다음에 도착할 header를 기다린다.
                    size = this.Client.Receive(buff, 0, _headerSize, SocketFlags.None);
                }
                if (size > 0)
                {
                    if (size == buff.Length)
                    {
                        _queue.Enqueue(buff);
                    }
                    else
                    {
                        _queue.enqueueFrom(buff, 0, size);
                    }

                    if (E_OnReceived != null)
                    {
                        E_OnReceived(this, new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, _queue.Size));
                    }
                    loopZero = 3;
                }
                else
                {
                    loopZero--;
                }

                if (loopZero == 0 || size < 0)
                {
                    if (E_NetError != null)
                    {
                        E_NetError(this, new NetworkErrorEventArgs(_id, NetworkErrorEventArgs.NetErrorMsg.READ_TIMEOUT));
                    }
                    break;
                }
                if (size == 0)
                {
                    continue;
                }
            }

            FinishAClient();
            ConnectionEvent(ConnType.Disconnected);
            //close();
            //OnConnectionFailed("Connection closed..");
        }
        private void recvLoop()
        {
            Console.WriteLine("server ready...");


            _isEndConnection = false;
            Console.WriteLine("accept client!");
            //_sh.setStream(_client.GetStream(),_client.Client);
            BeginAClient();
            try
            {
                _isRecvThreadRunning = true;
                while (!_isEndConnection)
                {
                    int    available = _server.Available;
                    Byte[] buff;
                    int    size;
                    if (available > 0)
                    {
                        buff = new Byte[available];
                        size = _server.Client.Receive(buff, 0, available, SocketFlags.None);
                    }
                    else
                    {
                        buff = new byte[_headerSize]; //available이 0일때는 다음에 도착할 header를 기다린다.
                        size = _server.Client.Receive(buff, 0, _headerSize, SocketFlags.None);
                    }
                    if (_isDisposing)
                    {
                        _isEndConnection = true;
                        return; //thread를 끝낸다. connection이나 neterror 이벤트를 발생시키지 않는다.
                    }
                    if (size > 0)
                    {
                        if (size == buff.Length)
                        {
                            _queue.Enqueue(buff);
                        }
                        else
                        {
                            _queue.enqueueFrom(buff, 0, size);
                        }

                        if (E_OnReceived != null)
                        {
                            E_OnReceived(this, new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, _queue.Size));
                        }
                        loopZero = 3;
                    }
                    else
                    {
                        loopZero--;
                    }

                    if (loopZero == 0 || size < 0)
                    {
                        if (E_NetError != null)
                        {
                            E_NetError(this, new NetworkErrorEventArgs(_id, NetworkErrorEventArgs.NetErrorMsg.READ_TIMEOUT));
                        }
                        break;
                    }
                    if (size == 0)
                    {
                        continue;
                    }
                }
            }
            catch {
            }
            _isRecvThreadRunning = false;

            _isEndConnection = false;
            FinishAClient();
            if (E_NetError != null)
            {
                E_NetError(this, new NetworkErrorEventArgs(_id, NetworkErrorEventArgs.NetErrorMsg.RECV_THREAD_FINISHED));
            }
            //ConnectionEvent(ConnType.Disconnected);
            //close();
            //OnConnectionFailed("Connection closed..");
        }