/// <summary> /// Default constructor /// </summary> /// <param name="packetType">packet type</param> /// <param name="packet">packet</param> /// <param name="offset">offset</param> /// <param name="size">size of packet in byte</param> /// <param name="iocpTcpClient">client</param> /// <param name="dataPacket">data packet for send</param> public PacketTransporter(PacketType packetType, Packet packet, int offset, int size, IocpTcpClient iocpTcpClient, Packet dataPacket = null) { m_packetType = packetType; m_packet = packet; m_offset = offset; m_size = size; m_iocpTcpClient = iocpTcpClient; m_dataPacket = dataPacket; }
/// <summary> /// Connection callback function /// </summary> /// <param name="result">result</param> private static void onConnected(IAsyncResult result) { IocpTcpClient tcpclient = result.AsyncState as IocpTcpClient; try { tcpclient.m_client.Client.EndConnect(result); } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); tcpclient.m_timeOutEvent.SetEvent(); return; } tcpclient.m_timeOutEvent.SetEvent(); return; }
/// <summary> /// Connection callback function /// </summary> /// <param name="result">result</param> private static void onConnected(IAsyncResult result) { IocpTcpClient tcpclient = result.AsyncState as IocpTcpClient; try { tcpclient.m_client.EndConnect(result); } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); tcpclient.m_timeOutEvent.SetEvent(); return; } tcpclient.m_timeOutEvent.SetEvent(); //if (tcpclient.m_callBackObj != null) // tcpclient.m_callBackObj.OnConnected(tcpclient, ConnectStatus.SUCCESS); return; }
/// <summary> /// Default copy constructor /// </summary> /// <param name="b">the object to copy from</param> public IocpTcpClient(IocpTcpClient b) : base(b) { m_clientOps = b.m_clientOps; }
/// <summary> /// Default constructor /// </summary> /// <param name="packetType">packet type</param> /// <param name="packet">packet</param> /// <param name="offset">offset</param> /// <param name="size">size of packet in byte</param> /// <param name="iocpTcpClient">client</param> /// <param name="dataPacket">data packet for send</param> public PacketTransporter(PacketType packetType,Packet packet, int offset, int size, IocpTcpClient iocpTcpClient,Packet dataPacket=null) { m_packetType = packetType; m_packet = packet; m_offset = offset; m_size = size; m_iocpTcpClient = iocpTcpClient; m_dataPacket = dataPacket; }
/// <summary> /// Default copy constructor /// </summary> /// <param name="b">the object to copy from</param> public IocpTcpClient(IocpTcpClient b) : base(b) { m_clientOps = b.m_clientOps; }
/// <summary> /// Make the connection to the server and start receiving /// </summary> protected override void execute() { ConnectStatus status = ConnectStatus.SUCCESS; try { lock (m_generalLock) { if (IsConnectionAlive) { status = ConnectStatus.FAIL_ALREADY_CONNECTED; throw new CallbackException(); } Guid = Guid.NewGuid(); CurSocketCount = 0; CallBackObj = m_clientOps.CallBackObj; HostName = m_clientOps.HostName; Port = m_clientOps.Port; ReceiveType = m_clientOps.ReceiveType; MaxSocketCount = m_clientOps.MaxSocketCount; ConnectionTimeOut = m_clientOps.ConnectionTimeOut; m_curPacketSequence = 0; m_clientSet.Clear(); m_curConnectionTry = 0; lock (m_sendLock) { m_pendingClientSet.Clear(); m_packetQueue.Clear(); m_pendingPacketSet.Clear(); m_errorPacketSet.Clear(); } lock (m_receiveLock) { m_curReceivedPacketId = -1; m_receivedQueue.Clear(); } m_sendReadyEvent.ResetEvent(); if (HostName == null || HostName.Length == 0) { HostName = ServerConf.DEFAULT_HOSTNAME; } if (Port == null || Port.Length == 0) { Port = ServerConf.DEFAULT_PORT; } ClientOps clientOps = new ClientOps(this, HostName, Port, true, ConnectionTimeOut); for (int i = 0; i < MaxSocketCount; i++) { INetworkClient client = new IocpTcpClient(); client.Connect(clientOps); } IsConnectionAlive = true; } } catch (CallbackException) { Task t = new Task(delegate() { OnParallelClientConnected(this, status); }); t.Start(); return; } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); Task t = new Task(delegate() { OnParallelClientConnected(this, ConnectStatus.FAIL_SOCKET_ERROR); }); t.Start(); return; } startSend(); }