Пример #1
0
 private void PacketReceived(TcpNetworkConnection conn, byte[] packet)
 {
     lock (this.ConnectionLock)
     {
         if (this._IsConnected)
         {
             this.DeliverPacket(this.Settings.Serializer.Deserialize(packet));
         }
     }
 }
Пример #2
0
        private void EndAcceptConnection(IAsyncResult result)
        {
            lock (this.ConnectionLock)
            {
                if (this._IsOpen)
                {
                    try
                    {
                        Socket socket = this.Socket.EndAccept(result);
                        if (this.Settings.MaxConnectionCount == 0 || this._ConnectionCount < this.Settings.MaxConnectionCount)
                        {
                            int id = this.ConnectionIDCounter++;
                            if (id == 0) id = this.ConnectionIDCounter++;
                            SocketError e;

                            try
                            {
                                socket.Send(BitConverter.GetBytes(id), 0, 4, SocketFlags.None, out e);
                            }
                            catch
                            {
                                e = SocketError.SocketError;
                            }

                            if (e == SocketError.Success)
                            {
                                TcpNetworkConnection connection = new TcpNetworkConnection(id, socket, ref this.ConnectionLock);
                                connection.OnDisconnect += new Action<TcpNetworkConnection>(ConnectionDisconnected);
                                connection.OnPacketReceived += new Action<TcpNetworkConnection, byte[]>(PacketReceived);
                                connection.Connect();
                                this.Connections.Add(id, connection);
                                this._ConnectionCount++;
                                // connection.Send(new NetworkPacket { Service = -1, Targets = new int[1] { id }, Data = id });
                                if (this.OnConnectionConnected != null) this.OnConnectionConnected(this, connection);
                            }
                        }
                        else
                        {
                            try
                            {
                                socket.BeginSend(new byte[4], 0, 4, SocketFlags.None, null, null);
                            }
                            catch { }
                        }

                        this.BeginAcceptConnection();
                    }
                    catch (SocketException e)
                    {
                        this.Error(e.SocketErrorCode);
                    }
                }
            }
        }
Пример #3
0
 private void ConnectionDisconnected(TcpNetworkConnection conn)
 {
     lock (this.ConnectionLock)
     {
         if (this._IsConnected)
         {
             this.Disconnect();
         }
     }
 }
Пример #4
0
 private void ConnectionDisconnected(TcpNetworkConnection connection)
 {
     lock (this.ConnectionLock)
     {
         this.Connections.Remove(connection.ID);
         if (this.OnConnectionDisconnected != null) this.OnConnectionDisconnected(this, connection);
     }
 }