示例#1
0
 /// <summary>
 /// Send the specified packet to all clients.
 /// </summary>
 /// <param name="packet">The packet to send.</param>
 internal void SendAll(ConnectionPacket packet)
 {
     if (this.OnSendPacket != null)
     {
         this.OnSendPacket(packet);
     }
 }
示例#2
0
 /// <summary>
 /// Send the specified packet to the other side.
 /// </summary>
 /// <param name="packet">The specified packet.</param>
 /// <returns>Whether the packet was successfully sent.</returns>
 public bool SendPacket(ConnectionPacket packet)
 {
     try
     {
         lock (this.ConnectionStream)
         {
             new BinaryFormatter().Serialize(this.ConnectionStream, packet);
             this.ConnectionStream.Flush();
             return(true);
         }
     }
     catch { this.FireOnDisconnect(); return(false); }
 }
示例#3
0
        private void HandleClient(TcpClient tcpClient, ref bool waiting)
        {
            ConnectionHandler         client       = new ConnectionHandler(tcpClient);
            Action <ConnectionPacket> onPacketSend = p => { if (!client.SendPacket(p))
                                                            {
                                                                this.Close();
                                                            }
            };

            this.OnSendPacket += onPacketSend;
            if (this.OnClientConnected != null)
            {
                this.OnClientConnected(client);
            }
            waiting = false;

            int i = 0;

            while (this.Opened)
            {
                if (i == 9 && !client.Ping())
                {
                    break;
                }
                if (client.Available == 0)
                {
                    Thread.Sleep(50); i++; i %= 10; continue;
                }

                ConnectionPacket packet = client.ReceivePacket();
                try { if (packet.Data.GetType() == typeof(Ping))
                      {
                          continue;
                      }
                }
                catch { }

                Action <object, PacketEventArgs> action;
                if (this.Events.TryGetValue(packet.EventID, out action))
                {
                    action(packet.Data, new PacketEventArgs(packet, client));
                }
                if (client.Events.TryGetValue(packet.EventID, out action))
                {
                    action(packet.Data, new PacketEventArgs(packet, client));
                }
            }

            this.OnSendPacket -= onPacketSend;
        }
示例#4
0
文件: Client.cs 项目: Unnar/SharpBag
        private void Listen()
        {
            int i = 0;

            while (this.Connected)
            {
                if (i == 9 && !this.ConnectionHandler.Ping())
                {
                    break;
                }
                if (this.Connection.Available == 0)
                {
                    Thread.Sleep(50); i++; i %= 10; continue;
                }

                ConnectionPacket packet = this.ConnectionHandler.ReceivePacket();
                try { if (packet.Data.GetType() == typeof(Ping))
                      {
                          continue;
                      }
                }
                catch { }

                Action <object, PacketEventArgs> action;
                if (this.Events.TryGetValue(packet.EventID, out action))
                {
                    action(packet.Data, new PacketEventArgs(packet, this.ConnectionHandler));
                }
                if (this.ConnectionHandler.Events.TryGetValue(packet.EventID, out action))
                {
                    action(packet.Data, new PacketEventArgs(packet, this.ConnectionHandler));
                }
            }

            if (this.OnDisconnected != null)
            {
                this.OnDisconnected();
            }
        }