Ejemplo n.º 1
0
        public TCPConnection(TCPSetting setting)
        {
            if (null == setting)
            {
                NetDebug.Error("[TCPConnection] the setting can not be null.");
                return;
            }

            this.setting = setting;
            Initialize(this.setting.ioNum);
        }
Ejemplo n.º 2
0
 private void AcceptAsyncCallback(SocketAsyncEventArgs saea)
 {
     try
     {
         if (saea.SocketError == SocketError.Success)
         {
             OnAcceptCallback(saea.AcceptSocket);
         }
     }
     catch (Exception ex)
     {
         NetDebug.Error("[TCPServer] AcceptAsyncCallback error:{0}.", ex.Message.ToString());
     }
     finally
     {
         AcceptAsync();
     }
 }
Ejemplo n.º 3
0
        public void Connect(UDPSetting setting)
        {
            if (null == setting)
            {
                NetDebug.Error("[UDPConnection] the setting can not be null.");
                return;
            }

            this.setting = setting;
            Initialize(this.setting.ioNum);

            remoterPoint = NetUtility.GetIPEndPoint(setting.host, setting.port);
            socket       = new Socket(remoterPoint.AddressFamily,
                                      SocketType.Dgram,
                                      ProtocolType.Udp);
            socket.Connect(remoterPoint);

            InitKCP(setting);
        }
Ejemplo n.º 4
0
 private void ConnectAsyncCallback(SocketAsyncEventArgs saea)
 {
     try
     {
         if (saea.SocketError == SocketError.Success)
         {
             OnConnectCallback(true, saea.AcceptSocket);
             saea.AcceptSocket = null;
         }
         else
         {
             OnConnectCallback(false, null);
         }
     }
     catch (Exception ex)
     {
         NetDebug.Error("[TCPClient] conncet callback error:{0}.", ex.Message.ToString());
         OnConnectCallback(false, null);
     }
 }
Ejemplo n.º 5
0
        public void Listen(UDPSetting setting)
        {
            if (null == setting)
            {
                NetDebug.Error("[UDPConnection] the setting can not be null.");
                return;
            }

            this.setting = setting;
            Initialize(this.setting.ioNum);

            localEndPoint = NetUtility.GetIPEndPoint(setting.host, setting.port);
            socket        = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
            socket.Bind(localEndPoint);

            kcpHandle = new KCPServerHandle(setting);
            kcpHandle.onSendHandle    = OnSend;
            kcpHandle.onReceiveHandle = OnReceive;

            Start();
        }
Ejemplo n.º 6
0
        public void Copy(byte[] data, int offset, int count)
        {
            if (null == data)
            {
                NetDebug.Error("[DynamicBuffer] Copy, the data can not be null.");
                return;
            }

            int reserveBytesLength = GetReserveBytesLength();

            if (reserveBytesLength >= count)
            {
                System.Buffer.BlockCopy(data, offset, buffer, usedLength, count);
            }
            else
            {
                int    size  = buffer.Length + count - reserveBytesLength;
                byte[] alloc = new byte[size];
                System.Buffer.BlockCopy(buffer, 0, alloc, 0, usedLength);
                System.Buffer.BlockCopy(data, offset, alloc, usedLength, count);
                buffer = alloc;
            }
            usedLength += count;
        }
Ejemplo n.º 7
0
        public void Copy(byte[] data, int offset, int count)
        {
            if (null == data)
            {
                NetDebug.Error("[MemoryBufferStream] copy, the data can not be null.");
                return;
            }

            int freeLength = GetFreeLength();

            if (freeLength >= count)
            {
                System.Buffer.BlockCopy(data, offset, buffer, length, count);
            }
            else
            {
                int    size  = buffer.Length + count - freeLength;
                byte[] alloc = new byte[size];
                System.Buffer.BlockCopy(buffer, 0, alloc, 0, length);
                System.Buffer.BlockCopy(data, offset, alloc, length, count);
                buffer = alloc;
            }
            length += count;
        }