コード例 #1
0
ファイル: Server.cs プロジェクト: prepare/box2c
 private void OnReceiveUDPData(byte packetType, BinaryWrapper reader, IPEndPoint endPoint)
 {
     if (ReceiveUDPData != null)
     {
         ReceiveUDPData(packetType, reader, endPoint);
     }
 }
コード例 #2
0
ファイル: Server.cs プロジェクト: prepare/box2c
 public void OnReceiveTCPData(byte packetType, BinaryWrapper reader, Player player)
 {
     if (ReceiveTCPData != null)
     {
         ReceiveTCPData(packetType, reader, player);
     }
 }
コード例 #3
0
ファイル: Client.cs プロジェクト: prepare/box2c
 internal void OnPlayerDataReceived(ConnectedPlayer player, BinaryWrapper reader)
 {
     if (PlayerDataReceived != null)
     {
         PlayerDataReceived(player, reader);
     }
 }
コード例 #4
0
ファイル: Server.cs プロジェクト: prepare/box2c
 internal void OnPacketReceived(byte packetType, BinaryWrapper reader, IPEndPoint endPoint)
 {
     if (PacketReceived != null)
     {
         PacketReceived(packetType, reader, endPoint);
     }
 }
コード例 #5
0
ファイル: Form3.cs プロジェクト: prepare/box2c
        void Tcp_PacketReceived(byte packetType, Networking.BinaryWrapper reader, System.Net.IPEndPoint endPoint)
        {
            textBox2.Text += "(DEBUG: Client: Received TCP " + packetType.ToString() + ")" + Environment.NewLine;

            if (packetType == Networking.ClientPacketTypeBase.ConnectionAck)
            {
                client.Tcp.NetStreamBinary.Write(Networking.PacketTypeBase.EndOfMessage);
            }
            else if (packetType == MyClientPackets.ClientChat)
            {
                var player = client.Players[reader.ReadInt()];
                textBox2.Text += player.Name + ": " + Networking.StringWriter.Read(reader) + Environment.NewLine;
            }
            else if (packetType == Networking.ClientPacketTypeBase.PlayerData)
            {
                foreach (var p in client.Players)
                {
                    if (listBox1.Items.Count <= p.Index)
                    {
                        listBox1.Items.Add("");
                    }

                    if (!p.Active)
                    {
                        listBox1.Items[p.Index] = "(Disconnected)";
                    }
                    else
                    {
                        listBox1.Items[p.Index] = p.Name;
                    }
                }
            }
        }
コード例 #6
0
ファイル: Client.cs プロジェクト: prepare/box2c
            public void Check()
            {
                while (Client.Available != 0)
                {
                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
                    var        dgram    = Client.Receive(ref endPoint);

                    using (var stream = new MemoryStream(dgram))
                    {
                        var reader = new BinaryWrapper(stream);

                        while (stream.Length != stream.Position)
                        {
                            byte packetType = reader.ReadByte();
                            OnPacketReceived(packetType, reader, endPoint);
                        }
                    }
                }

                if (Stream.BaseStream.Length != 0)
                {
                    var arr = _memory.ToArray();
                    Client.Send(arr, arr.Length);

                    _memory.SetLength(0);
                }
            }
コード例 #7
0
ファイル: Network.cs プロジェクト: prepare/box2c
        public static string Read(BinaryWrapper reader)
        {
            string str = string.Empty;

            int length = reader.ReadInt();

            for (int i = 0; i < length; ++i)
            {
                str += reader.ReadChar();
            }

            return(str);
        }
コード例 #8
0
ファイル: Server.cs プロジェクト: prepare/box2c
            public void Check()
            {
                while (Listener.Available != 0)
                {
                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
                    var        dgram    = Listener.Receive(ref endPoint);

                    using (var stream = new MemoryStream(dgram))
                    {
                        var reader = new BinaryWrapper(stream);

                        while (stream.Length != stream.Position)
                        {
                            byte packetType = reader.ReadByte();
                            Server.OnReceiveUDPData(packetType, reader, endPoint);
                        }
                    }
                }

                byte[] batchArray;

                if (BatchStream.BaseStream.Length != 0)
                {
                    batchArray = _memory.ToArray();
                }
                else
                {
                    batchArray = new byte[0];
                }

                foreach (var c in Server.Players)
                {
                    if (c.Udp.Stream.BaseStream.Length != 0 ||
                        BatchStream.BaseStream.Length != 0)
                    {
                        var arr = ((MemoryStream)c.Udp.Stream.BaseStream).ToArray().Concat <byte>(batchArray).ToArray <byte>();

                        Listener.Send(arr, arr.Length, c.Udp.EndPoint);

                        c.Udp.Stream.BaseStream.SetLength(0);
                    }
                }

                _memory.SetLength(0);
            }
