Пример #1
0
        /// <summary>
        /// This creates a requester, that uses given callback for processing remote requests
        /// TODO: WARNING: not completely implemented
        /// </summary>
        /// <typeparam name="TSend"></typeparam>
        /// <typeparam name="TReceive"></typeparam>
        /// <param name="sendFactory"></param>
        /// <param name="receiveFactory"></param>
        /// <param name="callback"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        public IClientPacketRequester <TSend, TReceive> CreatePacketRequester <TSend, TReceive>(string uniqueName,
                                                                                                INetworkPacketFactory <TSend> sendFactory,
                                                                                                INetworkPacketFactory <TReceive> receiveFactory,
                                                                                                ClientPacketRequestDelegate <TSend, TReceive> callback,
                                                                                                PacketFlags flags)
            where TReceive : INetworkPacket
            where TSend : INetworkPacket
        {
            ClientPacketRequesterNetworked <TSend, TReceive> requester = new ClientPacketRequesterNetworked
                                                                         <TSend, TReceive>(callback, this, sendFactory, receiveFactory, uniqueName);

            if ((flags & PacketFlags.TCP) == PacketFlags.None)
            {
                throw new NotImplementedException("Only TCP is currently supported!");
            }



            if (packetTransportersMap.ContainsKey(uniqueName))
            {
                throw new InvalidOperationException("Already constains smth with uniqueName!");
            }

            packetTransportersMap[requester.UniqueName] = requester;
            packetTransporters.Add(requester);

            return(requester);
        }
Пример #2
0
        private void SendReplyPacket <TSend, TReceive>(ClientPacketRequesterNetworked <TSend, TReceive> requester, TReceive packet, int requestID)
            where TSend : INetworkPacket
            where TReceive : INetworkPacket
        {
            if (requester.NetworkID == -1)
            {
                throw new InvalidOperationException("No networkID was assigned to this packet type!");
            }

            // WARNING: NEED SERIOUS OPTIMIZATION!!!! USE A BUFFER FOR THIS INSTEAD OF MASSIVE INSTANCE SPAWNING!!!
            MemoryStream strm = new MemoryStream();

            BinaryWriter bw = new BinaryWriter(strm);

            bw.Write(requester.NetworkID);
            bw.Write(requestID);
            requester.ReceiveFactory.ToStream(bw, packet);

            tcpConnection.SendPacket(strm.ToArray(), TCPPacketBuilder.TCPPacketFlags.None);
        }