示例#1
0
 public static void AddCommand(Command cmd)
 {
     try
     {
         if (cmd is AddSocketCommand)
         {
             AddSocketCommand addSocketCommand  = cmd as AddSocketCommand;
             long             theUniqueSocketId = addSocketCommand.theUniqueSocketId;
             TCPSocketContext tCPSocketContext  = new TCPSocketContext(addSocketCommand.theUniqueSocketId, addSocketCommand.theHandler);
             if (!NetLib.dictSocketContext.ContainsKey(theUniqueSocketId))
             {
                 Logger.DEBUG(theUniqueSocketId.ToString() + " added");
                 NetLib.dictSocketContext.Add(theUniqueSocketId, tCPSocketContext);
             }
             else
             {
                 TCPSocketContext tCPSocketContext2 = NetLib.dictSocketContext.get_Item(theUniqueSocketId);
                 Logger.ERROR("NetThread::ProcessCommands socket handle conflict " + tCPSocketContext.GetUniqueSocketId().ToString() + " VS " + tCPSocketContext2.GetUniqueSocketId().ToString());
             }
         }
         else if (cmd is SendPacketCommand)
         {
             SendPacketCommand sendPacketCommand = cmd as SendPacketCommand;
             long theUniqueSocketId2             = sendPacketCommand.theUniqueSocketId;
             if (NetLib.dictSocketContext.ContainsKey(theUniqueSocketId2))
             {
                 TCPSocketContext tCPSocketContext3 = NetLib.dictSocketContext.get_Item(theUniqueSocketId2);
                 Packet           packet            = new Packet();
                 packet.theCreateTimeMS = sendPacketCommand.theCreateTimeMS;
                 packet.theContent      = (sendPacketCommand.theContent.Clone() as byte[]);
                 tCPSocketContext3.Enqueue(packet);
                 NetLib.pendingPacketsLength += (long)packet.theContent.Length;
             }
         }
         else
         {
             CloseSocketCommand closeSocketCommand = cmd as CloseSocketCommand;
             long theUniqueSocketId3 = closeSocketCommand.theUniqueSocketId;
             if (NetLib.dictSocketContext.ContainsKey(theUniqueSocketId3))
             {
                 TCPSocketContext tCPSocketContext4 = NetLib.dictSocketContext.get_Item(theUniqueSocketId3);
                 Logger.DEBUG(theUniqueSocketId3.ToString() + " removed");
                 int num = 0;
                 tCPSocketContext4.HandleClose(out num);
                 NetLib.pendingPacketsLength -= (long)num;
                 NetLib.dictSocketContext.Remove(theUniqueSocketId3);
                 NetLib.PandoraNet_Close(theUniqueSocketId3);
             }
             else
             {
                 Logger.WARN("uniqueSocketId=" + theUniqueSocketId3.ToString() + " alreay missing");
             }
         }
     }
     catch (Exception ex)
     {
         Logger.ERROR(ex.get_StackTrace());
     }
 }
示例#2
0
 public void Close(long uniqueSocketId)
 {
     Logger.DEBUG("Y5" + uniqueSocketId.ToString());
     NetLib.AddCommand(new CloseSocketCommand
     {
         theUniqueSocketId = uniqueSocketId
     });
 }
示例#3
0
 public static void Reset()
 {
     using (Dictionary <long, TCPSocketContext> .Enumerator enumerator = NetLib.dictSocketContext.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             KeyValuePair <long, TCPSocketContext> current = enumerator.get_Current();
             long key = current.get_Key();
             NetLib.PandoraNet_Close(key);
         }
     }
     NetLib.dictSocketContext.Clear();
     NetLib.pendingPacketsLength = 0L;
 }
示例#4
0
        public long AsyncConnect(IPAddress address, ushort port, TCPSocketHandler handler)
        {
            Logger.DEBUG("Y1");
            string ipStr = address.ToString();
            long   num   = NetLib.PandoraNet_AsyncConnect(ipStr, port);

            if (num < 0L)
            {
                Logger.ERROR("NetLib.AsyncConnect ret=" + num.ToString());
                return(-1L);
            }
            NetLib.AddCommand(new AddSocketCommand
            {
                theUniqueSocketId = num,
                theHandler        = handler
            });
            return(num);
        }
        public int HandleInputEvent()
        {
            Logger.DEBUG("Z4");
            if (!this.isOnConnectCalled)
            {
                this.theHandler.OnConnected();
                this.isOnConnectCalled = true;
                return(0);
            }
            int num;

            while (true)
            {
                this.AdjustReceiveBuffer();
                int leftBufferLen = this.receiveBufferCapacity - this.receivedLength;
                num = NetLib.PandoraNet_AsyncRead(this.theUniqueSocketId, leftBufferLen);
                Logger.DEBUG("theUniqueSocketId=" + this.theUniqueSocketId.ToString() + " recvdLen=" + num.ToString());
                if (num < 0)
                {
                    break;
                }
                this.receivedLength += num;
                int num2 = this.SplitReceiveDataToPackets();
                if (num2 > 0)
                {
                    goto Block_3;
                }
                if (num2 < 0)
                {
                    goto Block_4;
                }
            }
            if (num != -100)
            {
                return(-1);
            }
Block_3:
            return(0);

Block_4:
            Logger.ERROR("Z6received data error");
            return(-2);
        }
        public int HandleOutputEvent(out int sentDataSize)
        {
            if (!this.isOnConnectCalled)
            {
                this.theHandler.OnConnected();
                this.isOnConnectCalled = true;
            }
            sentDataSize = 0;
            if (this.pendingPackets.get_Count() == 0)
            {
                return(0);
            }
            Packet packet = this.pendingPackets.Peek() as Packet;

            while (this.peekPacketSentSize < packet.theContent.Length)
            {
                int    num  = packet.theContent.Length - this.peekPacketSentSize;
                string text = Convert.ToBase64String(packet.theContent, this.peekPacketSentSize, num);
                int    num2 = NetLib.PandoraNet_AsyncWrite(text.get_Length(), text, this.theUniqueSocketId);
                if (num2 < 0)
                {
                    if (num2 == -100)
                    {
                        break;
                    }
                    return(-1);
                }
                else
                {
                    this.peekPacketSentSize += num2;
                }
            }
            if (this.peekPacketSentSize == packet.theContent.Length)
            {
                sentDataSize            = this.peekPacketSentSize;
                this.peekPacketSentSize = 0;
                Packet thePacket = this.pendingPackets.Dequeue() as Packet;
                this.theHandler.OnSent(thePacket);
            }
            return(this.pendingPackets.get_Count());
        }
