Example #1
0
        private void recvLoop()
        {
            int id = _threadId;//계속 가지고있음..

            Console.WriteLine("server ready...");
            TcpClient conn = _clients[id];

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

            BeginAClient();
            //          try
            //          {
            //_isRecvThreadRunning = true;
            _totalSize = 0;
            int errorCount = 0;

            while (id == _threadId)  //현재 thread가 최근 thread가 아니면 나감..
            {
                int size = 0;
                try
                {
                    if (id != _threadId)
                    {
                        return;                                                                       //
                    }
                    size     = conn.Client.Receive(_tempRecvBuffer, _totalSize, 1, SocketFlags.None); //일단 1개를 받음..
                    _recvPos = RecvPositions.AfterRecved;
                    if (conn.Available > 0)
                    {
                        size += conn.Client.Receive(_tempRecvBuffer, _totalSize + 1, conn.Available, SocketFlags.None);    //나머지를 받음.
                    }
                    if (size <= 0)
                    {
                        errorCount++;     //접속된 직후 이 경우가 있을 수 있으므로..
                        if (errorCount > 1)
                        {
                            if (conn.Client == null || conn.Client.Blocking == false || conn.Client.Connected == false)
                            {
                                //루프에서 나간다.
                                break;
                            }
                            else
                            {
                                try
                                {
                                    conn.Close();
                                }
                                catch { }
                                conn = null;
                                break;
                            }
                        }
                    }
                    else
                    {
                        errorCount = 0;
                    }
                }
                catch
                {
                    if (E_NetError != null && id == _threadId)
                    {
                        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 = _tempRecvBuffer;
                if (E_OnCanReceive != null && id == _threadId)
                {
                    E_OnCanReceive(this, args);
                }

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

                    if (E_OnReceivedInQueue != null && id == _threadId)
                    {
                        E_OnReceivedInQueue(this, args);
                    }
                }
                #region old

                /*
                 *
                 * int available = _server.Available;
                 *
                 * int size = 0;
                 * try
                 * {
                 *  size = _server.Client.Receive(_tempRecvBuffer, 0, available, SocketFlags.None);
                 *  //size = _uSocket.ReceiveFrom(_tempRecvBuffer, ref _remoteEP);// 0, available, SocketFlags.None); ;
                 * }
                 * catch
                 * {
                 *  break;
                 * }
                 *
                 * if (size <= 0)
                 * {
                 *  if (_server.Client == null || _server.Client.Blocking == false || _server.Client.Connected == false)
                 *  {
                 *      //루프에서 나간다.
                 *      break;
                 *  }
                 * }
                 * TransferEventArgs args = new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, size, "");
                 * if (E_OnReceived != null) E_OnReceived(this, args);
                 *
                 * if (args.IsHandled == false)
                 * {
                 *  Byte[] buff = new byte[size];
                 *  Buffer.BlockCopy(_tempRecvBuffer, 0, buff, 0, size);
                 *  _queue.enqueueFrom(buff);
                 *  args = new TransferEventArgs(_id, TransferEventArgs.TransferMode.Receive, size, _queue.Size, "");
                 *
                 *  if (E_OnQueueEntered != null) E_OnQueueEntered(this, args);
                 * }
                 */
                /*
                 * if (available > 0)
                 * {
                 *  buff = new Byte[available];
                 *  size = _server.Client.Receive(buff, 0, available, SocketFlags.None);
                 *  Win32APIs.SendMsgData("recved:" + size, "LogWindow");
                 * }
                 * 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;
                 */
                #endregion
            }

//            }
//            catch (Exception e) {
//                throw;
//            }
            _clients[id] = null;
            // _isRecvThreadRunning = false;

            //_isEndConnection = false;
            FinishAClient();
            if (E_NetError != null && id == _threadId)
            {
                E_NetError(this, new NetworkErrorEventArgs(_id, NetworkErrorEventArgs.NetErrorMsg.RECV_THREAD_FINISHED));
            }
            //ConnectionEvent(ConnType.Disconnected);
            //close();
            //OnConnectionFailed("Connection closed..");
        }
Example #2
0
        private void recvLoop()
        {
            int id = _currentId;

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


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

            //_isRecvThreadRunning = true;
            _totalSize[id] = 0;
            int errorCount = 0;

            try
            {
                while (!_isEndConnection[id])
                {
                    funcRunningInServerLoopForClient();
                    if (_callBackFunc != null)
                    {
                        _callBackFunc();
                    }
                    int size = 0;
                    try
                    {
                        size = _client[id].Client.Receive(_tempRecvBuffer[id], _totalSize[id], 1, SocketFlags.None);//일단 1개를 받음..
                        if (_client[id].Available > 0)
                        {
                            size += _client[id].Client.Receive(_tempRecvBuffer[id], _totalSize[id] + 1, _client[id].Available, SocketFlags.None);//나머지를 받음.
                        }
                        _recvPos = RecvPositions.AfterRecved;
                        if (size <= 0)
                        {
                            errorCount++; //접속된 직후 이 경우가 있을 수 있으므로..
                            if (errorCount > 3)
                            {
                                if (_client[id].Client == null || _client[id].Client.Blocking == false || _client[id].Client.Connected == false)
                                {
                                    //루프에서 나간다.
                                    break;
                                }
                                else
                                {
                                    try
                                    {
                                        _client[id].Close();
                                    }
                                    catch { }
                                    ConnectionEvent(ConnType.Disconnected, id);
                                    _client[id] = null;
                                }
                            }
                            continue;
                        }
                        else
                        {
                            errorCount = 0;
                        }
                    }
                    catch
                    {
                        if (E_NetError != null)
                        {
                            E_NetError(this, new NetworkErrorEventArgs(id, NetworkErrorEventArgs.NetErrorMsg.FAIL_WHEN_RECV_MSG));
                        }
                        break;
                    }

                    _totalSize[id] += size;
                    TransferEventArgs args = new TransferEventArgs(id, TransferEventArgs.TransferMode.Receive, size, _totalSize[id], "");
                    args.TempBuffer = _tempRecvBuffer[id];
                    if (E_OnCanReceive != null)
                    {
                        E_OnCanReceive(this, args);
                    }

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

                        if (E_OnReceivedInQueue != null)
                        {
                            E_OnReceivedInQueue(this, args);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                try
                {
                    _client[id].Close();
                }
                catch { }
            }
            // _isRecvThreadRunning = false;

            //_isEndConnection = false;
            _client[id] = null;
            FinishAClient();
            if (E_NetError != null)
            {
                E_NetError(this, new NetworkErrorEventArgs(id, NetworkErrorEventArgs.NetErrorMsg.RECV_THREAD_FINISHED));
            }
            //ConnectionEvent(ConnType.Disconnected);
            //close();
            //OnConnectionFailed("Connection closed..");
            ConnectionEvent(ConnType.Disconnected, id);
            //close();
            //OnConnectionFailed("Connection closed..");
        }
Example #3
0
        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, TransferEventArgs.TransferMode.Receive, size, size, "");
                args.TempBuffer = _tempRecvBuffer;
                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, 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..");
        }
Example #4
0
        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, 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, 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..");
        }