Esempio n. 1
0
        private void OnNetworkClientReceivedData(byte[] data, IPEndPoint receivedFrom)
        {
            if (!Datagram.Verify(data, 0, data.Length))
            {
                return;
            }

            if (ReceivedDataCommand != null)
            {
                var datagram = DatagramFactory.Create(data);

                ReceivedDataCommand.Invoke(this, NetworkPoints.GetOrAdd(new NetworkPointBase(receivedFrom)), DataCommandFactory.Create(datagram));
            }
        }
Esempio n. 2
0
        private void Send(IDataCommand dataCommand, IPEndPoint remoteEndPoint)
        {
            var payloadDataEntity = dataCommand.PayloadObject as PayloadDataEntity;

            if (payloadDataEntity != null)
            {
                payloadDataEntity.SourceId       = new byte[16]; // TODO
                payloadDataEntity.TargetEndPoint = remoteEndPoint;
            }

            if (dataCommand is IDataCommandRequest)
            {
                NetworkClient.Send(DatagramFactory.Create(dataCommand as IDataCommandRequest).ConvertToBytes(), remoteEndPoint);
            }
            else if (dataCommand is IDataCommandResponse)
            {
                NetworkClient.Send(DatagramFactory.Create(dataCommand as IDataCommandResponse).ConvertToBytes(), remoteEndPoint);
            }
        }
Esempio n. 3
0
        public static Datagram Create(IStreamBuffer streamBuffer)
        {
            var index = -1;

            while ((index = streamBuffer.IndexOf(Datagram.DatagramHeader)) >= 0)
            {
                streamBuffer.Seek(index);
                if (streamBuffer.Length < 13)
                {
                    return(null);
                }

                var buffer = new byte[5];
                streamBuffer.ReadOnly(buffer, 0, buffer.Length);

                var length = (buffer[1] << 24) + (buffer[2] << 16) + (buffer[3] << 8) + (buffer[4]);
                if (length + 13 >= streamBuffer.Capacity)
                {
                    streamBuffer.Reset();
                    return(null);
                }
                if (streamBuffer.Length < length + 13)
                {
                    return(null);
                }

                var data = new byte[length + 13];
                streamBuffer.ReadOnly(data, 0, data.Length);

                if (DatagramFactory.Verify(data))
                {
                    streamBuffer.Seek(data.Length);
                    return(DatagramFactory.Create(data));
                }
                else
                {
                    streamBuffer.Seek(1);
                }
            }

            return(null);
        }