示例#7
0
        public int SendPacket(long uniqueSocketId, byte[] content)
        {
            Logger.DEBUG("Y6" + uniqueSocketId.ToString());
            long pendingPacketsLength = NetLib.GetPendingPacketsLength();

            if ((long)NetFrame.kMaxPendingPacketsLength < pendingPacketsLength + (long)content.Length)
            {
                Logger.ERROR("Y7pending pakcets overflow");
                return(-1);
            }
            if (content.Length == 0)
            {
                Logger.ERROR("Y8empty content is not allowed");
                return(-2);
            }
            NetLib.AddCommand(new SendPacketCommand
            {
                theUniqueSocketId = uniqueSocketId,
                theContent        = content.Clone() as byte[],
                theCreateTimeMS   = (long)DateTime.get_Now().get_Millisecond()
            });
            return(0);
        }
示例#8
0
 public void Init()
 {
     NetLib.Init();
 }
示例#9
0
 public void Drive()
 {
     NetLib.Drive();
 }
示例#10
0
 public void Reset()
 {
     NetLib.Reset();
 }
示例#11
0
 public void Destroy()
 {
     NetLib.Destroy();
 }
示例#12
0
        public static void Drive()
        {
            List <long> list  = new List <long>();
            List <long> list2 = new List <long>();

            using (Dictionary <long, TCPSocketContext> .Enumerator enumerator = NetLib.dictSocketContext.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    KeyValuePair <long, TCPSocketContext> current = enumerator.get_Current();
                    list.Add(current.get_Key());
                }
            }
            using (List <long> .Enumerator enumerator2 = list.GetEnumerator())
            {
                while (enumerator2.MoveNext())
                {
                    long current2 = enumerator2.get_Current();
                    if (NetLib.dictSocketContext.ContainsKey(current2))
                    {
                        long             num = current2;
                        TCPSocketContext tCPSocketContext = NetLib.dictSocketContext.get_Item(num);
                        int num2 = NetLib.PandoraNet_DoSelect(num);
                        if (num2 != 0)
                        {
                            if (num2 == 1)
                            {
                                int num3 = tCPSocketContext.HandleInputEvent();
                                if (num3 < 0)
                                {
                                    int num4 = 0;
                                    tCPSocketContext.HandleClose(out num4);
                                    NetLib.pendingPacketsLength -= (long)num4;
                                    list2.Add(num);
                                }
                            }
                            else if (num2 == 2)
                            {
                                int num5 = 0;
                                int num6 = tCPSocketContext.HandleOutputEvent(out num5);
                                NetLib.pendingPacketsLength -= (long)num5;
                                if (num6 < 0)
                                {
                                    int num7 = 0;
                                    tCPSocketContext.HandleClose(out num7);
                                    NetLib.pendingPacketsLength -= (long)num7;
                                    list2.Add(num);
                                }
                            }
                            else
                            {
                                int num8 = 0;
                                tCPSocketContext.HandleClose(out num8);
                                NetLib.pendingPacketsLength -= (long)num8;
                                list2.Add(num);
                            }
                        }
                    }
                }
            }
            using (List <long> .Enumerator enumerator3 = list2.GetEnumerator())
            {
                while (enumerator3.MoveNext())
                {
                    long current3 = enumerator3.get_Current();
                    NetLib.dictSocketContext.Remove(current3);
                    NetLib.PandoraNet_Close(current3);
                }
            }
        }
示例#13
0
 public static void Destroy()
 {
     NetLib.PandoraNet_Destroy();
 }
示例#14
0
 public static void Init()
 {
     NetLib.PandoraNet_RegisterDataHandler(new NetLib.DataHandler(NetLib.DataCallback));
     NetLib.PandoraNet_RegisterLogHandler(new NetLib.LogHandler(NetLib.LogCallback));
     NetLib.PandoraNet_Init();
 }