Esempio n. 1
0
        public void readMore()
        {
            int unit = 0, size = 0;


            try
            {
                if (_client == null || _client.Client == null)
                {
                    return;
                }
                else
                {
                    unit = _client.Client.Available;
                }
            }
            catch
            {
                unit = 1;
            }
            size = _client.Client.Receive(_tempBuff, 0, unit, SocketFlags.None);

            if (size > 0)
            {
                _queue.enqueueFrom(_tempBuff, 0, size);
            }
        }
Esempio n. 2
0
        public void readMore()
        {
            int unit = 0, size = 0;


            try
            {
                if (_client == null || _client.Client == null)
                {
                    return;
                }
                else
                {
                    unit = _client.Client.Available;
                }
            }
            catch
            {
                unit = 1;
            }
            if (unit <= 0)
            {
                return;
            }

            size        = _client.Client.Receive(_tempBuff, _totalSize, unit, SocketFlags.None);
            _totalSize += size;

            if (size > 0 && _isRecvQueueUsing)
            {
                _queue.enqueueFrom(_tempBuff, 0, size);
            }
        }
Esempio n. 3
0
        public void readMore()
        {
            int size = 0;


            try
            {
                size = _uSocket.ReceiveFrom(_tempBuff, ref _remoteEP);
                if (size > 0)
                {
                    _queue.enqueueFrom(_tempBuff, 0, size);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("socket exception" + e.ToString());
                return;
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
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..");
        }
Esempio n. 6
0
        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..");
        }
Esempio n. 7
0
        private void recvLoop()
        {
            Console.WriteLine("server ready...");


            _isEndConnection = false;
            Console.WriteLine("accept client!");
            //_sh.setStream(_client.GetStream(),_client.Client);
            BeginAClient();
            try
            {
                _isRecvThreadRunning = true;
                int totalSize = 0;
                while (!_isEndConnection)
                {
                    int size = 0;
                    try
                    {
                        size = _server.Client.Receive(_tempRecvBuff, totalSize, 1, SocketFlags.None);//일단 1개를 받음..
                        if (_server.Available > 0)
                        {
                            size += _server.Client.Receive(_tempRecvBuff, totalSize + 1, _server.Available, SocketFlags.None);//나머지를 받음.
                        }
                    }
                    catch
                    {
                        if (E_NetError != null)
                        {
                            E_NetError(this, new NetworkErrorEventArgs(_id, NetworkErrorEventArgs.NetErrorMsg.FAIL_WHEN_RECV_MSG));
                        }
                        break;
                    }
                    totalSize += size;
                    TransferEventArgs args = new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, totalSize, "");
                    args.TempBuffer = _tempRecvBuff;
                    if (E_OnCanReceive != null)
                    {
                        E_OnCanReceive(this, args);
                    }

                    if (args.IsHandled)
                    {
                        if (args.UsedSize > 0 && totalSize > args.UsedSize)//직접처리했는데 버퍼에 있는 내용을 다 소비하지 않았다면..
                        {
                            //남은 부분을 앞으로 가져옴..
                            Buffer.BlockCopy(_tempRecvBuff, args.UsedSize, _tempRecvBuff, 0, totalSize - args.UsedSize);
                            totalSize -= args.UsedSize;
                        }
                    }
                    else //직접처리하지 않았으므로, queue에 넣어줌..
                    {
                        _queue.enqueueFrom(_tempRecvBuff, 0, totalSize);
                        totalSize = 0;
                    }

                    /*
                     * 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..");
        }