public void AddSocket(INetworkSocket socket) { m_clientSet.Add(socket); CurSocketCount++; ((IocpTcpSocket)socket).CallBackObj = this; ParallelPacket sendPacket = new ParallelPacket(getCurPacketSequence(), ParallelPacketType.READY, null); socket.Send(sendPacket.GetPacketRaw()); lock (m_sendLock) { m_pendingClientSet.Add(socket); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } }
/// <summary> /// NewConnection callback /// </summary> /// <param name="socket">client socket</param> public void OnNewConnection(INetworkSocket socket) { // Request Identity of new connected socket ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.IDENTITY_REQUEST,null); socket.Send(sendPacket.GetPacketRaw()); }
/// <summary> /// Receive callback /// </summary> /// <param name="socket">client socket</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkSocket socket, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.GetPacketType()) { case ParallelPacketType.IDENTITY_RESPONSE: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.GetPacketRaw(),receivedParallelPacket.GetHeaderSize(),receivedParallelPacket.GetDataByteSize()); Guid guid = serializer.GetPacket().m_guid; lock (m_listLock) { if (m_socketMap.ContainsKey(guid)) { m_socketMap[guid].AddSocket(socket); } else { IParallelSocketCallback socketCallback = CallBackObj.OnAccept(this, socket.IPInfo); if (socketCallback != null) { // Create new Parallel Socket ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this); parallelSocket.CallBackObj = socketCallback; parallelSocket.Start(); m_socketMap[guid] = parallelSocket; } else { // Rejected by server socket.Disconnect(); } } } break; default: // Invalid protocol socket.Disconnect(); break; } }
/// <summary> /// Receive callback /// </summary> /// <param name="client">client</param> /// <param name="receivedPacket">received packet</param> public void OnReceived(INetworkClient client, Packet receivedPacket) { ParallelPacket receivedParallelPacket = new ParallelPacket(receivedPacket); switch (receivedParallelPacket.GetPacketType()) { case ParallelPacketType.DATA: if (m_receiveType == ReceiveType.BURST) { if (CallBackObj != null) { // Task t = new Task(delegate() // { // CallBackObj.OnReceived(this, receivedParallelPacket); // }); // t.Start(); CallBackObj.OnReceived(this, receivedParallelPacket); } } else if (m_receiveType == ReceiveType.SEQUENTIAL) { lock (m_receiveLock) { m_receivedQueue.Enqueue(receivedParallelPacket); while (m_curReceivedPacketId == -1 || (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().GetPacketID())) { ParallelPacket curPacket = m_receivedQueue.Dequeue(); m_curReceivedPacketId = curPacket.GetPacketID(); if (CallBackObj != null) { // Task t = new Task(delegate() // { // CallBackObj.OnReceived(this, curPacket); // }); // t.Start(); m_callBackObj.OnReceived(this, curPacket); } } } } break; case ParallelPacketType.IDENTITY_REQUEST: PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(new IdentityResponse(Guid)); ParallelPacket sendPacket=new ParallelPacket(getCurPacketSequence(),ParallelPacketType.IDENTITY_RESPONSE,serializer.GetPacketRaw()); client.Send(sendPacket.GetPacketRaw()); break; case ParallelPacketType.READY: lock (m_sendLock) { m_pendingClientSet.Add(client); if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0)) m_sendReadyEvent.SetEvent(); } break; } }