Exemple #1
0
        public bool ConnectServer(ServerInfo serverInfo)
        {
            Server server = GetServer();

            Socket    sock             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            NetSocket connectSocketCtx = new NetSocket(this);

            connectSocketCtx.sock = sock;

            connectSocketCtx.SetSocketType(NetSocketType.CONNECT_SERVER_SOCKET);
            serverInfo.socketCtx = connectSocketCtx;
            connectSocketCtx.SetRemoteServerInfo(serverInfo);
            connectSocketCtx.dataTransMode      = serverInfo.dataTransMode;
            socketMap[connectSocketCtx.GetID()] = connectSocketCtx;


            if (connectSocketCtx.sock == null)
            {
                connectSocketCtx.SetSocketState(SocketState.INIT_FAILD);
                SocketError(connectSocketCtx);
                //LOG4CPLUS_ERROR(log.GetInst(), "初始化Socket失败,错误代码:" << _GetLastError());
                return(false);
            }

            connectSocketCtx.UpdataTimeStamp();
            Packet packet = connectSocketCtx.CreatePacket(0);

            // 开始连接服务器
            connectSocketCtx.SetSocketState(SocketState.CONNECTTING_SERVER);
            server.IocpPostConnect(packet);

            return(true);
        }