Example #1
0
        private void EndReceive(IAsyncResult result)
        {
            if (stage == PerStage.NotConnected)
            {
                return;
            }
            lock (m_lockread)
            {
                try
                {
                    int len = m_socket.EndReceive(result);
                    if (len == 0)
                    {
                        Disconnect(); return;
                    }

                    byte[] buf = Crypt.Decode(this.m_Connbuffer, len);
                    for (int i = 0; i < buf.Length; i++)
                    {
                        JHSNetworkReader ds = PacketFarmer.Accumulate(buf[i]);
                        if (ds != null)
                        {
                            if (HandleReader(ds))
                            {
                                if (NetConfig.UseStatistics)
                                {
                                    PacketsRec += 1;
                                    BitesRev   += len;
                                }
                            }
                        }
                    }
                    m_socket.BeginReceive(this.m_Connbuffer, 0, this.m_Connbuffer.Length, SocketFlags.None, asyncrec, null);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                }
                catch (SocketException sk)
                {
                    if (NetConfig.logFilter >= JHSLogFilter.Developer)
                    {
                        JHSDebug.LogError("JHSConnection :: Excepiton:" + sk.GetErrorCode());
                    }
                    Disconnect();
                }
                catch (Exception ex)
                {
                    if (NetConfig.logFilter >= JHSLogFilter.Developer)
                    {
                        JHSDebug.LogError("JHSConnection :: Excepiton:" + ex.ToString());
                    }
                    Disconnect();
                }
            }
        }
Example #2
0
 public override void Deserialize(JHSNetworkReader reader)
 {
     op = (SearchMatchOperations)reader.ReadByte();
     if (op == SearchMatchOperations.Search)
     {
         value = reader.ReadPackedUInt32();
     }
     if (op == SearchMatchOperations.START)
     {
         IP   = reader.ReadString();
         port = reader.ReadInt16();
     }
 }
        public JHSNetworkReader Accumulate(byte bt)
        {
            btbuf.Add(bt);
            if (btbuf.Count < PACKAGE_HEADER_LEN)
            {
                return(null);
            }

            byte[] buff = btbuf.ToArray();
            uint   head = ReadUInt32(buff, 0);
            uint   len  = ReadUInt16(buff, 4);
            int    ptr  = btbuf.Count - PACKAGE_HEADER_LEN;

            if (head == PACKAGE_HEADER_ID)
            {
                if (btbuf.Count >= 65536)
                {
                    btbuf.Clear();
                    return(null);
                }

                if (len == ptr)
                {
                    if (NetConfig.logFilter >= JHSLogFilter.Developer)
                    {
                        JHSDebug.Log("JHSPacketFarmer Read:" + BitConverter.ToString(buff));
                    }

                    btbuf.Clear();
                    var x = new JHSNetworkReader(buff);
                    x.ReadBytes(PACKAGE_HEADER_LEN);
                    return(x);
                }
            }

            return(null);
        }
Example #4
0
 protected bool HandleReader(JHSNetworkReader reader)
 {
     try
     {
         ushort sz        = reader.ReadUInt16();
         short  msgType   = reader.ReadInt16();
         byte[] msgBuffer = reader.ReadBytes(sz);
         if (isClient)
         {
             JHSNetworkReader msgReader = new JHSNetworkReader(msgBuffer);
             if (m_MessageHandlersDict.ContainsKey(msgType))
             {
                 JHSNetworkClient.PushMessage(new JHSNetworkMessage()
                 {
                     msgType = msgType,
                     reader  = msgReader,
                     conn    = this
                 });
             }
             else
             {
                 if (NetConfig.logFilter >= JHSLogFilter.Error)
                 {
                     JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId);
                 }
                 if (NetConfig.UseStatistics)
                 {
                     ReadError += 1;
                 }
             }
         }
         else
         {
             JHSNetworkReader          msgReader   = new JHSNetworkReader(msgBuffer);
             JHSNetworkMessageDelegate msgDelegate = null;
             if (m_MessageHandlersDict.ContainsKey(msgType))
             {
                 msgDelegate = m_MessageHandlersDict[msgType];
             }
             if (msgDelegate != null)
             {
                 msgDelegate(new JHSNetworkMessage()
                 {
                     msgType = msgType,
                     reader  = msgReader,
                     conn    = this
                 });
             }
             else
             {
                 if (NetConfig.logFilter >= JHSLogFilter.Error)
                 {
                     JHSDebug.LogError("JHSConnection :: Unknown message ID " + msgType + " connId:" + connectionId);
                 }
                 if (NetConfig.UseStatistics)
                 {
                     ReadError += 1;
                 }
             }
         }
     }
     catch { }
     return(true);
 }
 // De-serialize the contents of the reader into this message
 public virtual void Deserialize(JHSNetworkReader reader)
 {
 }
Example #6
0
 public StateObject()
 {
     reader = new JHSNetworkReader(buffer);
 }