Esempio n. 1
0
            public void Proc()
            {
                for (var it = _Peers.Begin(); it;)
                {
                    var itCheck = it;
                    it.MoveNext();

                    var NetRet = itCheck.Data.Proc();
                    if (NetRet != ENetRet.Ok)
                    {
                        _Close(itCheck, NetRet);
                        continue;
                    }

                    try
                    {
                        if (!itCheck.Data.Poll())
                        {
                            continue;
                        }

                        while (true)
                        {
                            var RecvState = itCheck.Data.RecvedBegin();
                            if (RecvState == ERecvState.NoData)
                            {
                                break;
                            }
                            else if (RecvState == ERecvState.UserData)
                            {
                                if (!itCheck.Data.DoesWillClose())
                                {
                                    var Key = itCheck.Data.Key;
                                    try
                                    {
                                        _RecvFunc(Key, itCheck.Data.StreamRcv);
                                    }
                                    catch (Exception Exception_)
                                    {
                                        throw new ExceptionNet(ENetRet.ExceptionExternal, Exception_);
                                    }
                                    if (!_Peers.Get((Int32)Key.PeerNum))
                                    {
                                        break;
                                    }
                                }
                            }

                            itCheck.Data.RecvedEnd();
                        }
                    }
                    catch (ExceptionNet Exception_)
                    {
                        if (Exception_.GetRet() == ENetRet.ExceptionExternal && Exception_.InnerException != null)
                        {
                            throw Exception_.InnerException;
                        }
                        else
                        {
                            _Close(itCheck, Exception_.GetRet());
                        }
                    }
                    catch
                    {
                        _Close(itCheck, ENetRet.SocketError);
                    }
                }
            }
Esempio n. 2
0
            public new void Proc()
            {
                base.Proc();

                for (var it = _Connectings.Begin(); it != _Connectings.End();)
                {
                    var itCheck = it;
                    it.MoveNext();

                    var itPeer   = _PeersAndConnectings.Get(itCheck.Index);
                    var PeerNum  = itPeer.Data.PeerNum;
                    var NamePort = itCheck.Data;
                    _Connectings.Remove(itCheck);

                    try
                    {
                        Socket Sock = null;

                        AddressFamily[] AddressFamilies = { AddressFamily.InterNetwork, AddressFamily.InterNetworkV6 };

                        foreach (var i in NamePort.GetIPAddresses())
                        {
                            if (i.AddressFamily != AddressFamily.InterNetwork)
                            {
                                continue;
                            }

                            foreach (var a in AddressFamilies)
                            {
                                Sock = new Socket(a, SocketType.Stream, ProtocolType.Tcp)
                                {
                                    NoDelay           = _NoDelay,
                                    SendBufferSize    = _SendBuffSize,
                                    ReceiveBufferSize = _RecvBuffSize
                                };

                                var RecvEvent = new SocketAsyncEventArgs();
                                RecvEvent.Completed     += new EventHandler <SocketAsyncEventArgs>(_Worker);
                                RecvEvent.UserToken      = new _SConnectingInfo(itPeer.Data, Sock);
                                RecvEvent.RemoteEndPoint = new IPEndPoint(i, NamePort.Port);

                                bool ConnRet = false;

                                try
                                {
                                    ConnRet = Sock.ConnectAsync(RecvEvent);
                                }
                                catch
                                {
                                    Sock = null;
                                    continue;
                                }

                                if (!ConnRet)
                                {
                                    if (!RecvEvent.ConnectSocket.Connected) // Android(iOS는 미확인)에서 Wifi, Data 모두 끈 상태에서 여기에 들어오므로 여기서 연결여부 체크
                                    {
                                        Sock = null;
                                        continue;
                                    }

                                    _Link(itPeer.Data, Sock, RecvEvent);
                                }
                                else
                                {
                                    _ConnectTimeOut.Add((Int32)PeerNum, Sock);
                                }

                                break; // Success
                            }

                            if (Sock != null)
                            {
                                break; // Success
                            }
                        }

                        if (Sock == null)
                        {
                            throw new Exception("Can not new Socket");
                        }
                    }
                    catch (ExceptionNet Exception_)
                    {
                        if (Exception_.GetRet() == ENetRet.ExceptionExternal && Exception_.InnerException != null)
                        {
                            throw Exception_.InnerException;
                        }
                        else
                        {
                            _PeersAndConnectings.Remove(itPeer);
                            _LinkFail(PeerNum, Exception_.GetRet());
                            continue;
                        }
                    }
                    catch
                    {
                        _PeersAndConnectings.Remove(itPeer);
                        _LinkFail(PeerNum, ENetRet.SocketError);
                        continue;
                    }
                }

                for (var LFConnect = _Connects.GetPopBuf();
                     LFConnect != null;
                     LFConnect = _Connects.GetPopBuf())
                {
                    var ConnectInfo = (_SConnectingInfo)LFConnect.Event.UserToken;
                    var itPeer      = _PeersAndConnectings.Get((Int32)ConnectInfo.Key.PeerNum);
                    if (itPeer && itPeer.Data.Equals(ConnectInfo.Key))
                    {
                        if (LFConnect.NetRet == ENetRet.Ok)
                        {
                            try
                            {
                                _Link(ConnectInfo.Key, ConnectInfo.socket, LFConnect.Event);
                            }
                            catch (ExceptionNet Exception_)
                            {
                                if (Exception_.GetRet() == ENetRet.ExceptionExternal && Exception_.InnerException != null)
                                {
                                    throw Exception_.InnerException;
                                }
                                else
                                {
                                    LFConnect.NetRet = Exception_.GetRet();
                                }
                            }
                            catch
                            {
                                LFConnect.NetRet = ENetRet.SystemError;
                            }
                        }

                        if (LFConnect.NetRet != ENetRet.Ok)
                        {
                            _ConnectTimeOut.Remove(itPeer.Index);
                            _PeersAndConnectings.Remove(itPeer);
                            _LinkFail(ConnectInfo.Key.PeerNum, LFConnect.NetRet);
                        }
                    }

                    _Connects.Pop();
                }

                if (_ProcPeriod.CheckAndNextLoose())
                {
                    _ConnectTimeOut.Proc();
                }
            }