コード例 #1
0
ファイル: Packet.cs プロジェクト: thedanieldude1/DanielCode
		public static byte[] ToBytes(Packet packet){
			byte[] buffer = new byte[16+packet.data.Length];
			var sender = BitConverter.GetBytes((Int32)packet.Sender);
			Array.Copy(sender,buffer,4);
			Array.Copy(BitConverter.GetBytes((Int32)packet.Reciever),0,buffer,4,4);
            Array.Copy(BitConverter.GetBytes((Int32)packet.PacketType), 0, buffer, 8, 4);
			Array.Copy(BitConverter.GetBytes((Int32)packet.data.Length),0,buffer,12,4);
			Array.Copy(packet.data,0,buffer,16,packet.data.Length);
			return buffer;

		}
コード例 #2
0
ファイル: Packet.cs プロジェクト: thedanieldude1/DanielCode
		public static byte[] ArrayToBytes(Packet[] packets){
			int byteamount = 0;
			foreach(Packet p in packets){byteamount+=16+p.data.Length;}
			byte[] buffer=new byte[byteamount];
            int curpos = 0;
            foreach(Packet p in packets)
            {
                var curp = ToBytes(p);
                Array.Copy(curp,0,buffer, curpos , curp.Length);
                curpos += curp.Length;
            }
			
			return buffer;
		}
コード例 #3
0
ファイル: Packet.cs プロジェクト: thedanieldude1/DanielCode
		public static byte[] ArrayToBytes(Packet[] packets){
			int byteamount = 0;
			foreach(Packet p in packets){byteamount+=16+p.data.Length;}
			byte[] buffer=new byte[byteamount];
			for(int i = 0;i<packets.Length;i++){
			var packet = packets[i];
			var sender = BitConverter.GetBytes(packet.Sender);
			Array.Copy(sender,buffer,4);
			Array.Copy(BitConverter.GetBytes(packet.Reciever),0,buffer,4,4);
            Array.Copy(BitConverter.GetBytes(packet.PacketType), 0, buffer, 8, 4);
			Array.Copy(BitConverter.GetBytes(packet.data.Length),0,buffer,12,4);
			Array.Copy(packet.data,0,buffer,16,packet.data.Length);
			
			}
			return buffer;
		}
コード例 #4
0
ファイル: Client.cs プロジェクト: thedanieldude1/DanielCode
		public async Task ProcessPacketsAsync(Packet[] packet){

			foreach(Packet p in packet){
				switch(p.PacketType){
					case 0:
						
						
						Console.WriteLine(Encoding.ASCII.GetString(p.data));
						
						break;
					default:
						Console.WriteLine("Unrecognized Packet Recieved");
						break;
				}
			}
			
		}
コード例 #5
0
ファイル: Server.cs プロジェクト: thedanieldude1/DanielCode
		public async Task ClientConnectionAsync(){
			while(true){
               
				var client = await server.AcceptTcpClientAsync();
				var stream = client.GetStream();
				byte[] length = new byte[4];
				await stream.ReadAsync(length,0,4);
				int count = BitConverter.ToInt32(length,0);
				byte[] buffer = new byte[count];
				stream.Read(buffer,0,count);
				Packet info = Packet.FromBytes(buffer);
				var clientinfo = ClientInfo.FromPacket(info,client);
                Console.WriteLine("Listening For Clients");
                clients.Add(clientinfo);
				Console.WriteLine("SERVER: " +clientinfo.Name+" has joined");
				int g = clients.IndexOf(clientinfo);
                
                //BroadcastData(Packet.ToBytes(new Packet(-3, -1, Encoding.ASCII.GetBytes(clientinfo.Name + " has joined"), 0)), null);// (x)=>c!=clients.IndexOf(x));
				
				Task.Run( async ()=>{ int c = g; await ProcessIncomingDataAsync(c);});
                BroadcastData(Packet.ToBytes(new Packet(0, 0, Encoding.ASCII.GetBytes(clientinfo.Name + " has joined"), 0)), null);
                List<Packet> packets = new List<Packet>();
				for(int i = 0;i<log.Count; i++){
                    var s = log[i];
                    var p = new Packet(-2, -1, Encoding.ASCII.GetBytes(s), 0);

                    packets.Add(p);
                    //Console.WriteLine(s + " " +Packet.ToBytes(p).Length);

                }
				log.Add(clientinfo.Name+" has joined");
                if (packets.Count > 0)
                {
                    SendData(Packet.ArrayToBytes(packets.ToArray()), client);

                    //Console.WriteLine("Sending Log of length "+log.Count);
                }
			}
		}
コード例 #6
0
ファイル: Server.cs プロジェクト: thedanieldude1/DanielCode
		public async Task ProcessPacketsAsync(Packet[] packet, ClientInfo info){
			foreach(Packet p in packet){
				switch(p.PacketType){
					case 0:
						var strea = Encoding.ASCII.GetString(p.data);
						Console.WriteLine(strea);
						log.Add(strea);
						int c = clients.IndexOf(info);
						BroadcastData(Packet.ToBytes(new Packet(-3,-1,Encoding.ASCII.GetBytes(strea),0)),(x)=>clients.IndexOf(x)!=c);
						break;
					case 2:
						BroadcastData(Packet.ToBytes(new Packet(-3,-1,Encoding.ASCII.GetBytes(info.Name+" has disconnected"),0)));
						log.Add(info.Name+" has disconnected");
						Console.WriteLine(info.Name+" has disconnected");
						info.Client.Close();
						clients.Remove(info);
						break;
					default:
						Console.WriteLine("Unrecognized Packet Recieved");
						break;
				}
			}
		}
コード例 #7
0
		public static ClientInfo FromPacket(Packet packet,TcpClient client){
			string name = Encoding.ASCII.GetString(packet.data);
			return new ClientInfo(client,name);
		}
コード例 #8
0
		public static Packet ToPacket(ClientInfo client){
			Packet pack = new Packet(-1,-2,null,5);
			byte[] name = Encoding.ASCII.GetBytes(client.Name);
			pack.data=name;
			return pack;
		}
コード例 #9
0
ファイル: Server.cs プロジェクト: thedanieldude1/DanielCode
        public void ProcessPacketsAsync(Packet[] packet, TcpClient info){
			foreach(Packet p in packet){
				switch(p.PacketType){
					case 0:
						var strea = Encoding.ASCII.GetString(p.data);
						Console.WriteLine(strea);
						log.Add(strea);
						//int c = clients.IndexOf(info);
                        //int g = c;
                        foreach (ClientInfo x in clients)
                        {
                            if (info != x.Client)
                            {
                                SendData(Packet.ToBytes(new Packet(0, 0, Encoding.ASCII.GetBytes(strea), 0)),x.Client);
                            }
                        }
						break;
					case 2:
                       //var clientinfo = clients.Find((x) => x.Client == info);
                       // Disconnect(clientinfo);
						break;
					default:
						Console.WriteLine("Unrecognized Packet Recieved");
						break;
				}
			}
		}