Beispiel #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);
        }
Beispiel #2
0
 private bool SocketConnected(NetSocket socket)
 {
     clientSocketID = socket.GetID();
     clientSocket   = socket;
     SendReqUpgradeProtocol(socket, "", "GamePackProtocol");
     return(true);
 }
Beispiel #3
0
 int SocketError(NetSocket socket)
 {
     if (useSingleSendDataTaskProcesser)
     {
         sendTaskProcesser.PostTask(PasueSendTask, null, this, TaskMsg.TMSG_PAUSE);
         socket.dePacketor.UnPack(SocketEvent.EV_SOCKET_OFFLINE, socket);
         socketErrorWaitSem.WaitOne();
         socketErrorWaitSem.Reset();
         RemoveSocket(socket.GetID());
         sendTaskProcesser.Continue();
     }
     else
     {
         socket.dePacketor.UnPack(SocketEvent.EV_SOCKET_OFFLINE, socket);
         RemoveSocket(socket.GetID());
     }
     return(0);
 }
Beispiel #4
0
        public Packet(NetSocket _socketCtx, int _maxBufSize = 1024)
        {
            socketCtx  = _socketCtx;
            serverTask = socketCtx.serverTask;
            socketID   = socketCtx.GetID();
            maxBufSize = _maxBufSize;

            if (_maxBufSize != 0)
            {
                buf = new byte[maxBufSize];
                Array.Clear(buf, 0, buf.Length);
            }

            packBuf.buf = buf;
            packBuf.len = maxBufSize;
        }
Beispiel #5
0
        void RecvedTask(object data)
        {
            Packet    packet    = (Packet)data;
            NetSocket socketCtx = packet.socketCtx;
            Server    server    = GetServer();

            if (CheckingPacketVaild(packet) == 0)
            {
                return;
            }

            SocketError socketError = System.Net.Sockets.SocketError.Success;

            packet.transferedBytes = socketCtx.sock.EndReceive(packet.ar, out socketError);

            if (socketError != System.Net.Sockets.SocketError.Success)
            {
                SocketError(socketCtx);
                return;
            }

            socketCtx.UpdataTimeStamp();

            if (socketCtx.dataTransMode == DataTransMode.MODE_PACK)
            {
                socketCtx.dePacketor.SetCurtPack(socketCtx, packet.buf, packet.transferedBytes);
                int ret = socketCtx.dePacketor.Extract();

                if (ret == 2)
                {
                    socketCtx.SetSocketState(SocketState.RECV_DATA_TOO_BIG);
                    SocketError(socketCtx.GetID());
                }
            }
            else
            {
                socketCtx.SetPack(packet.buf, 0, packet.transferedBytes);
                socketCtx.dePacketor.UnPack(SocketEvent.EV_SOCKET_RECV, socketCtx);
                socketCtx.RemovePack();
            }

            // 然后开始投递下一个WSARecv请求
            server.IocpPostRecv(packet);
        }
Beispiel #6
0
        void AcceptedClientTask(object data)
        {
            Packet    packet    = (Packet)data;
            NetSocket socketCtx = packet.socketCtx;
            Server    server    = GetServer();

            // 把这个有效的客户端信息,加入到socketMap中去
            socketMap[socketCtx.GetID()] = socketCtx;

            socketCtx.SetPack(packet.buf, 0, packet.transferedBytes);
            socketCtx.dePacketor.UnPack(SocketEvent.EV_SOCKET_ACCEPTED, socketCtx);
            socketCtx.RemovePack();

            int bufSize = socketCtx.dePacketor.GetMaxBufferSize();

            if (packet.packBuf.len != bufSize)
            {
                packet = socketCtx.CreatePacket(bufSize);
            }

            server.IocpPostRecv(packet);
        }
Beispiel #7
0
 public int PostSocketErrorTask(NetSocket socketCtx, int delay = 0)
 {
     return(PostTask(SocketErrorTask, socketCtx.GetID(), delay));
 }