Пример #1
0
 /// <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.PacketType)
     {
         case ParallelPacketType.DATA:
             if (ReceiveType == ReceiveType.BURST)
             {
                 OnParallelSocketReceived(this, receivedParallelPacket);
             }
             else if (ReceiveType == ReceiveType.SEQUENTIAL)
             {
                 lock (m_receiveLock)
                 {
                     m_receivedQueue.Enqueue(receivedParallelPacket);
                     while (!m_receivedQueue.IsEmpty() && m_curReceivedPacketId + 1 == m_receivedQueue.Peek().PacketID)
                     {
                         ParallelPacket curPacket = m_receivedQueue.Dequeue();
                         m_curReceivedPacketId = curPacket.PacketID;
                         if (m_curReceivedPacketId == long.MaxValue)
                         {
                             m_curReceivedPacketId = -1;
                         }
                         OnParallelSocketReceived(this, curPacket);
                     }
                 }
             }
             break;
         default:
             socket.Disconnect(); // Invalid protocol
             break;
     }
 }
Пример #2
0
 public void AddSocket(INetworkSocket socket)
 {
     lock (m_generalLock)
     {
         if (MaxStreamCountPerSocket != SocketCount.Infinite && m_clientSet.Count > MaxStreamCountPerSocket)
         {
             socket.Disconnect();
             return;
         }
         m_clientSet.Add(socket);
     }
     CurSocketCount++;
     ((IocpTcpSocket)socket).CallBackObj = this;
     ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.READY, null);
     socket.Send(sendPacket.PacketRaw);
     lock (m_sendLock)
     {
         m_pendingClientSet.Add(socket);
         if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
             m_sendReadyEvent.SetEvent();
     }
 }
        /// <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="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.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;
                default:
                    socket.Disconnect(); // Invalid protocol
                    break;
            }
        }
Пример #5
0
 /// <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.PacketType)
     {
         case ParallelPacketType.IDENTITY_RESPONSE:
             PacketSerializer<IdentityResponse> serializer = new PacketSerializer<IdentityResponse>(receivedParallelPacket.PacketRaw,receivedParallelPacket.HeaderSize,receivedParallelPacket.DataByteSize);
             IdentityResponse response = serializer.ClonePacketObj();
             Guid guid = response.m_guid;
             int streamCount = response.m_streamCount;
             lock (m_listLock)
             {
                 if (m_socketMap.ContainsKey(guid))
                 {
                     m_socketMap[guid].AddSocket(socket);
                 }
                 else
                 {
                     if (CallBackObj == null)
                     {
                         socket.Disconnect();
                         return;
                     }
                     
                     if (Acceptor.OnAccept(this, socket.IPInfo, streamCount))
                     {
                         // Create new Parallel Socket
                         IParallelSocketCallback socketCallback = Acceptor.GetSocketCallback();
                         ParallelSocket parallelSocket = new ParallelSocket(guid,socket, this);
                         parallelSocket.CallBackObj = socketCallback;
                         parallelSocket.Start();
                         m_socketMap[guid] = parallelSocket;
                         OnParallelServerAccepted(this, parallelSocket);
                     }
                     else
                     {
                         // Rejected by server
                         socket.Disconnect();
                     }
                 }
             }
             break;
         default:
             // Invalid protocol
             socket.Disconnect();
             break;
     }
 }