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); }
void ConnectedServerTask(object data) { Packet packet = (Packet)data; NetSocket socketCtx = packet.socketCtx; Server server = GetServer(); if (CheckingPacketVaild(packet) == 0) { return; } if (socketCtx.sock.Connected == false) { SocketError(socketCtx); return; } socketCtx.sock.EndConnect(packet.ar); socketCtx.SetSocketState(SocketState.CONNECTED_SERVER); socketCtx.UpdataTimeStamp(); int bufSize = socketCtx.dePacketor.GetMaxBufferSize(); if (packet.packBuf.len != bufSize) { packet = socketCtx.CreatePacket(bufSize); } socketCtx.dePacketor.UnPack(SocketEvent.EV_SOCKET_CONNECTED, socketCtx); server.IocpPostRecv(packet); }
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); }
void OnAcceptCallBack(IAsyncResult ar) { Packet packet = ar.AsyncState as Packet; NetSocket newSocketCtx = packet.socketCtx; //接收结果 newSocketCtx.sock = listenSocket.sock.EndAccept(ar); newSocketCtx.SetSocketState(SocketState.CONNECTED_CLIENT); newSocketCtx.UpdataTimeStamp(); Packet newPacket = newSocketCtx.CreatePacket(dePacketor.GetMaxBufferSize()); serverTaskMgr.PostServerMessage(ServerMessage.SMSG_ACCEPT_CLIENT, newPacket); //继续接受客户端的请求 serverTaskMgr.PostSingleIocpAccpetTask(packet); }