Example #1
0
        protected void SendToAsync(EndPoint endPoint, byte[] data, int offset, int count)
        {
            if (null == data || data.Length == 0)
            {
                NetDebug.Log("[NetSocket] SendToAsync, the data can not be null.");
                return;
            }

            if (data.Length > NetDefine.MAX_TCP_MESSAGE_LENGTH)
            {
                NetDebug.Error("[Netsocket] SendToAsync, the data length:{0} is greater message max length:{1}.",
                               data.Length,
                               NetDefine.MAX_TCP_MESSAGE_LENGTH);
                return;
            }

            SocketAsyncEventArgs saea = AllocSAEA();

            try
            {
                EncodeSend(saea, data, offset, count);
                saea.RemoteEndPoint = endPoint;
                bool willRaiseEvent = socket.SendToAsync(saea);

                if (!willRaiseEvent)
                {
                    SendToAsyncCallback(saea);
                }
            }
            catch (Exception ex)
            {
                NetDebug.Log("[NetSocket] SendToAsync, error:{0}.", ex.Message.ToString());
            }
        }
Example #2
0
        private void DecodeReceive(SocketAsyncEventArgs saea)
        {
            try
            {
                byte[] buffer = saea.Buffer;
                int    offset = saea.Offset;
                int    count  = saea.BytesTransferred;

                int error = 0;
                if (!OnDecodeReceive(buffer, offset, count, saea.RemoteEndPoint, out error))
                {
                    if (error > 0)
                    {
                        CloseSAEA(saea);
                        return;
                    }

                    NotifyReceiveMessage(buffer, offset, count, saea.RemoteEndPoint);
                }
            }
            catch (Exception ex)
            {
                NetDebug.Error("[NetSocket] OnReceive, error: {0}.", ex.Message.ToString());
            }
        }
Example #3
0
 public void Write(byte[] data, int offset, int count)
 {
     if (offset > totalSize || count + offset > totalSize)
     {
         NetDebug.Error("[SAEABuffer] write error.");
         return;
     }
     Buffer.BlockCopy(data, 0, buffer, offset, count);
 }
Example #4
0
        private SocketAsyncEventArgs AllocSAEA()
        {
            if (null == saeaMemory)
            {
                NetDebug.Error("[NetSocket] AllocSAEA, the saea memory is null.");
                return(null);
            }

            return(saeaMemory.Alloc());
        }
Example #5
0
 private void EncodeSend(SocketAsyncEventArgs saea, byte[] data, int offset, int count)
 {
     try
     {
         byte[] buffer     = saea.Buffer;
         int    saeaOffset = saea.Offset;
         int    packCount  = count;
         if (OnEncodeSend(buffer, saeaOffset, count, data, offset, out packCount))
         {
             saea.SetBuffer(offset, packCount);
         }
         else
         {
             saea.SetBuffer(data, offset, count);
         }
     }
     catch (Exception ex)
     {
         NetDebug.Error("[NetSocket] OnSend, error: {0}.", ex.Message.ToString());
     }
 }
Example #6
0
        protected virtual void SendAsync(byte[] data)
        {
            if (!ready4Send)
            {
                NetDebug.Log("[NetSocket] SendAsync, not ready for send.");
                return;
            }

            if (null == data || data.Length == 0)
            {
                NetDebug.Log("[NetSocket] SendAsync, the data can not be null.");
                return;
            }

            if (data.Length > NetDefine.MAX_MESSAGE_LENGTH)
            {
                NetDebug.Error("[Netsocket] SendAsync, the data length:{0} is greater message max length:{1}.",
                               data.Length,
                               NetDefine.MAX_MESSAGE_LENGTH);
                return;
            }

            SocketAsyncEventArgs saea = AllocSendSAEA();

            try
            {
                EncodeSend(saea, data);
                bool willRaiseEvent = socket.SendAsync(saea);
                if (!willRaiseEvent)
                {
                    SendAsyncCallback(saea);
                }
            }
            catch (Exception ex)
            {
                NetDebug.Log("[NetSocket] SendAsync, error:{0}.", ex.Message.ToString());
            }
        }