private uint SendSyncPacket(IDSocket socket, SmartPacket packet, int timeout) { uint errCode = (uint)ErrCode.Success; m_eventSyncPacket.Reset(); socket.SendPacket(packet); if (!m_eventSyncPacket.WaitOne(timeout)) { errCode = (uint)ErrCode.NetworkTimeout; } return errCode; }
private void ConnectThread() { uint errCode = (uint)ErrCode.Success; int loopCnt = 3; Log(string.Format("[VAAL] ConnectThread() : START")); lock (m_lockSocket) { CloseSocket(); m_buffer = new byte[0]; bool isFirst = true; while (true) { try { string ip; int port; m_eventConnect.Reset(); TcpClient client = new TcpClient(); client.NoDelay = true; if (isFirst) { isFirst = false; ip = m_proxyServer.LastConnectedServerIp; port = m_proxyServer.LastConnectedServerPort; } else { ip = m_proxyServer.ServerIp; port = m_proxyServer.ServerPort; m_proxyServer.SetLastConnectedServerInfo(ip, port); } Log(string.Format("[VAAL] ConnectThread({0}, {1}) : Try connecting...", ip, port)); //client.Connect(m_serverIp, m_serverPort); client.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), client); NwiWaitHandle[] waitHandles = new NwiWaitHandle[2]; waitHandles[0] = m_eventThreadStop; waitHandles[1] = m_eventConnect; int index = NwiWaitHandle.WaitAny(waitHandles, 3000); if (index == 0) { Log("[VAAL] ConnectThread() : m_eventThreadStop signaled"); break; } if (client.Connected) { Log(string.Format("[VAAL] ConnectThread() : Connected")); m_socket = new IDSocket(client); m_socket.RecvPacketHandler = new RecvPacketHandler(OnRecvPacket); m_socket.ErrHandler = new ErrorHandler(OnErrorHandler); m_socket.LogHandler = new LogHandler(OnLogHandler); m_eventSocketError.Reset(); StartReadPacketThread(); Thread.Sleep(100); HelloPacket packet = new HelloPacket(); packet.m_ver1 = (ushort)Version.Ver1; packet.m_ver2 = (ushort)Version.Ver2; packet.m_clientProductVer = m_proxyServer.ProductVer; packet.m_configVer = (m_proxyServer.ServerConfig == null) ? (byte)0 : m_proxyServer.ServerConfig.Version; errCode = SendSyncPacket(m_socket, packet, m_proxyServer.SocketTimeout); if (errCode == (uint)ErrCode.Success) { if (m_packetCode == PacketCode.Hello) { errCode = m_resultCode; } else { errCode = (uint)ErrCode.WrongPacketCode; } } packet.Dispose(); break; } else { Log(string.Format("[VAAL] ConnectThread() : Not connected")); } } catch (Exception e) { Log(string.Format("[VAAL] ConnectThread() : Exception={0}", e.Message)); errCode = (uint)ErrCode.InternalException; } if (--loopCnt <= 0) { errCode = (uint)ErrCode.NetworkCantConnect; break; } else { Thread.Sleep(2000); } } if (errCode == (uint)ErrCode.Success && m_socket != null) { if (m_retryLoginPacket != null && (m_socketState == SocketState.Login || m_socketState == SocketState.Relogin)) { Log(string.Format("[VAAL] ConnectThread() : Send Relogin Packet")); m_retryLoginPacket.m_recvTransNum = m_recvTransNum; m_retryLoginPacket.m_isAlreadyLogined = (byte)((m_socketState == SocketState.Login) ? 0 : 1); m_isLoginPacketSync = true; errCode = SendSyncPacket(m_socket, m_retryLoginPacket, m_proxyServer.SocketTimeout); } else { m_socketState = SocketState.Ready; m_eventSendPacket.Set(); } } if (errCode != (uint)ErrCode.Success) { m_socketState = SocketState.Disconnected; } } if (errCode != (uint)ErrCode.Success) { // 락 m_lockSocket 을 소유하고 아래 함수를 호출하면 안된다. (deadlock 상황 발생할 수 있음) CallNotifyHandler(NotifyEventCode.DisconnectFromServer, errCode, null, null); } Log(string.Format("[VAAL] ConnectThread() : END (EC={0}, SS={1})", errCode, m_socketState)); }
private void CloseSocket() { lock (m_lockSocket) { if (m_socket != null) { m_socket.ErrHandler = null; m_socket.Close(); m_socket = null; } } }