コード例 #9
0
ファイル: Server.cs プロジェクト: prepare/box2c
        public void Check()
        {
            if (!CheckConnect())
            {
                Disconnect();
                return;
            }

            while (Tcp.NetStream.DataAvailable)
            {
                var reader = new BinaryWrapper(Tcp.NetStream);

                while (true)
                {
                    byte packetType = reader.ReadByte();

                    if (packetType == PacketTypeBase.EndOfMessage)
                    {
                        break;
                    }
                    else if (packetType == ServerPacketTypeBase.ConnectionAck)
                    {
                        Active = true;
                        int port = reader.ReadInt();
                        Udp.EndPoint = new IPEndPoint(((IPEndPoint)Tcp.Client.Client.RemoteEndPoint).Address, port);
                        string playerName = StringWriter.Read(reader);

                        Name = playerName;
                    }
                    else if (packetType == ServerPacketTypeBase.Disconnected)
                    {
                        Disconnect();
                    }

                    Tcp.OnPacketReceived(packetType, reader, (IPEndPoint)Tcp.Client.Client.RemoteEndPoint);
                    Server.OnReceiveTCPData(packetType, reader, this);
                }
            }

            if (Tcp.BatchStream.BaseStream.Length != 0)
            {
                Tcp.NetStreamBinary.Write(Tcp.Memory.ToArray(), 0, (int)Tcp.Memory.Length);
                Tcp.Memory.SetLength(0);
            }
        }
コード例 #10
0
ファイル: Form3.cs プロジェクト: prepare/box2c
        void server_ReceiveTCPData(byte packetType, Networking.BinaryWrapper reader, Networking.Player player)
        {
            if (packetType == Networking.ServerPacketTypeBase.ConnectionAck)
            {
                server.AcceptPlayer(player);
            }
            else if (packetType == MyClientPackets.ServerChat)
            {
                var chat = Networking.StringWriter.Read(reader);
                foreach (var p in server.Players)
                {
                    // if p == player continue?
                    p.Tcp.BatchStream.Write(MyClientPackets.ClientChat);
                    p.Tcp.BatchStream.Write(player.Index);
                    Networking.StringWriter.Write(chat, p.Tcp.BatchStream);
                    p.Tcp.BatchStream.Write(Networking.PacketTypeBase.EndOfMessage);
                }
            }

            textBox2.Text += "(DEBUG: Server: Received TCP " + packetType.ToString() + ")" + Environment.NewLine;
        }
コード例 #11
0
ファイル: Network.cs プロジェクト: RubisetCie/box2c
        public static string Read(BinaryWrapper reader)
        {
            string str = string.Empty;

            int length = reader.ReadInt();

            for (int i = 0; i < length; ++i)
                str += reader.ReadChar();

            return str;
        }
コード例 #12
0
ファイル: Client.cs プロジェクト: prepare/box2c
            public void Check()
            {
                if (!Connected)
                {
                    if (_client.ConnectionState == ConnectionState.Connecting &&
                        (DateTime.Now - ConnectionTime).Seconds > 4)
                    {
                        _client.ConnectionState = ConnectionState.Timeout;
                    }

                    return;
                }

                while (Client.Available != 0)
                {
                    var reader = new BinaryWrapper(NetStream);

                    while (true)
                    {
                        byte packetType = reader.ReadByte();

                        if (packetType == PacketTypeBase.EndOfMessage)
                        {
                            break;
                        }
                        else if (packetType == ClientPacketTypeBase.SpawnAck)
                        {
                            int count = reader.ReadInt();
                            _client.Players.SetPlayers(count);
                            _client.PlayerIndex = reader.ReadInt();

                            _client.Players[_client.PlayerIndex].Name = _client.Name;

                            _client.Spawned();
                        }
                        else if (packetType == ClientPacketTypeBase.ConnectionAck)
                        {
                            _client.Connected();
                        }
                        else if (packetType == ClientPacketTypeBase.PlayerData)
                        {
                            int  index  = reader.ReadByte();
                            byte bits   = reader.ReadByte();
                            var  player = _client.GetPlayer(index);

                            if ((bits & ClientPacketTypeBase.PlayerDataBits.ActiveBit) != 0)
                            {
                                player.Active = reader.ReadBoolean();
                            }

                            if ((bits & ClientPacketTypeBase.PlayerDataBits.NameBit) != 0)
                            {
                                player.Name = StringWriter.Read(reader);
                            }

                            if (player.Active == false)
                            {
                                _client.Players.Remove(player);
                            }

                            _client.OnPlayerDataReceived(player, reader);
                        }

                        OnPacketReceived(packetType, reader, (IPEndPoint)Client.Client.RemoteEndPoint);
                    }
                }

                if (BatchStream.BaseStream.Length != 0)
                {
                    NetStreamBinary.Write(_memory.ToArray(), 0, (int)_memory.Length);
                    _memory.SetLength(0);
                }
            }
コード例 #13
0
ファイル: Form3.cs プロジェクト: prepare/box2c
 void server_ReceiveUDPData(byte packetType, Networking.BinaryWrapper reader, System.Net.IPEndPoint endPoint)
 {
     textBox2.Text += "(DEBUG: Server: Received UDP " + packetType.ToString() + ")" + Environment.NewLine;
 }
コード例 #14
0
ファイル: Form3.cs プロジェクト: prepare/box2c
 void Udp_PacketReceived(byte packetType, Networking.BinaryWrapper reader, System.Net.IPEndPoint endPoint)
 {
     textBox2.Text += "(DEBUG: Client: Received UDP " + packetType.ToString() + ")" + Environment.NewLine;
 }