Example #1
0
        // int loopZero = 3;
        private void recvLoop()
        {
            int       id   = _threadId;
            UdpClient conn = _clients[id];

            Console.WriteLine("server ready...");


            Console.WriteLine("accept client!");
            //_sh.setStream(_client.GetStream(),_client.Client);
            BeginAClient();
            int errCount = 0;

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


                int size = 0;
                try
                {
                    size       = conn.Client.Receive(_tempRecvBuffer, SocketFlags.None);
                    _totalSize = size;
                    _recvPos   = RecvPositions.AfterRecved;
                }
                catch {
                    break;
                }

                if (size <= 0)
                {
                    errCount++;
                    if (errCount > 3)
                    {
                        if (conn.Client == null || conn.Client.Blocking == false || conn.Client.Connected == false)
                        {
                            //루프에서 나간다.
                            break;
                        }
                        else
                        {
                            try
                            {
                                conn.Client.Close();
                            }
                            catch { }
                            conn.Client = null;
                            break;
                        }
                    }
                }
                else
                {
                    errCount = 0;
                }
                TransferEventArgs args = new TransferEventArgs(_id, _tempRecvBuffer, TransferEventArgs.TransferMode.Receive, size, size, "");
                if (E_OnCanReceive != null)
                {
                    E_OnCanReceive(this, args);
                }

                if (args.IsHandled == false && E_OnReceivedInQueue != null)
                {
                    Byte[] buff = new byte[size];
                    Buffer.BlockCopy(_tempRecvBuffer, 0, buff, 0, size);
                    _queue.enqueueFrom(buff);
                    _recvPos = RecvPositions.AfterQueueEntered;
                    args     = new TransferEventArgs(_id, _tempRecvBuffer, TransferEventArgs.TransferMode.Receive, size, _queue.Size, "");

                    if (E_OnReceivedInQueue != null)
                    {
                        E_OnReceivedInQueue(this, args);
                    }
                }

                /*
                 * if (available > 0)
                 * {
                 *  //buff = new Byte[available];
                 *
                 * }
                 * 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..");
        }
Example #2
0
        /// <summary>
        /// 루프내부구문이다.
        /// </summary>
        /// <returns>true를 리턴하면 루프에서 빠져나가는 것이고, false를 리턴하면 continue하는 것이며, null을 리턴하면 그냥 진행한다.</returns>
        protected bool?InRecvLoop(Byte[] buffer, Socket socket, int id, ref int totalSize, ref int errorCount, BufferQueue queue)
        {
            if (_callBackFunc != null)
            {
                return(_callBackFunc(buffer, socket, id, this));
            }
            int size = 0;

            try
            {
                if (_recvMode == ReceiveModes.Header)
                {
                    size = socket.Receive(buffer, 0, _headerSize, SocketFlags.None);
                }
                else if (_recvMode == ReceiveModes.Data)
                {
                    size = socket.Receive(buffer, 0, _dataSizeToGet, SocketFlags.None);
                }
                else //all
                {
                    size = socket.Receive(buffer, totalSize, 1, SocketFlags.None);//일단 1개를 받음..
                    if (socket.Available > 0)
                    {
                        size += socket.Receive(buffer, totalSize + 1, socket.Available, SocketFlags.None);//나머지를 받음.
                    }
                }

                _recvPos = RecvPositions.AfterRecved;
                #region when size <= 0
                if (size <= 0)
                {
                    errorCount++; //접속된 직후 이 경우가 있을 수 있으므로..
                    if (errorCount > 3)
                    {
                        if (socket == null || socket.Blocking == false || socket.Connected == false)
                        {
                            //루프에서 나간다.
                            return(true);
                        }
                        else
                        {
                            try
                            {
                                socket.Close();
                            }
                            catch { }
                            RunConnectionEvent(ConnType.Disconnected, "error count==3", id);
                            socket = null;
                        }
                        errorCount = 0;
                        return(true);
                    }
                    return(false);
                }
                else
                {
                    errorCount = 0;
                }
                #endregion
            }
            catch
            {
                if (E_NetError != null)
                {
                    E_NetError(this, new NetworkErrorEventArgs(id, NetworkErrorEventArgs.NetErrorMsg.FAIL_WHEN_RECV_MSG));
                }
                return(true);
            }

            TransferEventArgs args;


            if (_recvMode == ReceiveModes.Header || _recvMode == ReceiveModes.Data)
            {
                args             = new TransferEventArgs(id, buffer, TransferEventArgs.TransferMode.Receive, size, size, "");
                args.ReceiveMode = _recvMode;
            }
            else
            //if (_recvMode == ReceiveModes.All)
            {
                totalSize += size;

                args             = new TransferEventArgs(id, buffer, TransferEventArgs.TransferMode.Receive, size, totalSize, "");
                args.ReceiveMode = _recvMode;
            }
            if (E_OnCanReceive != null)
            {
                E_OnCanReceive(this, args);
            }


            if (_recvMode == ReceiveModes.All)
            {
                _recvMode      = args.ReceiveMode;
                _dataSizeToGet = args.DataSizeToGet;

                if (args.IsHandled)
                {
                    if (args.UsedSize > 0 &&
                        totalSize > args.UsedSize)
                    //직접처리했는데 버퍼에 있는 내용을 다 소비하지 않았다면..
                    {
                        //남은 부분을 앞으로 가져옴..
                        Buffer.BlockCopy(buffer, args.UsedSize, buffer, 0, totalSize - args.UsedSize);
                        totalSize -= args.UsedSize;
                    }
                    else//이 경우 모든 받은 내용을 처리했다고 가정한다.
                    {
                        totalSize = 0;//버퍼를 초기화한다. 제일 처음부터 받게 된다.
                    }
                }
                else if (E_OnReceivedInQueue != null) //직접처리하지 않았으므로, queue에 넣어줌..
                {
                    queue.enqueueFrom(buffer, 0, totalSize);
                    _recvPos  = RecvPositions.AfterQueueEntered;
                    totalSize = 0;
                    args      = new TransferEventArgs(id, buffer, TransferEventArgs.TransferMode.Receive, size, queue.Size, "");

                    if (E_OnReceivedInQueue != null)
                    {
                        E_OnReceivedInQueue(this, args);
                    }
                }
            }
            else if (_recvMode == ReceiveModes.Header)
            {
                _recvMode      = args.ReceiveMode;
                _dataSizeToGet = args.DataSizeToGet;
            }
            else if (_recvMode == ReceiveModes.Data)
            {
                _recvMode      = args.ReceiveMode;
                _dataSizeToGet = args.DataSizeToGet;
            }

            return(null);
        }
Example #3
0
        // int loopZero = 3;
        private void recvLoop()
        {
            Console.WriteLine("server ready...");


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

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


                int size = 0;
                try
                {
                    size       = _uSocket.ReceiveFrom(_tempRecvBuffer, ref _remoteEP);// 0, available, SocketFlags.None); ;
                    _totalSize = size;
                }
                catch
                {
                    break;
                }
                _recvPos = RecvPositions.AfterRecved;
                if (size <= 0)
                {
                    errCount++;
                    if (errCount > 1)
                    {
                        if (_uSocket == null || _uSocket.Blocking == false || _uSocket.Connected == false)
                        {
                            //루프에서 나간다.
                            break;
                        }
                        else
                        {
                            try
                            {
                                _uSocket.Close();
                            }
                            catch { }
                            _uSocket = null;
                            break;
                        }
                    }
                }
                else
                {
                    errCount = 0;
                }
                TransferEventArgs args = new TransferEventArgs(_id, _tempRecvBuffer, TransferEventArgs.TransferMode.Receive, size, _totalSize, "");

                if (E_OnCanReceive != null)
                {
                    E_OnCanReceive(this, args);
                }

                if (args.IsHandled == false && E_OnReceivedInQueue != null)
                {
                    Byte[] buff = new byte[size];
                    Buffer.BlockCopy(_tempRecvBuffer, 0, buff, 0, size);
                    _queue.enqueueFrom(buff);
                    _recvPos = RecvPositions.AfterQueueEntered;

                    args = new TransferEventArgs(_id, _tempRecvBuffer, TransferEventArgs.TransferMode.Receive, size, _queue.Size, "");

                    if (E_OnReceivedInQueue != null)
                    {
                        E_OnReceivedInQueue(this, args);
                    }
                }

                /*
                 * if (_callBackFunc != null) _callBackFunc();
                 *
                 * int available = _uSocket.Available;
                 * Byte[] buff;
                 * int size;
                 * 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);
                 *  totalCount++;
                 * }
                 * if (size > 0)
                 * {
                 *  if (size == buff.Length) _queue.Enqueue(buff);
                 *  else _queue.enqueueFrom(buff, 0, size);
                 *  totalSize += (ulong)size;
                 *  if (_queue.Size != (int)totalSize)
                 *  {
                 *      Console.Write("total size is not match to queueSize");
                 *  }
                 *  if (E_OnReceived != null) E_OnReceived(this, new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, _queue.Size));
                 *
                 *  loopZero = 3;
                 *  totalDataCount++;
                 *
                 *
                 * }
                 * 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..");
        }